-
Notifications
You must be signed in to change notification settings - Fork 3
/
sosach.py
80 lines (65 loc) · 2.51 KB
/
sosach.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
# coding: utf-8
import aiohttp
from urllib.parse import urljoin
import random
BOARDS = ['au', 'c', 'mlp', 'mov', 'sci', 'spc', 'gd', 'v', 'a', 'b', 'int', 'wm', 'tv']
FAPBOARDS = ['b', 'a', 'hc', 'e', 'gg', 'int', 'mov']
WORDS = ['webm', u'шebm', 'mp4', u'шебм', u'цуиь']
FAPWORDS = ['fap', u'фап', 'porn', u'порн', u'прон', 'hentai']
NOWORDS = []
async def get_json(url):
async with aiohttp.ClientSession() as session:
async with session.get(url) as s:
if s.status != 200:
return None
return await s.json()
async def webm_threads(board='b'):
t = await get_json('https://2ch.hk/%s/catalog.json' % board)
t = t['threads']
return [x['num'] for x in t if any(word in x['subject'].lower() for word in WORDS) and not any(word in x['subject'].lower() for word in NOWORDS)]
async def fap_threads(fg=False):
res = []
for board in FAPBOARDS if not fg else ['fg']:
t = await get_json('https://2ch.hk/%s/catalog.json' % board)
t = t['threads']
for x in t:
text = x['subject'].lower()+'\n'+x['comment'].lower()
if any(word in text for word in FAPWORDS):
res.append((board, x['num']))
return res
async def files(board, num):
tr = await get_json('https://2ch.hk/%s/res/%s.json' % (board, num))
if not tr:
return []
res = []
posts = tr['threads'][0]['posts']
for post in posts:
for f in post.get('files', []):
if f['path'].endswith('mp4') or f['path'].endswith('webm'):
res.append((f.get('fullname', 'video'), 'https://2ch.hk/%s/res/%s.html#%s' % (board, num, post['num']), urljoin('https://2ch.hk/',f['path'])))
return res
async def random_webm(board='b'):
webms = []
threads = await webm_threads(board)
for num in threads:
res = await files(board, num)
webms.extend(res)
return webms and random.choice(webms) or None
async def random_fap_webm(fg=False):
threads = await fap_threads(fg)
webms = []
while threads and not webms:
board, num = threads.pop(random.randint(0, len(threads)-1))
webms = await files(board, num)
return webms and random.choice(webms) or None
async def all_webms(board):
webms = []
if board == 'f':
threads = await fap_threads()
else:
threads = await webm_threads(board)
threads = [(board, tr) for tr in threads]
for board, tr in threads:
res = await files(board, tr)
webms.extend(res)
return webms