/
fourchan.py
81 lines (59 loc) · 2.26 KB
/
fourchan.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
#!/usr/bin/env python3
import urllib.request, json, os, random, copy
from settings import Settings
from setters import fromstr
import util
exts = ['.png', '.jpg', '.jpeg']
def set4chan():
hdr = { 'User-Agent' : "Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Firefox/24.0" }
boardslist = json.loads(urllib.request.urlopen(urllib.request.Request("http://api.4chan.org/boards.json", headers=hdr)).read().decode('utf8'))['boards']
numpages = 0
boards = []
for board in boardslist:
if hasattr(Settings, "BOARD"):
if board['board'] == Settings.BOARD or board['title'] == Settings.BOARD:
boards.append(board)
continue
if hasattr(Settings, "BOARDS"):
if board['board'] in Settings.BOARDS or board['title'] in Settings.BOARDS:
boards.append(board)
continue
if len(boards) == 0:
boards.append({'board': "w", 'pages': 11})
set = False
random.shuffle(boards)
for board in boards:
pages = list(range(0, board['pages']))
random.shuffle(pages)
for page in pages:
req = urllib.request.Request("http://api.4chan.org/" + board['board'] + "/" + str(page) + ".json", headers=hdr)
page = json.loads(urllib.request.urlopen(req).read().decode('utf8'))
threads = copy.copy(page['threads'])
random.shuffle(threads)
for thread in threads:
op = thread['posts'][0]
if 'sticky' in op:
break
req = urllib.request.Request("http://api.4chan.org/" + board['board'] + "/res/" + str(op['no']) + ".json", headers=hdr)
posts = json.loads(urllib.request.urlopen(req).read().decode('utf8'))['posts']
random.shuffle(posts)
for post in posts:
if not ('w' in post and 'h' in post and 'ext' in post and 'tim' in post):
break
width = int(post['w'])
height = int(post['h'])
ext = post['ext']
if ext.lower() in exts and width >= Settings.WIDTH and height >= Settings.HEIGHT:
fname = str(post['tim']) + ext
path = util.wget("http://images.4chan.org/" + board['board'] + "/src/" + fname, Settings.DIRECTORY)
set = fromstr(Settings.WALLMANAGER)(path, width, height)
if set:
print("Set: " + fname + " from /" + board['board'] + "/" + str(op['no']) + ": " + str(width) + "x" + str(height))
break
if set:
break
if set:
break
if set:
break
return True