-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
154 lines (111 loc) · 3.03 KB
/
main.py
File metadata and controls
154 lines (111 loc) · 3.03 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
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
# %%
import bs4
import requests
import json
import pathlib
import subprocess as sp
import RevUtils.Logger as Rlog
import RevUtils.General as Rgen
# %%
urls = dict()
'''
{
<<<post_url>>>:
{
<<<urls>>>:[],
<<<done>>>: True
}
}
'''
def load():
global urls
urls = json.loads(pathlib.Path('urls.json.bk').read_text())
def save():
global urls
pathlib.Path('urls.json.bk').write_text(json.dumps(urls))
# %%
load()
# %%
main = 'http://panahian.ir/post/686'
main = requests.get(main).content.decode('utf8')
main = bs4.BeautifulSoup(markup=main, features='html.parser')
main = main.select('div.cnt li p a')
dw_dir = 'dw'
# %%
posts = [x["href"] for x in main]
posts = set(posts)
Rlog.info(f'{posts=}')
Rlog.info(f'{len(posts)=}')
# %%
def fix_url(url: str):
if url.startswith('//'):
return f'http:{url}'
return url
def fix_title(title: str):
rms = [
['|', '-'],
['\n', ''],
['(', ''],
[')', ''],
['\xa0', ''],
['\u200c', ''],
['*', ''],
['?', ''],
['\\', ''],
['/', ''],
['"', ''],
[':', ''],
['<', ''],
['>', ''],
]
for rm in rms:
title = title.replace(rm[0], rm[1])
return title
def download(url: str, dir: str):
aria2c = sp.Popen(['aria2c', '--file-allocation=none', '-s1', '-d', dir, url],
stdin=sp.PIPE,
stdout=sp.PIPE,
stderr=sp.PIPE)
aria2c.wait(timeout=300)
# result = aria2c.communicate()[0].decode()
return aria2c.returncode
# %%
for post in posts:
Rlog.info('*')
if not post.startswith('http'):
post = f'http://panahian.ir{post}'
Rlog.info(f'{post=}')
if post in urls:
if urls[post]['done']:
Rlog.info('post already done, skip...')
continue
else:
urls[post] = {'urls': [], 'done': False}
bs4_parsed = requests.get(post).content.decode('utf8')
bs4_parsed = bs4.BeautifulSoup(bs4_parsed, features='html.parser')
title = bs4_parsed.find(name='meta', attrs={'name': 'description'})['content']
title = fix_title(title)
Rlog.info(f'{title=}')
post_dw_dir = f'{dw_dir}/{title}'
Rgen.make_dirs_if_not_exists(post_dw_dir)
bs4_parsed = bs4_parsed.select('.DivPlayerDownload')
# print(f'{bs4_parsed=}')
for i in range(len(bs4_parsed)):
# Rlog.info(bs4_parsed[i])
bs4_parsed[i] = bs4_parsed[i].select_one('a')
if not bs4_parsed[i]:
Rlog.info('not found!')
continue
bs4_parsed[i] = bs4_parsed[i]['href']
bs4_parsed[i] = fix_url(bs4_parsed[i])
Rlog.info(bs4_parsed[i])
if bs4_parsed[i] in urls[post]['urls']:
Rlog.info('url already downloaded, skip...')
continue
download(bs4_parsed[i], post_dw_dir)
urls[post]['urls'].append(bs4_parsed[i])
save()
Rlog.info(f'{len(bs4_parsed)=}')
urls[post]['done'] = True
save()
# time.sleep(.5)