forked from hacks-guide/Guide_3DS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rss.py
70 lines (59 loc) · 3.15 KB
/
rss.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
#!/usr/bin/env python3
"""Python Script for generating an rss.xml for the Guide. Requires bencodepy from pypy."""
import os
import hashlib
import urllib.parse
import datetime
import bencodepy
dir = os.path.join(os.getcwd(), "torrents")
rss = os.path.join(os.getcwd(), "rss.xml")
with open(rss, "w") as xml:
xml.write("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n")
xml.write("<rss version=\"2.0\">\n")
xml.write("\t<channel>\n")
xml.write("\t\t<title>Plailect Guide Feed</title>\n")
xml.write("\t\t<lastBuildDate>{0}</lastBuildDate>\n".format(datetime.datetime.utcnow().strftime("%a, %d %b %Y %X +0000")))
xml.write("\t\t<link>https://github.com/Plailect/Guide_3DS/</link>\n")
for filename in os.listdir(dir):
if filename.endswith(".torrent"):
filepath = os.path.join(dir, filename)
with open(filepath, "rb") as a:
raw = a.read()
tor = bencodepy.decode(raw)
trackers = []
infohash = hashlib.sha1(bencodepy.encode(tor[b"info"])).hexdigest().upper()
magp = {"xt": "urn:btih:{0}".format(infohash), "dn": tor[b"info"][b"name"], "xl": tor[b"info"][b"length"]}
magstr = urllib.parse.urlencode(magp)
for anncl in tor[b"announce-list"]:
if isinstance(anncl, list):
for annc in anncl:
trackers.append(annc.decode("utf-8"))
else:
trackers.append(anncl.decode("utf-8"))
length = tor[b"info"][b"length"]
name = tor[b"info"][b"name"].decode("utf-8")
ts = tor[b"creation date"]
pubdate = datetime.datetime.utcfromtimestamp(int(ts))
xml.write("\t\t<item>\n")
xml.write("\t\t\t<title>{0}</title>\n".format(name))
xml.write("\t\t\t<description>{0}</description>\n".format(name))
xml.write("\t\t\t<guid>{0}</guid>\n".format(infohash))
xml.write("\t\t\t<link>magnet:?xt=urn:btih:{0}</link>\n".format(infohash))
xml.write("\t\t\t<pubDate>{0}</pubDate>\n".format(pubdate.strftime("%a, %d %b %Y %X +0000")))
xml.write("\t\t\t<contentLength>{0}</contentLength>\n".format(length))
xml.write("\t\t\t<infoHash>{0}</infoHash>\n".format(infohash))
xml.write("\t\t\t<magnetURI>magnet:?xt=urn:btih:{0}</magnetURI>\n".format(infohash))
#xml.write("\t\t\t<fileName>{0}</fileName><fileName>\n".format(name))
xml.write("\t\t\t<enclosure url=\"magnet:?xt=urn:btih:{0}\" type=\"application/x-bittorrent\" />\n".format(infohash))
if trackers:
xml.write("\t\t\t<trackers>\n")
xml.write("\t\t\t\t<group order=\"random\">\n")
for tracker in trackers:
xml.write("\t\t\t\t\t<tracker>\n")
xml.write("\t\t\t\t\t\t{0}\n".format(tracker))
xml.write("\t\t\t\t\t</tracker>\n")
xml.write("\t\t\t\t</group>\n")
xml.write("\t\t\t</trackers>\n")
xml.write("\t\t</item>\n")
xml.write("\t</channel>\n")
xml.write("</rss>")