parser.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #!/usr/bin/env python3
  2. # processes https://malscraper.azurewebsites.net/
  3. # until I setup a db for bookkeeping of my own this is my ghetto solution
  4. import sys
  5. import xmltodict
  6. from models import Data, Summary, Anime
  7. # global state because I couldn't design this utility to be better
  8. category = 'NONE'
  9. in_list = 0
  10. body1 = '''<html>
  11. <head>
  12. <title>strlst anime index</title>
  13. <meta charset="utf-8"/>
  14. <link rel='stylesheet' type='text/css' href='../style-list.css'>
  15. </head>
  16. <body>
  17. <h1><a href="../index.html">strlst</a> anime index</h1>
  18. <p>listing consumed anime, done this way because all other list keeping utilities suck</p>
  19. <p>note:
  20. <ul>
  21. <li>no ratings, ratings are only a temporal concept anyway</li>
  22. <li>notable anime denoted with a little (++) (in accordance with newspeak)</li>
  23. <li>autogenerated using <a href='https://notabug.org/strlst/malscraperparser'>https://notabug.org/strlst/malscraperparser</a></li>
  24. </ul>
  25. </p>
  26. '''
  27. body2 = '''
  28. </body>
  29. </html>
  30. '''
  31. # designed to generate actual html for my own list
  32. # terrible solution
  33. def gen_list(_, entry):
  34. global in_list
  35. global category
  36. s = Summary()
  37. a = Anime()
  38. if a.match_structure(entry):
  39. if a.data['my_status'] != category:
  40. if in_list:
  41. in_list = 0
  42. print('</ul>')
  43. category = a.data['my_status']
  44. print('<h2>{c}</h2>'.format(c=category))
  45. if not in_list:
  46. in_list = 1
  47. print('<ul>')
  48. print(' <li>{title}{fav} <b>({watched}/{total_eps} eps.) ({fr} - {to})</b></li>'.format(
  49. title = a.data['series_title'],
  50. fav = ' (++)' if a.data['my_score'] != '0' else '',
  51. watched = a.data['my_watched_episodes'],
  52. total_eps = a.data['series_episodes'],
  53. fr = a.data['my_start_date'],
  54. to = a.data['my_finish_date'],
  55. ))
  56. if len(a.data['my_comments']) > 0:
  57. in_list = 0
  58. print('</ul>')
  59. print(' <p>{c}</p>'.format(c=a.data['my_comments'].replace('\n', '</p><p>')))
  60. return True
  61. # unused
  62. def handle_entry(_, entry):
  63. s = Summary()
  64. a = Anime()
  65. if a.match_structure(entry):
  66. print(a)
  67. elif s.match_structure(entry):
  68. print(s)
  69. return True
  70. if __name__ == '__main__':
  71. xml = sys.stdin.read()
  72. print(body1)
  73. xmltodict.parse(xml, item_depth=2, item_callback=gen_list)
  74. print(body2)