-
Notifications
You must be signed in to change notification settings - Fork 0
/
kinozal_parser.py
145 lines (121 loc) · 5 KB
/
kinozal_parser.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
import requests
from bs4 import BeautifulSoup
# import sys
import bencodepy
import hashlib
import base64
# url = http://kinozal.tv/browse.php
def get_html(url):
r = requests.get(url)
return r.text # Возвращаем HTML - код страницы url
def get_all_links(html, maxsearch):
# try:
# soup = BeautifulSoup(html, 'lxml')
# except:
# print('Страница со списком найденных торрентов не загружена')
# exit(0)
# try:
# tds = soup.find('table', class_='t_peer w100p').find_all('td', class_='nam')
# except:
# print('По вашему запросу ничего не обнаружено')
# exit(0)
soup = BeautifulSoup(html, 'lxml')
# print(soup)
tds = soup.find('table', class_='t_peer w100p').find_all('td', class_='nam')
links = []
# session = requests.session() # Авторизация
# url_login = 'http://kinozal.tv/takelogin.php' #
# data = {'username': 'expk13', 'password': 'expk--'} #
# session.post(url_login, data=data).text #
i = 0
for td in tds:
a = td.find('a').get('href') # Строка с ссылками
desc = td.find('a').text # Описание торрента
# link_torrent = get_page(session.post('http://kinozal.tv' + a).text) # Получаем ссылку для скачивания
link_torrent = 'http://dl.kinozal.tv/download.php?id=' + a.split('=')[1]
link = f'http://kinozal.tv{a} {link_torrent} {desc}'
links.append([a.split('=')[1], desc])
# print (len(links),'=',links)
i += 1
if i >= maxsearch:
# print (i)
break
# if get_torrent_file(link_torrent, session) == 11: # Скачать торрент файл во временный с именем 1.torrent
# print(link)
# link = link + ' ' + make_magnet_from_file('1.torrent')
# print(link)
# links.append(link)
# i += 1
# print ('i=',i)
# if i == 5:
# print (i)
# break
# else:
# i += 1
# link = link + ' ' + 'Не удалось скачать torrent файл'
# print(link)
# links.append(link)
# print('Не удалось скачать torrent файл')
# if i == 5:
# print (i)
# break
# else:
# print ('Найдено 5 записей ',i)
return links
# def get_page(html):
#
#
# # print (session.post(url).text)
# # print(url)
# try:
# soup = BeautifulSoup(html, 'lxml')
# except:
# print('Страница с торрентом не загружена')
# exit(0)
# try:
# tds = soup.find('table', class_='w100p').find('a').get('href')
# except:
# print('Таблица со списком не обнаружена')
# exit(0)
# return tds
# Функция для скачивания торрент файла во временный с именем temp.torrent
def get_torrent_file(link, session): # Скачать торрент файл
file_torrent = session.get(link)
if file_torrent.status_code != 200:
return 1
with open('temp.torrent', 'wb') as f: #
f.write(file_torrent.content) #
f.close()
return 0
def autenticitate(): # Авторизация на сайте
session = requests.session()
url = 'http://kinozal.tv/takelogin.php'
data = {'username': 'shamen', 'password': '221979'}
# print (session.post(url, data=data).text)
def make_magnet_from_file(file): # Создание magnet ссылок из torrent файла
try:
metadata = bencodepy.decode_from_file(file)
except:
# print('Вы использовали доступное Вам количество торрент-файлов в сутки')
return 'Вы использовали доступное Вам количество торрент-файлов в сутки'
subj = metadata[b'info']
hashcontents = bencodepy.encode(subj)
digest = hashlib.sha1(hashcontents).digest()
b32hash = base64.b32encode(digest).decode()
return 'magnet:?' \
+ 'xt=urn:btih:' + b32hash \
+ '&dn=' + metadata[b'info'][b'name'].decode() \
+ '&tr=' + metadata[b'announce'].decode() \
# + '&xl=' + str(metadata[b'info'])
def main():
# Для перебора страниц
# url = 'http://kinozal.tv/browse.php?s=lost' # Ссылка с поиском
# url = 'http://kinozal.tv/browse.php'
url = 'https://kinozal.tv/details.php?id=1987352'
all_link = get_all_links(get_html(url), 50)
# print (all_link)
for i in all_link:
print('id:', i[0], 'Описание:', i[1])
# autenticitate() # Авторизация
if __name__ == '__main__':
main()