1
0

mab-refresh-thumbnail-mal.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #!/usr/bin/env python3
  2. # Copyright Penta (c) 2018/2021 - Under BSD License
  3. # Library import
  4. import os
  5. import re
  6. import asyncio
  7. import urllib.request
  8. import string
  9. import time
  10. import socket
  11. from html2text import HTML2Text
  12. from bs4 import BeautifulSoup
  13. from configparser import ConfigParser
  14. # Custom library
  15. from myanimebot.myanimelist import get_thumbnail
  16. import myanimebot.globals as globals
  17. # Script version
  18. VERSION = "1.2"
  19. globals.logger.info("Booting the MyAnimeBot Thumbnail Refresher " + VERSION + "...")
  20. def main() :
  21. globals.logger.info("Starting the refresher task...")
  22. count = 0
  23. cursor = globals.conn.cursor(buffered=True)
  24. cursor.execute("SELECT guid, title, thumbnail FROM t_animes WHERE service = %s", [globals.SERVICE_MAL])
  25. datas = cursor.fetchall()
  26. globals.logger.info(str(len(datas)) + " medias are going to be checked.")
  27. for data in datas:
  28. try:
  29. image = get_thumbnail(data[0])
  30. if (image == data[2]) :
  31. if (image != "") :
  32. globals.logger.debug("Thumbnail for " + str(data[1]) + " already up to date.")
  33. else :
  34. globals.logger.info("Thumbnail for " + str(data[1]) + " still empty.")
  35. else :
  36. if (image != "") :
  37. cursor.execute("UPDATE t_animes SET thumbnail = %s WHERE guid = %s", [image, data[0]])
  38. globals.conn.commit()
  39. globals.logger.info("Updated thumbnail found for \"" + str(data[1]) + "\": %s", image)
  40. count += 1
  41. else :
  42. try :
  43. urllib.request.urlopen(data[2])
  44. globals.logger.info("Thumbnail for \"" + str(data[1]) + "\" is now empty, avoiding change.")
  45. except :
  46. globals.logger.info("Thumbnail for \"" + str(data[1]) + "\" has been deleted!")
  47. except Exception as e :
  48. globals.logger.warning("Error while updating thumbnail for '" + str(data[1]) + "': " + str(e))
  49. time.sleep(3)
  50. globals.logger.info("All thumbnails checked!")
  51. cursor.close()
  52. globals.logger.info(str(count) + " new thumbnails, time taken: %ss" % round((time.time() - startTime), 2))
  53. # Starting main function
  54. if __name__ == "__main__" :
  55. startTime = time.time()
  56. main()
  57. globals.logger.info("Thumbnail refresher script stopped")
  58. # We close all the ressources
  59. globals.conn.close()