pageGeneration.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #!/usr/bin/env python3
  2. # vim: tabstop=4 shiftwidth=4 expandtab
  3. import antennaDB
  4. import argparse
  5. import datetime
  6. import signoffs
  7. import html
  8. db = antennaDB.AntennaDB()
  9. def printPage(pagetext, output=None):
  10. if output:
  11. outputfile = open(output, "w")
  12. outputfile.write(pagetext)
  13. outputfile.close()
  14. else:
  15. print(pagetext)
  16. # Update statsfile
  17. def updateStatFile(urlNr, entryNr, output=None):
  18. outputString = f"{datetime.datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%SZ')}\t{urlNr}\t{entryNr}\t%.2f"%(entryNr/urlNr)
  19. if output:
  20. outputfile = open(output, "a")
  21. outputfile.write(f"{outputString}\n")
  22. outputfile.close()
  23. else:
  24. print(outputString)
  25. # Generate index page
  26. def generateFeedPage(entries, output=None):
  27. pagetext='''# Antenna
  28. ## Receiving Transmissions From Geminispace
  29. => about.gmi What is this?
  30. => submit Send transmission!
  31. '''
  32. datestamp = "0000-00-00"
  33. for entry in entries:
  34. timestamp = datetime.datetime.utcfromtimestamp(entry.updated).strftime('%Y-%m-%d')
  35. if not datestamp == timestamp:
  36. datestamp = timestamp
  37. pagetext += "\n"
  38. pagetext += "=> " + entry.link + " " + timestamp + " " + entry.author + ": " + entry.title + "\n"
  39. pagetext += f"\n> {signoffs.getsig()}\n"
  40. pagetext += '''
  41. => log Tail the log
  42. => twtxt.gmi Antenna's twtxt page.
  43. => atom.xml Antenna's Atom feed.
  44. => filter Customize your Antenna view.
  45. => stats.tsv Check out the latest statistics in tsv format.
  46. '''
  47. printPage(pagetext, output)
  48. def generateAtomFeed(entries, output=None):
  49. pagetext=f'''<?xml version="1.0" encoding="UTF-8"?><feed xmlns="http://www.w3.org/2005/Atom">
  50. <title>Antenna</title>
  51. <id>gemini://warmedal.se/~antenna/</id>
  52. <updated>{datetime.datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%SZ')}</updated>
  53. <subtitle>Receiving transmissions from geminispace</subtitle>
  54. <link href="gemini://warmedal.se/~antenna/" rel="alternate"></link>
  55. <link href="gemini://warmedal.se/~antenna/atom.xml" rel="self"></link>
  56. '''
  57. for entry in entries:
  58. timestamp = datetime.datetime.utcfromtimestamp(entry.updated).strftime('%Y-%m-%dT%H:%M:%SZ')
  59. pagetext += f''' <entry>
  60. <id>{html.escape(entry.link)}</id>
  61. <title>{html.escape(entry.title)}</title>
  62. <updated>{timestamp}</updated>
  63. <link href="{html.escape(entry.link)}" rel="alternate"></link>
  64. <author>
  65. <name>{html.escape(entry.author)}</name>
  66. </author>
  67. </entry>
  68. '''
  69. pagetext += "</feed>"
  70. printPage(pagetext, output)
  71. def generateTwtxtPage(entries, output=None):
  72. pagetext='''# Antenna Twtxt
  73. => twtxt.txt Plain text version
  74. '''
  75. for entry in entries:
  76. pagetext += f"{entry.author} {entry.feedurl}\n> {datetime.datetime.utcfromtimestamp(entry.posted).strftime('%Y-%m-%dT%H:%M:%SZ')} {entry.twt}\n\n"
  77. pagetext += f"\n=> log Tail the log\n\n> {signoffs.getsig()}\n"
  78. printPage(pagetext, output)
  79. def generateTwtxtFile(entries, output=None):
  80. pagetext = ""
  81. for entry in entries:
  82. pagetext += f"{entry.author}\t{entry.feedurl}\t{datetime.datetime.utcfromtimestamp(entry.posted).strftime('%Y-%m-%dT%H:%M:%SZ')}\t{entry.twt}\n"
  83. printPage(pagetext, output)