backlink.py 766 B

1234567891011121314151617181920212223242526272829303132
  1. import os
  2. import re
  3. from collections import defaultdict
  4. import webtools as wt
  5. threadpath = "./threads/"
  6. def load_thread(th):
  7. th = int(th)
  8. thp = str(threadpath + str(th) + '.txt')
  9. if not os.path.exists(thp):
  10. return None
  11. with open(thp, 'r') as mt:
  12. mt = mt.read().splitlines()
  13. return mt
  14. def do_backlink(th='0'):
  15. mt = load_thread(th)
  16. bld = defaultdict(list)
  17. for n, t in enumerate(mt):
  18. repl = re.findall(r'\>\>[1-9][00-99]*', t)
  19. repl = [r[8:] for r in repl if r]
  20. for r in repl:
  21. if int(r) >= len(mt) or str(n) in bld[r]:
  22. continue
  23. bld[r].append(str(n))
  24. for r in sorted([int(k) for k in bld.keys()]):
  25. r = str(r)
  26. return bld