microblog.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. import sys, os, traceback
  2. import dateutil.parser
  3. from time import strftime, localtime
  4. # returns html-formatted string
  5. def make_buttons(btn_dict, msg_id):
  6. buttons = "<div class=\"buttons\">"
  7. fmt = "<a href=\"%s\">[%s]</a>"
  8. for key in btn_dict:
  9. url = btn_dict[key]
  10. if url[-1] == '=':
  11. # then interpret it as a query string
  12. url += str(msg_id)
  13. buttons += fmt % (url,key)
  14. buttons += "</div>"
  15. return buttons
  16. # apply div classes for use with .css
  17. def make_post(num, timestamp, conf, msg):
  18. fmt = conf["format"]
  19. if "buttons" in conf:
  20. b = make_buttons(conf["buttons"], num)
  21. else:
  22. b = ""
  23. return fmt.format(
  24. __timestamp__=timestamp, __num__=num, __msg__=msg, __btn__=b)
  25. def make_gallery(indices, w, conf=None):
  26. tag = []
  27. if indices == []:
  28. return tag
  29. template = '''
  30. <div class=\"panel\">
  31. <a href=\"%s\"><img src=\"%s\" class=\"embed\"></a>
  32. </div>
  33. '''
  34. tag.append("<div class=\"gallery\">")
  35. for index in reversed(indices):
  36. image = w.pop(index)
  37. is_path = image[0] == '.' or image[0] == '/'
  38. if conf and not is_path:
  39. thumb = "%s/%s" % (conf["path_to_thumb"], image)
  40. full = "%s/%s" % (conf["path_to_fullsize"], image)
  41. tag.append(template % (full,thumb))
  42. continue
  43. elif not conf and not is_path:
  44. msg = ("Warning: no path defined for image %s!" % image)
  45. print(msg,file=sys.stderr)
  46. else:
  47. pass
  48. tag.append(template % (image, image))
  49. tag.append("</div>")
  50. return tag
  51. # apply basic HTML formatting - only div class here is gallery
  52. from html import escape
  53. def markup(message, config):
  54. def is_image(s, image_formats):
  55. l = s.rsplit('.', maxsplit=1)
  56. if len(l) < 2:
  57. return False
  58. # Python 3.10.5
  59. # example result that had to be filtered:
  60. # string: started.
  61. # result: ['started', '']
  62. if l[1] == str(''):
  63. return False
  64. #print(s, l, file=sys.stderr)
  65. if l[1] in image_formats:
  66. return True
  67. return False
  68. result = 0
  69. tagged = ""
  70. # support multiple images (gallery style)
  71. tags = [] # list of strings
  72. output = []
  73. gallery = []
  74. ptags = config["tag_paragraphs"]
  75. sep = ""
  76. if "line_separator" in config:
  77. sep = config["line_separator"]
  78. for line in message:
  79. images = [] # list of integers
  80. words = line.split()
  81. for i in range(len(words)):
  82. word = words[i]
  83. # don't help people click http
  84. if word.find("src=") == 0 or word.find("href=") == 0:
  85. continue
  86. elif word.find("https://") != -1:
  87. w = escape(word)
  88. new_word = ("<a href=\"%s\">%s</a>") % (w, w)
  89. words[i] = new_word
  90. elif word.find("#") != -1 and len(word) > 1:
  91. # split by unicode blank character if present
  92. # allows tagging such as #fanfic|tion
  93. w = word.split(chr(8206))
  94. # w[0] is the portion closest to the #
  95. tags.append(w[0])
  96. new_word = "<span class=\"hashtag\">%s</span>" % (w[0])
  97. if len(w) > 1:
  98. new_word += w[1]
  99. words[i] = new_word
  100. elif is_image(word, config["accepted_images"]):
  101. images.append(i)
  102. if len(images) > 0:
  103. # function invokes pop() which modifies list 'words'
  104. gc = config["gallery"] if "gallery" in config else None
  105. gallery = make_gallery(images, words, gc)
  106. if ptags and len(words) > 0:
  107. words.insert(0,"<p>")
  108. words.append("</p>")
  109. output.append(" ".join(words))
  110. # avoid paragraph with an image gallery
  111. if len(gallery) > 0:
  112. output.append("".join(gallery))
  113. gallery = []
  114. return sep.join(output), tags
  115. class Post:
  116. def __init__(self, ts, msg):
  117. self.timestamp = ts.strip() # string
  118. self.message = msg # list
  119. # format used for sorting
  120. def get_epoch_time(self):
  121. t = dateutil.parser.parse(self.timestamp)
  122. return int(t.timestamp())
  123. # format used for display
  124. def get_short_time(self):
  125. t = dateutil.parser.parse(self.timestamp)
  126. return t.strftime("%y %b %d")
  127. def parse_txt(filename):
  128. content = []
  129. with open(filename, 'r') as f:
  130. content = f.readlines()
  131. posts = [] # list of posts - same order as file
  132. message = [] # list of lines
  133. # {-1 = init;; 0 = timestamp is next, 1 = message is next}
  134. state = -1
  135. timestamp = ""
  136. for line in content:
  137. if state == -1:
  138. state = 0
  139. continue
  140. elif state == 0:
  141. timestamp = line
  142. state = 1
  143. elif state == 1:
  144. if len(line) > 1:
  145. message.append(line)
  146. else:
  147. p = Post(timestamp, message)
  148. posts.append(p)
  149. # reset
  150. message = []
  151. state = 0
  152. return posts
  153. def get_posts(posts, config):
  154. taginfos = []
  155. tagcloud = dict() # (tag, count)
  156. tagged = dict() # (tag, index of message)
  157. total = len(posts)
  158. count = total
  159. index = count # - 1
  160. timeline = []
  161. btns = None
  162. for post in posts:
  163. markedup, tags = markup(post.message, config)
  164. count -= 1
  165. index -= 1
  166. timeline.append(
  167. make_post(count, post.get_short_time(), config, markedup)
  168. )
  169. for tag in tags:
  170. if tagcloud.get(tag) == None:
  171. tagcloud[tag] = 0
  172. tagcloud[tag] += 1
  173. if tagged.get(tag) == None:
  174. tagged[tag] = []
  175. tagged[tag].append(index)
  176. return timeline, tagcloud, tagged
  177. def make_tagcloud(d, rell):
  178. sorted_d = {k: v for k,
  179. v in sorted(d.items(),
  180. key=lambda item: -item[1])}
  181. output = []
  182. fmt = "<span class=\"hashtag\"><a href=\"%s\">%s(%i)</a></span>"
  183. #fmt = "<span class=\"hashtag\">%s(%i)</span>"
  184. for key in d.keys():
  185. link = rell % key[1:]
  186. output.append(fmt % (link, key, d[key]))
  187. return output
  188. class Paginator:
  189. def __init__(self, post_count, ppp, loc=None):
  190. if post_count <= 0:
  191. raise Exception
  192. if not loc:
  193. loc = "pages"
  194. if loc and not os.path.exists(loc):
  195. os.mkdir(loc)
  196. self.TOTAL_POSTS = post_count
  197. self.PPP = ppp
  198. self.TOTAL_PAGES = int(post_count/self.PPP)
  199. self.SUBDIR = loc
  200. self.FILENAME = "%i.html"
  201. self.written = []
  202. def toc(self, current_page=None, path=None): #style 1
  203. if self.TOTAL_PAGES < 1:
  204. return "[no pages]"
  205. if path == None:
  206. path = self.SUBDIR
  207. # For page 'n' do not create an anchor tag
  208. fmt = "<a href=\"%s\">[%i]</a>" #(filename, page number)
  209. anchors = []
  210. for i in reversed(range(self.TOTAL_PAGES)):
  211. if i != current_page:
  212. x = path + "/" + (self.FILENAME % i)
  213. anchors.append(fmt % (x, i))
  214. else:
  215. anchors.append("<b>[%i]</b>" % i)
  216. return "\n".join(anchors)
  217. # makes one page
  218. def singlepage(self, template, tagcloud, timeline_, i=None, p=None):
  219. tc = "\n".join(tagcloud)
  220. tl = "\n\n".join(timeline_)
  221. toc = self.toc(i, p)
  222. return template.format(
  223. postcount=self.TOTAL_POSTS, tags=tc, pages=toc, timeline=tl
  224. )
  225. def paginate(self, template, tagcloud, timeline, is_tagline=False):
  226. outfile = "%s/%s" % (self.SUBDIR, self.FILENAME)
  227. timeline.reverse() # reorder from oldest to newest
  228. start = 0
  229. for i in range(start, self.TOTAL_PAGES):
  230. fn = outfile % i
  231. with open(fn, 'w') as f:
  232. self.written.append(fn)
  233. prev = self.PPP * i
  234. curr = self.PPP * (i+1)
  235. sliced = timeline[prev:curr]
  236. sliced.reverse()
  237. f.write(self.singlepage(template, tagcloud, sliced, i, "."))
  238. return
  239. import argparse
  240. if __name__ == "__main__":
  241. def sort(filename):
  242. def export(new_content, new_filename):
  243. with open(new_filename, 'w') as f:
  244. print(file=f)
  245. for post in new_content:
  246. print(post.timestamp, file=f)
  247. print("".join(post.message), file=f)
  248. return
  249. posts = parse_txt(filename)
  250. posts.sort(key=lambda e: e.get_epoch_time())
  251. outfile = ("%s.sorted" % filename)
  252. print("Sorted text written to ", outfile)
  253. export(reversed(posts), outfile)
  254. def get_args():
  255. p = argparse.ArgumentParser()
  256. p.add_argument("template", help="an html template file")
  257. p.add_argument("content", help="text file for microblog content")
  258. p.add_argument("--sort", \
  259. help="sorts content from oldest to newest"
  260. " (this is a separate operation from page generation)", \
  261. action="store_true")
  262. args = p.parse_args()
  263. if args.sort:
  264. sort(args.content)
  265. exit()
  266. return args.template, args.content
  267. # assume relative path
  268. def demote_css(template, css_list, level=1):
  269. prepend = ""
  270. if level == 1:
  271. prepend = '.'
  272. else:
  273. for i in range(level):
  274. prepend = ("../%s" % prepend)
  275. tpl = template
  276. for css in css_list:
  277. tpl = tpl.replace(css, ("%s%s" % (prepend, css) ))
  278. return tpl
  279. # needs review / clean-up
  280. # ideally relate 'lvl' with sub dir instead of hardcoding
  281. def writepage(template, timeline, tagcloud, config, subdir = None):
  282. html = ""
  283. with open(template,'r') as f:
  284. html = f.read()
  285. try:
  286. count = len(timeline)
  287. p = config["postsperpage"]
  288. pagectrl = Paginator(count, p, subdir)
  289. except ZeroDivisionError as e:
  290. print("error: ",e, ". check 'postsperpage' in config", file=sys.stderr)
  291. exit()
  292. except Exception as e:
  293. print("error: ",e, ("(number of posts = %i)" % count), file=sys.stderr)
  294. exit()
  295. latest = timeline if count <= pagectrl.PPP else timeline[:pagectrl.PPP]
  296. if subdir == None: # if top level page
  297. lvl = 1
  298. tcloud = make_tagcloud(tagcloud, "./tags/%s/latest.html")
  299. print(pagectrl.singlepage(html, tcloud, latest))
  300. tcloud = make_tagcloud(tagcloud, "../tags/%s/latest.html")
  301. pagectrl.paginate(
  302. demote_css(html, config["relative_css"], lvl),
  303. tcloud, timeline
  304. )
  305. elif subdir == "placeholder":
  306. lvl = 1
  307. tcloud = make_tagcloud(tagcloud, "./tags/%s/latest.html")
  308. with open ("webring.html", 'w') as f:
  309. print(pagectrl.singlepage(html, tcloud, timeline),file=f)
  310. else: # if timelines per tag
  311. is_tagline = True
  312. lvl = 2
  313. newhtml = demote_css(html, config["relative_css"], lvl)
  314. tcloud = make_tagcloud(tagcloud, "../%s/latest.html")
  315. fn = "%s/latest.html" % subdir
  316. with open(fn, 'w') as f:
  317. pagectrl.written.append(fn)
  318. f.write(
  319. pagectrl.singlepage(newhtml, tcloud, latest, p=".")
  320. )
  321. pagectrl.paginate(newhtml, tcloud, timeline, is_tagline)
  322. return pagectrl.written
  323. import toml
  324. def load_settings():
  325. s = dict()
  326. filename = "settings.toml"
  327. if os.path.exists(filename):
  328. with open(filename, 'r') as f:
  329. s = toml.loads(f.read())
  330. else:
  331. s = None
  332. return s
  333. import json
  334. # generate json basd on profile
  335. def export_profile(post_count, last_update, config):
  336. if "profile" not in config:
  337. return
  338. if "username" not in config["profile"] \
  339. or "url" not in config["profile"]:
  340. print("Warning: no profile exported", file=sys.stderr)
  341. return
  342. profile = {
  343. "username" : config["profile"]["username"],
  344. "url": config["profile"]["url"],
  345. "last-updated": last_update,
  346. "short-bio" : config["profile"]["short_bio"],
  347. "post-count" : post_count
  348. }
  349. if "avatar" in config["profile"]:
  350. profile["avatar"] = config["profile"]["avatar"]
  351. with open(config["file_output"], 'w') as f:
  352. print(json.dumps(profile), file=f)
  353. def fn1(webring_config): # come up with better name later/
  354. def get_proxy():
  355. proxy = ""
  356. if "http_proxy" in os.environ:
  357. proxy = os.environ["http_proxy"]
  358. elif "https_proxy" in os.environ:
  359. proxy = os.environ["https_proxy"]
  360. host = proxy[proxy.rfind('/') + 1: proxy.rfind(':')]
  361. port = proxy[proxy.rfind(':') + 1:]
  362. foo = proxy.find("socks://") >= 0 or proxy.find("socks5h://")
  363. return host, int(port), foo
  364. def fetch(follow_list):
  365. import pycurl
  366. from io import BytesIO
  367. curl = pycurl.Curl()
  368. if "http_proxy" in os.environ or "https_proxy" in os.environ:
  369. hostname, port_no, is_socks = get_proxy()
  370. curl.setopt(pycurl.PROXY, hostname)
  371. curl.setopt(pycurl.PROXYPORT, port_no)
  372. if is_socks:
  373. curl.setopt(pycurl.PROXYTYPE, pycurl.PROXYTYPE_SOCKS5_HOSTNAME)
  374. other_people = []
  375. for someone in follow_list:
  376. buf = BytesIO()
  377. curl.setopt(curl.WRITEDATA, buf)
  378. curl.setopt(pycurl.URL, someone)
  379. try:
  380. curl.perform()
  381. other_people.append(json.loads(buf.getvalue()))
  382. except pycurl.error as e:
  383. print(e,": ", someone, file=sys.stderr)
  384. # print(buf.getvalue(),"\n\t", curl.getinfo(curl.CONTENT_TYPE), file=sys.stderr)
  385. curl.close()
  386. return other_people
  387. list_of_json_objs = fetch(webring_config["list"])
  388. try:
  389. list_of_json_objs.sort(key=lambda e: e["last-updated"], reverse=True)
  390. except:
  391. pass
  392. if list_of_json_objs == []:
  393. print("no remote profiles loaded", file=sys.stderr)
  394. return []
  395. rendered = []
  396. template = webring_config["format"]
  397. SHORT_BIO_LIMIT = 150
  398. for profile in list_of_json_objs:
  399. try:
  400. epoch_timestamp = profile["last-updated"]
  401. if not isinstance(epoch_timestamp, int):
  402. epoch_timestamp = 0
  403. post_count = profile["post-count"]
  404. if not isinstance(post_count, int):
  405. post_count = 0
  406. self_desc = profile["short-bio"]
  407. if len(profile["short-bio"]) >= SHORT_BIO_LIMIT:
  408. self_desc = profile["short-bio"][:SHORT_BIO_LIMIT] + "..."
  409. foo = template.format(
  410. __avatar__=escape(profile["avatar"]),
  411. __handle__=escape(profile["username"]),
  412. __url__=escape(profile["url"]),
  413. __post_count__ = post_count,
  414. __shortbio__= escape(self_desc),
  415. __lastupdated__= strftime(
  416. "%Y %b %d", localtime(epoch_timestamp)) )
  417. except KeyError as e:
  418. print("remote profile is missing key: ", e, file=sys.stderr)
  419. print("\tsource: ", profile, file=sys.stderr)
  420. rendered.append(foo)
  421. return rendered
  422. def main():
  423. tpl, content = get_args()
  424. cfg = load_settings()
  425. if cfg == None:
  426. print("exit: no settings.toml found.", file=sys.stderr)
  427. return
  428. if "post" not in cfg:
  429. print("exit: table 'post' absent in settings.toml", file=sys.stderr)
  430. return
  431. if "page" not in cfg:
  432. print("exit: table 'page' absent in settings.toml", file=sys.stderr)
  433. return
  434. p = parse_txt(content)
  435. tl, tc, tg = get_posts(p, cfg["post"])
  436. if tl == []:
  437. return
  438. # main timeline
  439. updated = []
  440. updated += writepage(tpl, tl, tc, cfg["page"])
  441. # timeline per tag
  442. if tc != dict() and tg != dict():
  443. if not os.path.exists("tags"):
  444. os.mkdir("tags")
  445. for key in tg.keys():
  446. tagline = []
  447. for index in tg[key]:
  448. tagline.append(tl[index])
  449. # [1:] means to omit hashtag from dir name
  450. updated += writepage(
  451. tpl, tagline, tc, cfg["page"], \
  452. subdir="tags/%s" % key[1:] \
  453. )
  454. if "webring" in cfg:
  455. if cfg["webring"]["enabled"] == True:
  456. export_profile(
  457. len(p), p[0].get_epoch_time(), cfg["webring"] )
  458. if "following" in cfg["webring"]:
  459. fellows = fn1(cfg["webring"]["following"] )
  460. if fellows != []:
  461. updated += writepage(
  462. tpl, fellows, tc, cfg["page"], subdir="placeholder" )
  463. with open("updatedfiles.txt", 'w') as f:
  464. for filename in updated:
  465. print(filename, file=f) # sys.stderr)
  466. if "latestpage" in cfg:
  467. print(cfg["latestpage"], file=f)
  468. if "latestpages" in cfg:
  469. for page in cfg["latestpages"]:
  470. print(page, file=f)
  471. try:
  472. main()
  473. except KeyError as e:
  474. traceback.print_exc()
  475. print("\n\tA key may be missing from your settings file.", file=sys.stderr)
  476. except dateutil.parser._parser.ParserError:
  477. traceback.print_exc()
  478. print("\n\tFailed to interpret a date from string..",
  479. "\n\tYour file of posts may be malformed.",
  480. "\n\tCheck if your file starts with a line break.", file=sys.stderr)
  481. except toml.decoder.TomlDecodeError:
  482. traceback.print_exc()
  483. print("\n\tYour configuration file is malformed.")