rss.py 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #!/usr/bin/env python3
  2. #
  3. # Copyright (C) 2016 James Elliott
  4. # Copyright (C) 2016-2018 Plailect
  5. # Copyright (C) 2022-2023 Nintendo Homebrew
  6. #
  7. # SPDX-License-Identifier: MIT
  8. #
  9. # Python Script for generating an rss.xml for the Guide. Requires bencodepy from pypy.
  10. import os
  11. import hashlib
  12. import urllib.parse
  13. import datetime
  14. import bencodepy
  15. dir = os.path.join(os.getcwd(), "torrents")
  16. rss = os.path.join(os.getcwd(), "rss.xml")
  17. items = []
  18. with open(rss, "w") as xml:
  19. xml.write("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n")
  20. xml.write("<rss version=\"2.0\">\n")
  21. xml.write("\t<channel>\n")
  22. xml.write("\t\t<title>Plailect Guide Feed</title>\n")
  23. xml.write("\t\t<lastBuildDate>{0}</lastBuildDate>\n".format(datetime.datetime.utcnow().strftime("%a, %d %b %Y %X +0000")))
  24. xml.write("\t\t<link>https://github.com/Plailect/Guide_3DS/</link>\n")
  25. for filename in os.listdir(dir):
  26. if filename.endswith(".torrent"):
  27. filepath = os.path.join(dir, filename)
  28. with open(filepath, "rb") as a:
  29. raw = a.read()
  30. tor = bencodepy.decode(raw)
  31. trackers = []
  32. infohash = hashlib.sha1(bencodepy.encode(tor[b"info"])).hexdigest().upper()
  33. magp = {"xt": "urn:btih:{0}".format(infohash), "dn": tor[b"info"][b"name"], "xl": tor[b"info"][b"length"]}
  34. magstr = urllib.parse.urlencode(magp)
  35. if b'announce-list' in tor:
  36. for anncl in tor[b'announce-list']:
  37. if isinstance(anncl, list):
  38. for annc in anncl:
  39. trackers.append(annc.decode("utf-8"))
  40. else:
  41. trackers.append(anncl.decode("utf-8"))
  42. length = tor[b"info"][b"length"]
  43. name = tor[b"info"][b"name"].decode("utf-8")
  44. ts = tor[b"creation date"]
  45. items.append({"name": name, "infohash": infohash, "length": length, "ts": ts, "trackers": trackers})
  46. items = sorted(items, key=lambda d: d['ts'], reverse=True)
  47. for i in items:
  48. pubdate = datetime.datetime.utcfromtimestamp(int(i["ts"]))
  49. xml.write("\t\t<item>\n")
  50. xml.write("\t\t\t<title>{0}</title>\n".format(i["name"]))
  51. xml.write("\t\t\t<description>{0}</description>\n".format(i["name"]))
  52. xml.write("\t\t\t<guid>{0}</guid>\n".format(i["infohash"]))
  53. xml.write("\t\t\t<link>magnet:?xt=urn:btih:{0}</link>\n".format(i["infohash"]))
  54. xml.write("\t\t\t<pubDate>{0}</pubDate>\n".format(pubdate.strftime("%a, %d %b %Y %X +0000")))
  55. xml.write("\t\t\t<contentLength>{0}</contentLength>\n".format(i["length"]))
  56. xml.write("\t\t\t<infoHash>{0}</infoHash>\n".format(i["infohash"]))
  57. xml.write("\t\t\t<magnetURI>magnet:?xt=urn:btih:{0}</magnetURI>\n".format(i["infohash"]))
  58. #xml.write("\t\t\t<fileName>{0}</fileName><fileName>\n".format(name))
  59. xml.write("\t\t\t<enclosure url=\"magnet:?xt=urn:btih:{0}\" type=\"application/x-bittorrent\" />\n".format(i["infohash"]))
  60. if i["trackers"]:
  61. xml.write("\t\t\t<trackers>\n")
  62. xml.write("\t\t\t\t<group order=\"random\">\n")
  63. for tracker in i["trackers"]:
  64. xml.write("\t\t\t\t\t<tracker>\n")
  65. xml.write("\t\t\t\t\t\t{0}\n".format(tracker))
  66. xml.write("\t\t\t\t\t</tracker>\n")
  67. xml.write("\t\t\t\t</group>\n")
  68. xml.write("\t\t\t</trackers>\n")
  69. xml.write("\t\t</item>\n")
  70. xml.write("\t</channel>\n")
  71. xml.write("</rss>")