-
Notifications
You must be signed in to change notification settings - Fork 0
/
dl_blog_image.async.py
191 lines (154 loc) · 7.13 KB
/
dl_blog_image.async.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
import re
import sys
from bs4 import BeautifulSoup
from aiohttp import ClientSession, ClientConnectorError, ClientTimeout
from itertools import chain
from asyncio import run, Semaphore, sleep
from datetime import datetime
from aiofiles import open
from os import path, getcwd, utime, stat, cpu_count
from tqdm.asyncio import tqdm
from concurrent.futures import as_completed, ProcessPoolExecutor, Future
PARALLEL_LIMIT = 300
blog_list = ["angerme-ss-shin", "angerme-amerika", "angerme-new", "juicejuice-official", "tsubaki-factory",
"morningmusume-10ki", "morningm-13ki", "morningmusume15ki", "morningmusume-9ki", "beyooooonds-rfro",
"beyooooonds-chicatetsu", "beyooooonds", "ocha-norma", "countrygirls", "risa-ogata", # "shimizu--saki",
"kumai-yurina-blog", "sudou-maasa-blog", "sugaya-risako-blog", "miyamotokarin-official",
"kobushi-factory", "sayumimichishige-blog", "kudo--haruka", "airisuzuki-officialblog",
"angerme-ayakawada"]
# blog_list = ["juicejuice-official"]
request_header = {
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:106.0) Gecko/20100101 Firefox/106.0'
}
def debug_print(*print_str: object, end: str = '\n'):
if False:
print(print_str, end='')
print(end)
async def run_each(name: str) -> None:
sem: Semaphore = Semaphore(PARALLEL_LIMIT)
session: ClientSession = ClientSession(trust_env=True, headers=request_header, timeout=ClientTimeout(total=10 * 60))
list_pages_count = await parse_list_pages_count(name)
print(name, list_pages_count)
url_lists = await tqdm.gather(*[parse_list_page(name, i, sem, session) for i in range(1, list_pages_count + 1)],
desc=name)
url_list = list(chain.from_iterable(url_lists))
for url in url_list:
if 'html' not in url:
print(url)
executor = ProcessPoolExecutor(max_workers=cpu_count())
futures = await tqdm.gather(*[parse_blog_post(url, sem, session, executor) for url in url_list], desc="scan blog")
images_list = list()
for future in tqdm(as_completed(futures), desc="waiting processing " + name, total=len(futures)):
images_list.append(future.result())
executor.shutdown()
image_link_package = list(chain.from_iterable(images_list))
await tqdm.gather(
*[download_image(filename, url, date, sem, session) for filename, url, date in image_link_package],
desc="downloading images")
await session.close()
async def parse_list_pages_count(blog_name: str) -> int:
async with ClientSession(trust_env=True, headers=request_header) as session:
async with session.get(f"https://ameblo.jp/{blog_name}/entrylist.html") as resp:
resp_html = await resp.text()
last_url = BeautifulSoup(resp_html, 'lxml').find('a', class_='skin-paginationEnd')['href']
return int(re.search('entrylist-(.*?).html', last_url).group(1))
async def parse_list_page(blog_name: str, order: int, sem: Semaphore, session: ClientSession) -> list[str]:
async with sem:
async with session.get(f"https://ameblo.jp/{blog_name}/entrylist-{order}.html") as resp:
resp_html = await resp.text()
debug_print(blog_name, order)
archive_body = BeautifulSoup(resp_html, 'lxml').find('ul', class_='skin-archiveList')
blog_boxes = archive_body.find_all('li', class_='skin-borderQuiet')
page_url_list: list[str] = list()
for blog_box in blog_boxes:
title = blog_box.find('h2', {'data-uranus-component': 'entryItemTitle'})
url = "https://ameblo.jp" + title.find('a')['href']
if "secret.ameba.jp" in url:
continue
debug_print(title.text, url)
page_url_list.append(url)
return page_url_list
def parse_image(html: str, url: str) -> list:
blog_account = url.split('/')[-2]
theme = theme_curator(grep_theme(html), blog_account)
date = datetime.fromisoformat(grep_modified_time(html))
blog_entry = url.split('/')[-1].split('.')[0].removeprefix("entry-")
parse = BeautifulSoup(html, 'lxml')
debug_print(theme + " " * (8 - len(theme)), end='')
debug_print(date.date(), end='\t')
debug_print(parse.find('title').text)
entry_body = parse.find('div', {'data-uranus-component': 'entryBody'})
for span in entry_body.find_all('span'):
span.decompose()
for emoji in entry_body.find_all('img', class_='emoji'):
emoji.decompose()
image_divs = entry_body.find_all('img', class_='PhotoSwipeImage')
return_list = list()
for div in image_divs:
return_list.append((
'='.join([theme, blog_account, blog_entry]) + '-' + div["data-image-order"] + '.jpg',
str(div["src"]).split('?')[0],
date
))
return return_list
async def parse_blog_post(url: str, sem: Semaphore, session: ClientSession, executor: ProcessPoolExecutor) -> Future:
# -> list[tuple[str, str, datetime]]:
while True:
async with sem:
try:
async with session.get(url) as resp:
resp_html = await resp.text()
await sleep(1.0)
break
except ClientConnectorError as e:
await sleep(5.0)
print(e, file=sys.stderr)
return executor.submit(parse_image, resp_html, url)
async def download_image(filename: str, url: str, date: datetime, sem: Semaphore, session: ClientSession) -> None:
filepath = path.join(getcwd(), "images", filename)
if path.isfile(filepath):
# print(f"file already downloaded.: {filename}")
return
async with sem:
# print("download: ", url)
async with session.get(url) as resp:
if resp.content_type != "image/jpeg":
return
async with open(file=filepath, mode="wb") as f:
await f.write(await resp.read())
utime(path=filepath, times=(stat(path=filepath).st_atime, date.timestamp()))
theme_regex = re.compile('"theme_name":"(.*?)"')
modified_time_regex = re.compile('"dateModified":"(.*?)"')
def grep_theme(html: str) -> str:
return str(theme_regex.search(html).group(1))
def grep_modified_time(html: str) -> str:
return str(modified_time_regex.search(html).group(1))
def theme_curator(theme: str, blog_id: str) -> str:
if theme == "":
theme = 'None'
elif 'risa-ogata' == blog_id:
theme = '小片リサ'
elif 'shimizu--saki' == blog_id:
theme = "清水佐紀"
elif 'kumai-yurina-blog' == blog_id:
theme = "熊井友理奈"
elif 'sudou-maasa-blog' == blog_id:
theme = "須藤茉麻"
elif 'sugaya-risako-blog' == blog_id:
theme = "菅谷梨沙子"
elif 'miyamotokarin-official' == blog_id:
theme = "宮本佳林"
elif 'sayumimichishige-blog' == blog_id:
theme = "道重さゆみ"
elif 'kudo--haruka' == blog_id:
theme = "工藤遥"
elif 'airisuzuki-officialblog' == blog_id:
theme = "鈴木愛理"
elif 'angerme-ayakawada' == blog_id:
theme = "和田彩花"
elif '梁川 奈々美' == theme:
theme = '梁川奈々美'
return theme
if __name__ == '__main__':
for blog in blog_list:
run(run_each(blog))