mab-refresh-thumbnail-mal.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. # Custom library
  12. from myanimebot.myanimelist import get_thumbnail
  13. import myanimebot.globals as globals
  14. # Script version
  15. VERSION = "1.2"
  16. globals.logger.info("Booting the MyAnimeBot Thumbnail Refresher " + VERSION + "...")
  17. def refresh_thumbnail_mal(startTime) :
  18. globals.logger.info("Starting the refresher task...")
  19. count = 0
  20. cursor = globals.conn.cursor(buffered=True)
  21. cursor.execute("SELECT guid, title, thumbnail FROM t_animes WHERE service = %s", [globals.SERVICE_MAL])
  22. datas = cursor.fetchall()
  23. globals.logger.info(str(len(datas)) + " medias are going to be checked.")
  24. for data in datas:
  25. try:
  26. image = get_thumbnail(data[0])
  27. if (image == data[2]) :
  28. if (image != "") :
  29. globals.logger.debug("Thumbnail for " + str(data[1]) + " already up to date.")
  30. else :
  31. globals.logger.info("Thumbnail for " + str(data[1]) + " still empty.")
  32. else :
  33. if (image != "") :
  34. cursor.execute("UPDATE t_animes SET thumbnail = %s WHERE guid = %s", [image, data[0]])
  35. globals.conn.commit()
  36. globals.logger.info("Updated thumbnail found for \"" + str(data[1]) + "\": %s", image)
  37. count += 1
  38. else :
  39. try :
  40. urllib.request.urlopen(data[2])
  41. globals.logger.info("Thumbnail for \"" + str(data[1]) + "\" is now empty, avoiding change.")
  42. except Exception as e :
  43. globals.logger.info("Thumbnail for \"" + str(data[1]) + "\" has been deleted! (" + str(e) + ")")
  44. except Exception as e :
  45. globals.logger.warning("Error while updating thumbnail for '" + str(data[1]) + "': " + str(e))
  46. time.sleep(globals.MYANIMELIST_SECONDS_BETWEEN_REQUESTS)
  47. globals.logger.info("All thumbnails checked!")
  48. cursor.close()
  49. globals.logger.info(str(count) + " new thumbnails, time taken: %ss" % round((time.time() - startTime), 2))
  50. # Starting main function
  51. if __name__ == "__main__" :
  52. refresh_thumbnail_mal(time.time())
  53. globals.logger.info("Thumbnail refresher script stopped")
  54. # We close all the ressources
  55. globals.conn.close()