kindlerss.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. from lxml import etree
  2. import urllib.request
  3. import os
  4. from argparse import ArgumentParser
  5. parser = ArgumentParser()
  6. parser.add_argument("url", type=str)
  7. parser.add_argument("output", type=str)
  8. args = parser.parse_args()
  9. HEADER = """<!DOCTYPE HTML>
  10. <html>
  11. <head>
  12. <meta charset="utf-8">
  13. <title>RSS Feed</title>
  14. <style>
  15. * { font-family: sans-serif; line-height: 1.6em;}
  16. a:link { color: black }
  17. a:visited {color: #666 }
  18. </style>
  19. </head>
  20. <body>"""
  21. URL = args.url
  22. DIR = args.output
  23. if not os.path.exists(DIR):
  24. os.makedirs(DIR)
  25. os.makedirs(os.path.join(DIR, "articles"), exist_ok=True)
  26. FILE = open(os.path.join(DIR, "index.html"), "w")
  27. print(HEADER, file=FILE)
  28. print("<h1>RSS Feed</h1>", file=FILE)
  29. print("<ul>", file=FILE)
  30. feed = urllib.request.urlopen("https://finetuned.nl/freshrss/p/i/?a=rss&rid=610659e817a51&hours=168")
  31. feed = etree.parse(feed)
  32. feed = feed.getroot()
  33. channel = feed.find("channel")
  34. for item in channel.iterfind("item"):
  35. title = item.find("title").text
  36. guid = item.find("guid").text
  37. contents = item.find("description").text
  38. print(f"<li><a href=\"articles/{guid}.html\">{title}</a></li>", file=FILE)
  39. with open(os.path.join(DIR, "articles", f"{guid}.html"), "w") as file:
  40. print(HEADER, file=file)
  41. print(f"<h1>{title}</h1>", file=file)
  42. print(contents, file=file)
  43. print("</body>", file=file)
  44. print("</html>", file=file)
  45. print("</ul>", file=FILE)
  46. print("</body>", file=FILE)
  47. print("</html>", file=FILE)
  48. FILE.close()