-
Notifications
You must be signed in to change notification settings - Fork 0
/
4cdl.py
262 lines (212 loc) · 8.71 KB
/
4cdl.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
from requests import request, Response
from bs4 import BeautifulSoup
from datetime import datetime
from argparse import ArgumentParser, SUPPRESS
from typing import Self
FOURCHAN_IMAGE_CDN = "https://i.4cdn.org"
FOURCHAN_JSON_CDN = "https://a.4cdn.org"
FOURCHAN_BOARD_URL = "https://boards.4channel.org"
class FourChanScraper:
# (private) __write_file__ - Writes data )bytes) as file to disk
def __write_file__(self: Self, name: str, content: bytes) -> None:
with open(name, "wb") as w:
if w.writable():
w.write(content)
# (private): Print error message and exit
def __error__(self: Self, err: str) -> None:
print(f"Error: {err}")
exit()
# (private): Create a get request through requests module
def __get_requ__(self: Self, url: str) -> Response:
ask = request("GET", url)
if ask.status_code != 200:
self.__error__(
f"GET request failed. Status code: {ask.status_code}"
)
return ask
# (unused): Scrap a page for images (if any)
def __unused_front_scrape__(self: Self, board: str, page: int) -> None:
url = f"{FOURCHAN_BOARD_URL}/{board}/{page}"
resp = self.__get_requ__(url)
soup = BeautifulSoup(resp.text, "html.parser")
sfa = soup.find_all("a", attrs={ "class": "fileThumb" })
for each in sfa:
img_url = each.get("href").replace("//", "https://")
file_name = img_url.replace(f"{FOURCHAN_IMAGE_CDN}/{board}/", "")
resp = self.__get_requ__(img_url)
print(f"Got: {file_name}")
self.__write_file__(file_name, resp.content)
# (public): Scrap any image page of a specific page (if any)
def chan_page_images(self: Self, board: str, page: int) -> None:
if page < 1:
self.__error__("Page number can't be zero or negative")
url = f"{FOURCHAN_JSON_CDN}/{board}/{page}.json"
resp = self.__get_requ__(url)
for list_iter_one in resp.json()["threads"]: # "threads" doesn't implies user created threads
for list_iter_two in list_iter_one["posts"]:
if "tim" and "ext" in list_iter_two:
file_name = f"{list_iter_two['tim']}{list_iter_two['ext']}"
url = f"{FOURCHAN_IMAGE_CDN}/{board}/{file_name}"
resp = self.__get_requ__(url)
print(f"Got: {file_name}")
self.__write_file__(file_name, resp.content)
# (public): Scrap all images of a specific thread (if any)
def chan_thread_images(self: Self, board: str, thread_id: int) -> None:
url = f"{FOURCHAN_JSON_CDN}/{board}/thread/{thread_id}.json"
resp = self.__get_requ__(url)
for each in resp.json()["posts"]:
if "tim" and "ext" in each:
file_name = f"{each['tim']}{each['ext']}"
url = f"{FOURCHAN_IMAGE_CDN}/{board}/{file_name}"
resp = self.__get_requ__(url)
print(f"Got: {file_name}")
self.__write_file__(file_name, resp.content)
# (public): Scrap all threads on a page and look for images (if any)
def chan_all_threads_images(self: Self, board: str, page: int) -> None:
if page <= 1:
url = f"{FOURCHAN_BOARD_URL}/{board}"
resp = self.__get_requ__(url)
else:
url = f"{FOURCHAN_BOARD_URL}/{board}/{page}"
resp = self.__get_requ__(url)
soup = BeautifulSoup(resp.text, "html.parser")
sfi = soup.find_all("div", attrs={ "class": "thread" })
for each in sfi:
scrap_id = each["id"].removeprefix("t")
url = f"{FOURCHAN_JSON_CDN}/{board}/thread/{scrap_id}.json"
resp = self.__get_requ__(url)
for each in resp.json()["posts"]:
if "tim" and "ext" in each:
file_name = f"{each['tim']}{each['ext']}"
url = f"{FOURCHAN_IMAGE_CDN}/{board}/{file_name}"
resp = self.__get_requ__(url)
print(f"Got: {file_name}")
self.__write_file__(file_name, resp.content)
# (public): Scrap all comments on a page (if any)
def chan_page_comments(self: Self, board: str, page: int) -> None:
if page < 1:
self.__error__("Page number can't be zero or negative")\
url = f"{FOURCHAN_JSON_CDN}/{board}/{page}.json"
resp = self.__get_requ__(url)
for list_iter_one in resp.json()["threads"]:
for list_iter_two in list_iter_one["posts"]:
if "com" in list_iter_two:
print(
f"ID: {list_iter_two['no']}\n"
f"User: {list_iter_two['name']}\n"
f"Time: {datetime.fromtimestamp(list_iter_two['time'])}\n"
f"Comment: {list_iter_two['com']}\n"
)
# (public): Scrap all comments of a specific thread (if any)
def chan_thread_comments(self: Self, board: str, thread_id: int) -> None:
url = f"{FOURCHAN_JSON_CDN}/{board}/thread/{thread_id}.json"
resp = self.__get_requ__(url)
for each in resp.json()["posts"]:
if "com" in each:
print(
f"ID: {each['no']}\n"
f"User: {each['name']}\n"
f"Time: {datetime.fromtimestamp(each['time'])}\n"
f"Comment: {each['com']}\n"
)
# (public): Scrap all threads on a page and look for comments (if any)
def chan_all_threads_comments(self: Self, board: str, page: int) -> None:
if page < 1:
self.__error__("Page number can't be zero or negative")
if page == 1:
url = f"{FOURCHAN_BOARD_URL}/{board}"
resp = self.__get_requ__(url)
else:
url = f"{FOURCHAN_BOARD_URL}/{board}/{page}"
resp = self.__get_requ__(url)
soup = BeautifulSoup(resp.text, "html.parser")
sfi = soup.find_all("div", attrs={ "class": "thread" })
for each in sfi:
scrap_id = each["id"].removeprefix("t")
url = f"{FOURCHAN_JSON_CDN}/{board}/thread/{scrap_id}.json"
resp = self.__get_requ__(url)
for each in resp.json()["posts"]:
if "com" in each:
print(
f"ID: {each['no']}\n"
f"User: {each['name']}\n"
f"Time: {datetime.fromtimestamp(each['time'])}\n"
f"Comment: {each['com']}\n"
)
# Main access function
def main():
parser = ArgumentParser(
add_help=False,
usage=SUPPRESS
)
chan = FourChanScraper()
# Add arguments
parser.add_argument(
"--page-images",
action="store_true",
dest="page_images",
help="Scrap all images on a page"
)
parser.add_argument(
"--thread-images",
action="store_true",
dest="thread_images",
help="Scrap all images of a specific thread"
)
parser.add_argument(
"--all-threads-images",
action="store_true",
dest="all_threads_images",
help="Scrap all threads on a page and look for images"
)
parser.add_argument(
"--page-comments",
action="store_true",
dest="page_comments",
help="Scrap all comments on a page"
)
parser.add_argument(
"--thread-comments",
action="store_true",
dest="thread_comments",
help="Scrap all comments of a specific thread"
)
parser.add_argument(
"--all-threads-comments",
action="store_true",
dest="all_threads_comments",
help="Scrap all threads on a page and look for comments"
)
parser.add_argument(
"--board",
type=str
)
parser.add_argument(
"--page",
type=int
)
parser.add_argument(
"--thread",
type=int
)
args = parser.parse_args()
# Check for arguments
chan.chan_page_images(
board=args.board, page=args.page
) if args.page_images == True else None
chan.chan_thread_images(
board=args.board, thread_id=args.thread
) if args.thread_images == True else None
chan.chan_all_threads_images(
board=args.board, page=args.page
) if args.all_threads_images == True else None
chan.chan_page_comments(
board=args.board, page=args.page
) if args.page_comments == True else None
chan.chan_thread_comments(
board=args.board, thread_id=args.thread
) if args.thread_comments == True else None
chan.chan_all_threads_comments(
board=args.board, page=args.page
) if args.all_threads_comments == True else None
main()