forked from LeoEkky/OpenAI-Codex-Code-Generation
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path4chan search.py
More file actions
59 lines (49 loc) · 1.74 KB
/
Copy path4chan search.py
File metadata and controls
59 lines (49 loc) · 1.74 KB
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
from bs4 import BeautifulSoup
import requests
import urllib
import re
import os
import sys
def get_soup(url):
return BeautifulSoup(requests.get(url).text, "lxml")
def get_main_page(board):
return get_soup("http://boards.4chan.org/{}/".format(board))
def get_thread_page(board, thread_id):
return get_soup("http://boards.4chan.org/{}/res/{}".format(board, thread_id))
def get_thread_ids(board):
main_page = get_main_page(board)
thread_ids = []
for link in main_page.find_all("a", class_="replylink"):
thread_ids.append(link["href"][1:])
return thread_ids
def get_image_urls(thread_id):
thread_page = get_thread_page("g", thread_id)
image_urls = []
for post in thread_page.find_all("div", class_="postContainer"):
for link in post.find_all("a"):
if link.has_attr("href") and link["href"].endswith("jpg"):
image_urls.append(link["href"])
return image_urls
def download_image(url, filename):
print ("Downloading {} to {}".format(url, filename))
urllib.urlretrieve(url, filename)
def download_images(board):
thread_ids = get_thread_ids(board)
for thread_id in thread_ids:
image_urls = get_image_urls(thread_id)
for url in image_urls:
download_image(url, url.split("/")[-1])
def main():
if len(sys.argv) < 2:
print ("Usage: python 4chan.py <board> [<thread_id>]")
sys.exit(1)
board = sys.argv[1]
if len(sys.argv) > 2:
thread_id = sys.argv[2]
image_urls = get_image_urls(thread_id)
for url in image_urls:
download_image(url, url.split("/")[-1])
else:
download_images(board)
if __name__ == "__main__":
main()