forked from lilei8058/jav_bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
crawler.py
398 lines (366 loc) · 14.9 KB
/
crawler.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
import datetime
import os.path
import time
from typing import List, Dict
import bs4
import requests
from mbot.openapi import mbot_api
from mbot.common.numberutils import NumberUtils
from moviebotapi.core.utils import copy_value
def str_cookies_to_dict(cookies):
dict_cookie = {}
str_cookie_list = cookies.split(';')
for cookie in str_cookie_list:
if cookie.strip(' '):
cookie_key_value = cookie.split('=')
if len(cookie_key_value) < 2:
continue
key = cookie_key_value[0]
value = cookie_key_value[1]
dict_cookie[key] = value
return dict_cookie
class CrawlerDetail:
code: str
title: str
length: str
poster: str
banner: str
tags: List
casts: List
release_date: str
def __init__(self, data: Dict):
copy_value(data, self)
class JavLibrary:
host: str = 'https://www.javlibrary.com'
top20_url: str = f'{host}/cn/vl_mostwanted.php?page=1'
cookie: str
ua: str
proxies: dict
cookie_dict: dict
headers: dict
poster_folder: str = f"{os.path.abspath(os.path.dirname(__file__))}/poster"
banner_folder: str = f"{os.path.abspath(os.path.dirname(__file__))}/banner"
poster_folder_name: str = '/poster'
banner_folder_name: str = '/banner'
def __init__(self, cookie: str, ua: str, proxies: dict = None):
self.cookie = cookie
self.ua = ua
self.proxies = proxies
self.cookie_dict = str_cookies_to_dict(cookie)
self.headers = {'cookie': cookie, 'Referer': self.host}
if ua:
self.headers['User-Agent'] = ua
def crawling_top20(self):
av_list = []
res = requests.get(url=self.top20_url, proxies=self.proxies, cookies=self.cookie_dict, headers=self.headers)
soup = bs4.BeautifulSoup(res.text, 'html.parser')
videos = soup.select('div.video>a')
for video in videos:
code = video.select('a div.id')[0].text
img = video.find('img').get('src')
overview = video.select('a div.title')[0].text
av = {
'code': code,
'img': img,
'overview': overview
}
av_list.append(av)
return av_list
def crawling_detail(self, code, retry_time: int = 1):
if retry_time > 3:
return
url = f"{self.host}/cn/vl_searchbyid.php?keyword={code}"
res = requests.get(url=url, proxies=self.proxies, cookies=self.cookie_dict, headers=self.headers)
soup = bs4.BeautifulSoup(res.text, 'html.parser')
title = soup.select_one('h3.post-title>a')
if not title:
time.sleep(5)
self.crawling_detail(code, retry_time + 1)
return None
title = title.text
video_info = soup.select_one('div#video_info')
date = video_info.select_one('div#video_date td.text').text
length = video_info.select_one('div#video_length span.text').text
genres = video_info.select('div#video_genres span')
casts = video_info.select('div#video_cast td.text>span>span.star>a')
banner = soup.select_one('img#video_jacket_img').get('src')
poster = banner.replace('pl.jpg', 'ps.jpg')
cast_list = [item.text for item in casts]
genres_list = [item.text for item in genres]
poster_path = self.save_poster(poster, code)
banner_path = self.save_banner(banner, code)
crawler_detail = CrawlerDetail({
'code': code,
'title': title,
'length': length,
'release_date': date,
'tags': genres_list,
'casts': cast_list,
'poster': poster_path,
'banner': banner_path
})
return crawler_detail
def save_poster(self, url, code):
res = requests.get(url=f"https:{url}", proxies=self.proxies, cookies=self.cookie_dict, headers=self.headers)
if not os.path.exists(self.poster_folder):
os.makedirs(self.poster_folder)
poster_path = f"{self.poster_folder}/{code}.jpg"
if os.path.exists(poster_path):
return poster_path
with open(poster_path, 'wb') as poster_img:
poster_img.write(res.content)
return poster_path
def save_banner(self, url, code):
res = requests.get(url=f"https:{url}", proxies=self.proxies, cookies=self.cookie_dict, headers=self.headers)
if not os.path.exists(self.banner_folder):
os.makedirs(self.banner_folder)
banner_path = f"{self.banner_folder}/{code}.jpg"
if os.path.exists(banner_path):
return banner_path
with open(banner_path, 'wb') as banner_img:
banner_img.write(res.content)
return banner_path
class MTeam:
site_id: str = 'mteam'
host: str = 'https://kp.m-team.cc'
cookie: str
ua: str
cookie_dict: dict
headers: dict
torrent_folder: str = '/data/jav_bot_torrents'
def __init__(self):
m_team = self.get_m_team()
self.cookie = m_team.cookie
self.ua = m_team.user_agent
self.cookie_dict = str_cookies_to_dict(self.cookie)
self.headers = {'cookie': self.cookie, 'Referer': self.host}
if self.ua:
self.headers['User-Agent'] = self.ua
if not os.path.exists(self.torrent_folder):
os.makedirs(self.torrent_folder)
def crawling_torrents(self, keyword):
url = f'{self.host}/adult.php?incldead=1&spstate=0&inclbookmarked=0&search={keyword}&search_area=0&search_mode=0'
res = requests.get(url=url, cookies=self.cookie_dict, headers=self.headers)
soup = bs4.BeautifulSoup(res.text, 'html.parser')
trs = soup.select('table.torrents > tr:has(table.torrentname)')
torrents = []
for tr in trs:
title = tr.select('a[title][href^="details.php?id="]')[0].get('title')
download_url = tr.select('a[href^="download.php?id="]')[0].get('href')
size = tr.select('td.rowfollow:nth-last-child(6)')[0].text
seeders = tr.select('td.rowfollow:nth-last-child(5)')[0].text
leechers = tr.select('td.rowfollow:nth-last-child(4)')[0].text
describe_list = tr.select('table.torrentname > tr > td.embedded')[0].contents
describe = describe_list[len(describe_list) - 1].text
torrent = {
'title': title,
'download_url': download_url,
'size': size,
'seeders': seeders,
'leechers': leechers,
'describe': describe
}
weight = self.get_weight(title, describe, int(seeders), size)
torrent['weight'] = weight
torrents.append(torrent)
return torrents
@staticmethod
def get_weight(title: str, describe: str, seeders: int, size: str):
cn_keywords = ['中字', '中文字幕', '色花堂', '字幕']
weight = 0
content = title + describe
for keyword in cn_keywords:
if content.find(keyword) > -1:
weight = weight + 5000
break
weight = weight + seeders
if seeders == 0:
weight = -1
mb_size = NumberUtils.trans_size_str_to_mb(size)
if mb_size > 10240:
weight = -1
return weight
@staticmethod
def get_best_torrent(torrents):
if torrents:
sort_list = sorted(torrents, key=lambda torrent: torrent['weight'], reverse=True)
torrent = sort_list[0]
if torrent['weight'] < 0:
return None
return torrent
return None
def download_torrent(self, code, download_url):
res = requests.get(f"{self.host}/{download_url}", cookies=self.cookie_dict)
torrent_path = f'{self.torrent_folder}/{code}.torrent'
with open(torrent_path, 'wb') as torrent:
torrent.write(res.content)
torrent.flush()
return torrent_path
def get_m_team(self):
site_list = mbot_api.site.list()
m_team_list = list(
filter(lambda x: x.site_id == self.site_id, site_list))
if len(m_team_list) > 0:
m_team = m_team_list[0]
return m_team
return None
class JavBus:
hosts: List = ['https://www.javbus.com', 'https://www.javsee.bar', 'https://www.seejav.icu',
'https://www.javsee.in']
cookie: str
ua: str
proxies: dict
cookie_dict: dict
headers: dict
poster_folder: str = f"{os.path.abspath(os.path.dirname(__file__))}/poster"
banner_folder: str = f"{os.path.abspath(os.path.dirname(__file__))}/banner"
poster_folder_name: str = '/poster'
banner_folder_name: str = '/banner'
def __init__(self, cookie: str, ua: str, proxies: dict = None):
self.cookie = cookie
self.ua = ua
self.proxies = proxies
if 'exitmad' not in self.cookie:
self.cookie = f"exitmad=all;{self.cookie}"
else:
self.cookie = self.cookie.replace('existmag=mag', 'existmag=all')
self.cookie_dict = str_cookies_to_dict(cookie)
self.headers = {'cookie': cookie, 'Referer': self.hosts[0]}
if ua:
self.headers['User-Agent'] = ua
def crawling_actor(self, star_code, start_date: datetime.datetime):
for host in self.hosts:
url = f"{host}/star/{star_code}"
try:
res = requests.get(url=url, proxies=self.proxies, headers=self.headers, cookies=self.cookie_dict)
break
except Exception as e:
continue
if not res:
return None
soup = bs4.BeautifulSoup(res.text, 'html.parser')
movie_list = soup.select('a.movie-box')
code_list = []
for item in movie_list:
date_list = item.select('date')
code_list.append({'date': date_list[1].text, 'code': date_list[0].text})
start_date_timestamp = int(start_date.strftime('%Y%m%d'))
filter_list = list(
filter(
lambda x: int(
datetime.datetime.strptime(x['date'], "%Y-%m-%d").strftime('%Y%m%d')) >= start_date_timestamp
and 'VR' not in x['code'],
code_list))
finally_list = []
for item in filter_list:
time.sleep(5)
teacher_list = self.get_teacher_list(item['code'])
teacher_num = None
if teacher_list:
teacher_num = len(teacher_list)
if teacher_num and teacher_num < 4:
finally_list.append(item)
return finally_list
def get_teacher_list(self, code: str):
for host in self.hosts:
url = f"{host}/{code}"
try:
res = requests.get(url=url, proxies=self.proxies, headers=self.headers, cookies=self.cookie_dict)
break
except Exception as e:
continue
if not res:
return None
soup = bs4.BeautifulSoup(res.text, 'html.parser')
actor_list = soup.select('span.genre>a')
if actor_list:
teacher_list = []
for actor in actor_list:
name = actor.text
url = actor.get('href')
code_split_list = url.split('/')
code = code_split_list[len(code_split_list) - 1]
teacher_list.append({'teacher_name': name, 'teacher_code': code})
return teacher_list
return None
def crawling_detail(self, code):
for host in self.hosts:
url = f"{host}/{code}"
try:
res = requests.get(url=url, proxies=self.proxies, headers=self.headers, cookies=self.cookie_dict)
break
except Exception as e:
continue
if not res:
return None
soup = bs4.BeautifulSoup(res.text, 'html.parser')
ps = soup.select('div.info>p')
title = soup.select_one('h3').text
release_date = ps[1].contents[1]
duration = ps[2].contents[1]
tags = soup.select('p>span.genre>label>a')
tag_list = [item.text for item in tags]
casts = soup.select('span.genre>a')
cast_list = [item.text for item in casts]
banner = soup.select_one('a.bigImage>img').get('src')
poster = banner.replace('cover', 'thumb').replace('_b', '')
poster_path = self.save_poster(poster, code)
banner_path = self.save_banner(banner, code)
crawler_detail = CrawlerDetail({
'code': code,
'title': title,
'length': duration,
'release_date': release_date,
'tags': tag_list,
'casts': cast_list,
'poster': poster_path,
'banner': banner_path
})
return crawler_detail
def crawling_by_code(self, code):
teacher_list = self.get_teacher_list(code)
if teacher_list and len(teacher_list) == 1:
teacher = teacher_list[0]
return {'teacher_code': teacher['teacher_code'], 'teacher_name': teacher['teacher_name']}
return None
def crawling_by_name(self, teacher_name):
for host in self.hosts:
url = f"{host}/searchstar/{teacher_name}"
try:
res = requests.get(url=url, proxies=self.proxies, headers=self.headers, cookies=self.cookie_dict)
break
except Exception as e:
continue
if not res:
return None
soup = bs4.BeautifulSoup(res.text, 'html.parser')
actors = soup.select('a.avatar-box')
if len(actors) > 1:
return None
if len(actors) < 1:
return None
teacher_url = actors[0].get('href')
code_split_list = teacher_url.split('/')
code = code_split_list[len(code_split_list) - 1]
teacher_name = actors[0].select('div.photo-frame>img')[0].get('title')
return {'teacher_code': code, 'teacher_name': teacher_name}
def save_poster(self, url, code):
res = requests.get(url=f"{self.hosts[0]}{url}", proxies=self.proxies, cookies=self.cookie_dict, headers=self.headers)
if not os.path.exists(self.poster_folder):
os.makedirs(self.poster_folder)
poster_path = f"{self.poster_folder}/{code}.jpg"
if os.path.exists(poster_path):
return poster_path
with open(poster_path, 'wb') as poster_img:
poster_img.write(res.content)
return poster_path
def save_banner(self, url, code):
res = requests.get(url=f"{self.hosts[0]}{url}", proxies=self.proxies, cookies=self.cookie_dict, headers=self.headers)
if not os.path.exists(self.banner_folder):
os.makedirs(self.banner_folder)
banner_path = f"{self.banner_folder}/{code}.jpg"
if os.path.exists(banner_path):
return banner_path
with open(banner_path, 'wb') as banner_img:
banner_img.write(res.content)
return banner_path