Torrents commited on
Commit
826d2e0
·
verified ·
1 Parent(s): 1f9f627

Create main.py

Browse files
Files changed (1) hide show
  1. main.py +256 -0
main.py ADDED
@@ -0,0 +1,256 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, request, jsonify
2
+ from bs4 import BeautifulSoup
3
+ import requests
4
+ import re
5
+ import webbrowser
6
+
7
+ app = Flask(__name__)
8
+
9
+ html_content = """
10
+ <!DOCTYPE html>
11
+ <html lang="en">
12
+ <head>
13
+ <meta charset="UTF-8">
14
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
15
+ <title>Torrent</title>
16
+ <style>
17
+ body {
18
+ font-family: Arial, sans-serif;
19
+ margin: 0;
20
+ padding: 0;
21
+ background-color: #171a21;
22
+ color: #fff;
23
+ }
24
+ #container {
25
+ max-width: 800px;
26
+ margin: 20px auto;
27
+ padding: 20px;
28
+ background-color: #2c2f35;
29
+ border-radius: 8px;
30
+ }
31
+ h1 {
32
+ text-align: center;
33
+ color: #66c0f4;
34
+ }
35
+ #search-container {
36
+ margin-bottom: 20px;
37
+ text-align: center;
38
+ }
39
+ #search-input, #category-select {
40
+ padding: 10px;
41
+ border: none;
42
+ border-radius: 4px;
43
+ margin: 5px;
44
+ }
45
+ #search-input {
46
+ width: 50%;
47
+ }
48
+ #category-select {
49
+ width: 20%;
50
+ }
51
+ #search-button {
52
+ padding: 10px 20px;
53
+ background-color: #4b5263;
54
+ color: #fff;
55
+ border: none;
56
+ border-radius: 4px;
57
+ cursor: pointer;
58
+ }
59
+ #game-list {
60
+ list-style-type: none;
61
+ padding: 0;
62
+ }
63
+ .game-item {
64
+ margin-bottom: 10px;
65
+ padding: 10px;
66
+ background-color: #3e4251;
67
+ border-radius: 4px;
68
+ }
69
+ .game-name {
70
+ font-size: 18px;
71
+ color: #66c0f4;
72
+ }
73
+ .game-details {
74
+ font-size: 14px;
75
+ }
76
+ .download-button {
77
+ padding: 8px 16px;
78
+ background-color: #66c0f4;
79
+ color: #fff;
80
+ border: none;
81
+ border-radius: 4px;
82
+ cursor: pointer;
83
+ }
84
+ </style>
85
+ </head>
86
+ <body>
87
+ <div id="container">
88
+ <h1>Torrent</h1>
89
+ <div id="search-container">
90
+ <input type="text" id="search-input" placeholder="Enter keyword">
91
+ <select id="category-select">
92
+ <option value="0">Все</option>
93
+ <option value="movies">Фильмы</option>
94
+ <option value="series">Сериалы</option>
95
+ <option value="6">Телевизор</option>
96
+ <option value="7">Мультипликация</option>
97
+ <option value="10">Аниме</option>
98
+ <option value="2">Музыка</option>
99
+ <option value="8">Игры</option>
100
+ <option value="9">Софт</option>
101
+ <option value="13">Спорт и здоровье</option>
102
+ <option value="15">Юмор</option>
103
+ <option value="14">Хозяйство и быт</option>
104
+ <option value="11">Книги</option>
105
+ <option value="3">Другое</option>
106
+ </select>
107
+ <button id="search-button">Search</button>
108
+ </div>
109
+ <ul id="game-list"></ul>
110
+ </div>
111
+
112
+ <script>
113
+ document.getElementById("search-button").addEventListener("click", function() {
114
+ searchGames();
115
+ });
116
+
117
+ document.getElementById("search-input").addEventListener("keypress", function(event) {
118
+ if (event.key === "Enter") {
119
+ searchGames();
120
+ }
121
+ });
122
+
123
+ function searchGames() {
124
+ var query = document.getElementById("search-input").value.trim();
125
+ var category = document.getElementById("category-select").value;
126
+ if (query !== "") {
127
+ var url = "/search/" + category + "/" + query;
128
+ fetch(url)
129
+ .then(response => response.json())
130
+ .then(data => {
131
+ var gameList = document.getElementById("game-list");
132
+ gameList.innerHTML = "";
133
+ data.forEach(game => {
134
+ var listItem = document.createElement("li");
135
+ listItem.className = "game-item";
136
+ var nameElem = document.createElement("div");
137
+ nameElem.className = "game-name";
138
+ nameElem.textContent = game.name;
139
+ listItem.appendChild(nameElem);
140
+ if (game.size) {
141
+ var detailsElem = document.createElement("div");
142
+ detailsElem.className = "game-details";
143
+ detailsElem.textContent = "Size: " + game.size + ", Seeders: " + game.seeders + ", Leechers: " + game.leechers;
144
+ listItem.appendChild(detailsElem);
145
+ }
146
+ var downloadButton = document.createElement("button");
147
+ downloadButton.className = "download-button";
148
+ downloadButton.textContent = "Download";
149
+ downloadButton.addEventListener("click", function() {
150
+ window.open(game.download_link);
151
+ });
152
+ listItem.appendChild(downloadButton);
153
+ gameList.appendChild(listItem);
154
+ });
155
+ })
156
+ .catch(error => console.log(error));
157
+ }
158
+ }
159
+ </script>
160
+ </body>
161
+ </html>
162
+ """
163
+
164
+ def get_game_name(html):
165
+ soup = BeautifulSoup(html, 'html.parser')
166
+ anchor = soup.find('a', {'href': re.compile(r'\/torrent\/\d+\/')})
167
+ if anchor:
168
+ return anchor.text.strip()
169
+ return None
170
+
171
+ def get_game_size(html):
172
+ soup = BeautifulSoup(html, 'html.parser')
173
+ size_td = soup.find("td", align="right", text=re.compile(r'\d+\.\d+\s*(GB|MB)'))
174
+ if size_td:
175
+ return size_td.text.strip()
176
+ return None
177
+
178
+ def search_games(category, query):
179
+ if not query:
180
+ return []
181
+
182
+ if category == "all":
183
+ url = f"https://rutor.info/search/0/0/000/0/{query}/"
184
+ elif category == "movies":
185
+ urls = [
186
+ f"https://rutor.info/search/0/1/000/0/{query}/", # Зарубежные фильмы
187
+ f"https://rutor.info/search/0/5/000/0/{query}/", # Наши фильмы
188
+ f"https://rutor.info/search/0/12/000/0/{query}/" # Научно-популярные фильмы
189
+ ]
190
+ games = []
191
+ for url in urls:
192
+ response = requests.get(url)
193
+ if response.status_code == 200:
194
+ soup = BeautifulSoup(response.text, "html.parser")
195
+ torrents = soup.find_all("tr", class_="tum")
196
+ for torrent in torrents:
197
+ name = get_game_name(str(torrent))
198
+ if name:
199
+ size = get_game_size(str(torrent))
200
+ seeders = torrent.find("span", class_="green").text.strip()
201
+ leechers = torrent.find("span", class_="red").text.strip()
202
+ download_link = torrent.find("a", class_="downgif")["href"]
203
+ game_info = {'name': name, 'size': size, 'seeders': seeders, 'leechers': leechers, 'download_link': download_link}
204
+ games.append(game_info)
205
+ return games
206
+ elif category == "series":
207
+ urls = [
208
+ f"https://rutor.info/search/0/4/000/0/{query}/", # Зарубежные сериалы
209
+ f"https://rutor.info/search/0/16/000/0/{query}/" # Наши сериалы
210
+ ]
211
+ games = []
212
+ for url in urls:
213
+ response = requests.get(url)
214
+ if response.status_code == 200:
215
+ soup = BeautifulSoup(response.text, "html.parser")
216
+ torrents = soup.find_all("tr", class_="tum")
217
+ for torrent in torrents:
218
+ name = get_game_name(str(torrent))
219
+ if name:
220
+ size = get_game_size(str(torrent))
221
+ seeders = torrent.find("span", class_="green").text.strip()
222
+ leechers = torrent.find("span", class_="red").text.strip()
223
+ download_link = torrent.find("a", class_="downgif")["href"]
224
+ game_info = {'name': name, 'size': size, 'seeders': seeders, 'leechers': leechers, 'download_link': download_link}
225
+ games.append(game_info)
226
+ return games
227
+ else:
228
+ url = f"https://rutor.info/search/0/{category}/000/0/{query}/"
229
+ response = requests.get(url)
230
+ if response.status_code == 200:
231
+ soup = BeautifulSoup(response.text, "html.parser")
232
+ torrents = soup.find_all("tr", class_="tum")
233
+ games = []
234
+ for torrent in torrents:
235
+ name = get_game_name(str(torrent))
236
+ if name:
237
+ size = get_game_size(str(torrent))
238
+ seeders = torrent.find("span", class_="green").text.strip()
239
+ leechers = torrent.find("span", class_="red").text.strip()
240
+ download_link = torrent.find("a", class_="downgif")["href"]
241
+ game_info = {'name': name, 'size': size, 'seeders': seeders, 'leechers': leechers, 'download_link': download_link}
242
+ games.append(game_info)
243
+ return games
244
+ return []
245
+
246
+ @app.route('/')
247
+ def index():
248
+ return html_content
249
+
250
+ @app.route('/search/<category>/<query>')
251
+ def search_endpoint(category, query):
252
+ games = search_games(category, query)
253
+ return jsonify(games)
254
+
255
+ if __name__ == '__main__':
256
+ app.run(debug=True)