Skip to content

Instantly share code, notes, and snippets.

@aqzlpm11
Created July 17, 2022 03:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aqzlpm11/9455ff1d3c33734a573a04185745a507 to your computer and use it in GitHub Desktop.
Save aqzlpm11/9455ff1d3c33734a573a04185745a507 to your computer and use it in GitHub Desktop.
byr最新免费资源rss订阅工具
"""
byr最新免费资源rss订阅工具
(配合uTorrent的自动下载rss订阅功能,可刷上传流量)
"""
import requests
import re
from lxml import etree
import datetime
import PyRSS2Gen
from fastapi import FastAPI, Response
# COOKIE 从登陆后的浏览器里获取,PASSKEY是你自己的
COOKIE = 'YOUR_COOKIE'
PASSKEY = 'YOUR_PASSKEY'
app = FastAPI()
@app.get('/')
def index():
infos = fetch_free_list()
rss = generate_rss(infos)
return Response(rss)
CONTINUE_ERROR_N = 0
def fetch_free_list():
"""
获取免费列表的第一页
"""
global CONTINUE_ERROR_N
if CONTINUE_ERROR_N > 5:
raise RuntimeError("CONTINUE_ERROR_N > 5")
url = 'https://byr.pt/torrents.php?spstate=2'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36 Edg/100.0.1185.44',
'Cookie': COOKIE
}
response = requests.get(url, headers=headers)
selector = etree.HTML(response.text)
trs = selector.xpath('//table[@class="torrents"]/form//tr')
if len(trs) == 0:
CONTINUE_ERROR_N += 1
raise RuntimeError("Fetch fail. Do you set the right cookie?")
CONTINUE_ERROR_N = 0
infos = []
for tr in trs:
try:
title = tr.xpath('td[2]/table/tr/td[1]/a/@title')[0]
url = tr.xpath('td[2]/table/tr/td[1]/a/@href')[0]
is_sticy = len(tr.xpath('td[2]/table/tr/td[1]/img[@class="sticky"]')) > 0
torrent_id = re.findall('id=(\d+)', url)[0]
torrent_url = f'https://byr.pt/download.php?id={torrent_id}'
pub_time = tr.xpath('td[4]/span/@title')[0]
size = tr.xpath('td[5]/text()')
infos.append({
'title': title,
'torrent_url': torrent_url,
'detail_url': url,
'pub_time': pub_time,
'size': size,
'is_sticy': is_sticy
})
except IndexError:
pass
return infos
def generate_rss(infos, size_limit_in_GB=200, pub_time_limit_in_hour=24, sticy_ignore_pub_time=True):
"""
生成uTorrent可用的rss。
size_limit_in_GB: 大小限制(小于),单位GB。
pub_time_limit_in_hour: 发布时间限制(距离当前时间小于),单位hour。
"""
items = []
for info in infos:
# -------- Size limit ---------
if info['size'][1] in ['TB', 'PB']:
continue
if info['size'][1] == 'GB' and float(info['size'][0]) > size_limit_in_GB:
continue
# -------- Publish recently ---------
if info['is_sticy'] and sticy_ignore_pub_time:
pass
else:
pub_time = datetime.datetime.strptime(info['pub_time'], '%Y-%m-%d %H:%M:%S')
now = datetime.datetime.now()
if now - pub_time > datetime.timedelta(hours=pub_time_limit_in_hour):
continue
items.append(PyRSS2Gen.RSSItem(
title = info['title'],
link = info['detail_url'],
enclosure=PyRSS2Gen.Enclosure(info['torrent_url'] + f'&passkey={PASSKEY}', 1, 'application/x-bittorrent')
))
rss = PyRSS2Gen.RSS2(
title = "BYR Latest Free",
link = '',
description = '',
lastBuildDate = datetime.datetime.now(),
items = items
)
return rss.to_xml()
if __name__ == '__main__':
import uvicorn
uvicorn.run(
app=app,
host='127.0.0.1',
port=8822,
workers=1
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment