Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3.  
  4. from io import BytesIO
  5. from sys import argv
  6. from textwrap import wrap
  7. from zipfile import ZipFile
  8.  
  9. from fimfarchive.fetchers import FimfarchiveFetcher
  10. from fimfarchive.stories import Story
  11. from fimfarchive.utils import tqdm
  12.  
  13.  
  14. TERMS = {
  15. 'bone',
  16. 'remember',
  17. 'stasis',
  18. 'tar wol',
  19. }
  20.  
  21. TAGS = {
  22. 'Human',
  23. 'Princess Luna',
  24. }
  25.  
  26.  
  27. def matches(story: Story) -> bool:
  28. tags = {tag['name'] for tag in story.meta['tags']}
  29.  
  30. for tag in TAGS:
  31. if tag not in tags:
  32. return False
  33.  
  34. zobj = ZipFile(BytesIO(story.data))
  35. html = [name for name in zobj.namelist() if name.endswith('html')]
  36. text = b' '.join(zobj.read(name) for name in html).decode().lower()
  37.  
  38. for term in TERMS:
  39. if term not in text:
  40. return False
  41.  
  42. return True
  43.  
  44.  
  45. def main(*args: str) -> None:
  46. if len(args) != 2:
  47. exit(f"Usage: {args[0]} <ARCHIVE>")
  48.  
  49. fetcher = FimfarchiveFetcher(args[1])
  50. progress = tqdm(fetcher)
  51.  
  52. for story in progress:
  53. if not matches(story):
  54. continue
  55.  
  56. key = story.key
  57. meta = story.meta
  58. name = meta['title']
  59. desc = '\n'.join(wrap(meta['short_description']))
  60. line = f"[{key}] {name}\n{desc}\n"
  61.  
  62. progress.write(line)
  63.  
  64.  
  65. if __name__ == '__main__':
  66. main(*argv)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement