kindlerss.py 1.7 KB

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