|
2 | 2 | import pickle |
3 | 3 | import random |
4 | 4 | import time |
| 5 | +import re |
| 6 | +import json |
5 | 7 | from functools import lru_cache |
6 | 8 |
|
7 | 9 | from lxml import etree |
| 10 | +from urllib.parse import urlsplit |
8 | 11 |
|
9 | 12 | from app.helper import ChromeHelper |
10 | 13 | from app.utils import ExceptionUtils, StringUtils, RequestUtils |
@@ -115,66 +118,104 @@ def check_torrent_attr(self, torrent_url, cookie, ua=None, proxy=False): |
115 | 118 | :return: 种子属性,包含FREE 2XFREE HR PEER_COUNT等属性 |
116 | 119 | """ |
117 | 120 | ret_attr = { |
118 | | - "free": False, |
119 | | - "2xfree": False, |
120 | | - "hr": False, |
121 | | - "peer_count": 0 |
122 | | - } |
123 | | - if not torrent_url: |
124 | | - return ret_attr |
125 | | - xpath_strs = self.get_grap_conf(torrent_url) |
126 | | - if not xpath_strs: |
127 | | - return ret_attr |
128 | | - html_text = self.__get_site_page_html(url=torrent_url, |
129 | | - cookie=cookie, |
130 | | - ua=ua, |
131 | | - render=xpath_strs.get('RENDER'), |
132 | | - proxy=proxy) |
133 | | - if not html_text: |
134 | | - return ret_attr |
135 | | - try: |
136 | | - html = etree.HTML(html_text) |
137 | | - # 检测2XFREE |
138 | | - for xpath_str in xpath_strs.get("2XFREE"): |
139 | | - if html.xpath(xpath_str): |
140 | | - ret_attr["free"] = True |
141 | | - ret_attr["2xfree"] = True |
142 | | - # 检测FREE |
143 | | - for xpath_str in xpath_strs.get("FREE"): |
144 | | - if html.xpath(xpath_str): |
145 | | - ret_attr["free"] = True |
146 | | - # 检测HR |
147 | | - for xpath_str in xpath_strs.get("HR"): |
148 | | - if html.xpath(xpath_str): |
149 | | - ret_attr["hr"] = True |
150 | | - # 检测PEER_COUNT当前做种人数 |
151 | | - for xpath_str in xpath_strs.get("PEER_COUNT"): |
152 | | - peer_count_dom = html.xpath(xpath_str) |
153 | | - if peer_count_dom: |
154 | | - peer_count_str = ''.join(peer_count_dom[0].itertext()) |
155 | | - peer_count_digit_str = "" |
156 | | - for m in peer_count_str: |
157 | | - if m.isdigit(): |
158 | | - peer_count_digit_str = peer_count_digit_str + m |
159 | | - if m == " ": |
160 | | - break |
161 | | - ret_attr["peer_count"] = int(peer_count_digit_str) if len(peer_count_digit_str) > 0 else 0 |
162 | | - except Exception as err: |
163 | | - ExceptionUtils.exception_traceback(err) |
| 121 | + "free": False, |
| 122 | + "2xfree": False, |
| 123 | + "hr": False, |
| 124 | + "peer_count": 0 |
| 125 | + } |
| 126 | + if 'm-team' in torrent_url: |
| 127 | + split_url = urlsplit(torrent_url) |
| 128 | + base_url = f"{split_url.scheme}://{split_url.netloc}" |
| 129 | + detail_url = f"{base_url}/api/torrent/detail" |
| 130 | + res = re.findall(r'\d+', torrent_url) |
| 131 | + param = res[0] |
| 132 | + json_text = self.__get_site_page_html(url=detail_url, |
| 133 | + cookie=cookie, |
| 134 | + ua=ua, |
| 135 | + proxy=proxy, |
| 136 | + param=param) |
| 137 | + json_data = json.loads(json_text) |
| 138 | + if json_data['message'] != "SUCCESS": |
| 139 | + return ret_attr |
| 140 | + discount = json_data['data']['status']['discount'] |
| 141 | + seeders = json_data['data']['status']['seeders'] |
| 142 | + if discount == 'FREE': |
| 143 | + ret_attr["free"] = True |
| 144 | + ret_attr['peer_count'] = int(seeders) |
| 145 | + |
| 146 | + else: |
| 147 | + if not torrent_url: |
| 148 | + return ret_attr |
| 149 | + xpath_strs = self.get_grap_conf(torrent_url) |
| 150 | + if not xpath_strs: |
| 151 | + return ret_attr |
| 152 | + html_text = self.__get_site_page_html(url=torrent_url, |
| 153 | + cookie=cookie, |
| 154 | + ua=ua, |
| 155 | + render=xpath_strs.get('RENDER'), |
| 156 | + proxy=proxy) |
| 157 | + if not html_text: |
| 158 | + return ret_attr |
| 159 | + try: |
| 160 | + html = etree.HTML(html_text) |
| 161 | + # 检测2XFREE |
| 162 | + for xpath_str in xpath_strs.get("2XFREE"): |
| 163 | + if html.xpath(xpath_str): |
| 164 | + ret_attr["free"] = True |
| 165 | + ret_attr["2xfree"] = True |
| 166 | + # 检测FREE |
| 167 | + for xpath_str in xpath_strs.get("FREE"): |
| 168 | + if html.xpath(xpath_str): |
| 169 | + ret_attr["free"] = True |
| 170 | + # 检测HR |
| 171 | + for xpath_str in xpath_strs.get("HR"): |
| 172 | + if html.xpath(xpath_str): |
| 173 | + ret_attr["hr"] = True |
| 174 | + # 检测PEER_COUNT当前做种人数 |
| 175 | + for xpath_str in xpath_strs.get("PEER_COUNT"): |
| 176 | + peer_count_dom = html.xpath(xpath_str) |
| 177 | + if peer_count_dom: |
| 178 | + peer_count_str = ''.join(peer_count_dom[0].itertext()) |
| 179 | + peer_count_digit_str = "" |
| 180 | + for m in peer_count_str: |
| 181 | + if m.isdigit(): |
| 182 | + peer_count_digit_str = peer_count_digit_str + m |
| 183 | + if m == " ": |
| 184 | + break |
| 185 | + ret_attr["peer_count"] = int(peer_count_digit_str) if len(peer_count_digit_str) > 0 else 0 |
| 186 | + except Exception as err: |
| 187 | + ExceptionUtils.exception_traceback(err) |
164 | 188 | # 随机休眼后再返回 |
165 | 189 | time.sleep(round(random.uniform(1, 5), 1)) |
166 | 190 | return ret_attr |
167 | 191 |
|
168 | 192 | @staticmethod |
169 | 193 | @lru_cache(maxsize=128) |
170 | | - def __get_site_page_html(url, cookie, ua, render=False, proxy=False): |
| 194 | + def __get_site_page_html(url, cookie, ua, render=False, proxy=False, param=None): |
171 | 195 | chrome = ChromeHelper(headless=True) |
172 | 196 | if render and chrome.get_status(): |
173 | 197 | # 开渲染 |
174 | 198 | if chrome.visit(url=url, cookie=cookie, ua=ua, proxy=proxy): |
175 | 199 | # 等待页面加载完成 |
176 | 200 | time.sleep(10) |
177 | 201 | return chrome.get_html() |
| 202 | + elif 'm-team' in url: |
| 203 | + param = {'id': param} |
| 204 | + headers = {} |
| 205 | + headers.update({ |
| 206 | + "User-Agent": f"{ua}" |
| 207 | + }) |
| 208 | + headers.update({ |
| 209 | + "contentType": 'application/json;charset=UTF-8' |
| 210 | + }) |
| 211 | + res = RequestUtils( |
| 212 | + cookies=cookie, |
| 213 | + headers=headers, |
| 214 | + proxies=Config().get_proxies() if proxy else None |
| 215 | + ).post_res(url=url, data=param) |
| 216 | + if res and res.status_code == 200: |
| 217 | + res.encoding = res.apparent_encoding |
| 218 | + return res.text |
178 | 219 | else: |
179 | 220 | res = RequestUtils( |
180 | 221 | cookies=cookie, |
|
0 commit comments