mab-refresh-thumbnail-mal.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #!/usr/bin/env python3
  2. # Copyright Penta (c) 2018/2020 - Under BSD License
  3. # Compatible for Python 3.6.X
  4. #
  5. # Check and update all the thumbnail for manga/anime in the MyAnimeBot database.
  6. # Can be pretty long and send a lot of request to MyAnimeList.net,
  7. # Use it only once in a while to clean the database.
  8. #
  9. # Dependencies (for CentOS 7):
  10. # yum install python3 mariadb-devel gcc python3-devel
  11. # python3.6 -m pip install --upgrade pip
  12. # pip3.6 install mysql python-dateutil asyncio html2text bs4 aiodns cchardet configparser
  13. # pip3.6 install mysql.connector
  14. # Library import
  15. import os
  16. import re
  17. import asyncio
  18. import urllib.request
  19. import string
  20. import time
  21. import socket
  22. from html2text import HTML2Text
  23. from bs4 import BeautifulSoup
  24. from configparser import ConfigParser
  25. # Custom library
  26. from myanimebot.myanimelist import get_thumbnail
  27. import myanimebot.globals as globals
  28. # Script version
  29. VERSION = "1.2"
  30. globals.logger.info("Booting the MyAnimeBot Thumbnail Refresher " + VERSION + "...")
  31. def main() :
  32. globals.logger.info("Starting the refresher task...")
  33. count = 0
  34. cursor = globals.conn.cursor(buffered=True)
  35. cursor.execute("SELECT guid, title, thumbnail FROM t_animes")
  36. datas = cursor.fetchall()
  37. globals.logger.info(str(len(datas)) + " medias are going to be checked.")
  38. for data in datas:
  39. try:
  40. image = get_thumbnail(data[0])
  41. if (image == data[2]) :
  42. if (image != "") :
  43. globals.logger.debug("Thumbnail for " + str(data[1]) + " already up to date.")
  44. else :
  45. globals.logger.info("Thumbnail for " + str(data[1]) + " still empty.")
  46. else :
  47. if (image != "") :
  48. cursor.execute("UPDATE t_animes SET thumbnail = %s WHERE guid = %s", [image, data[0]])
  49. globals.conn.commit()
  50. globals.logger.info("Updated thumbnail found for \"" + str(data[1]) + "\": %s", image)
  51. count += 1
  52. else :
  53. try :
  54. urllib.request.urlopen(data[2])
  55. globals.logger.info("Thumbnail for \"" + str(data[1]) + "\" is now empty, avoiding change.")
  56. except :
  57. globals.logger.info("Thumbnail for \"" + str(data[1]) + "\" has been deleted!")
  58. except Exception as e :
  59. globals.logger.warning("Error while updating thumbnail for '" + str(data[1]) + "': " + str(e))
  60. time.sleep(3)
  61. globals.logger.info("All thumbnails checked!")
  62. cursor.close()
  63. globals.logger.info(str(count) + " new thumbnails, time taken: %ss" % round((time.time() - startTime), 2))
  64. # Starting main function
  65. if __name__ == "__main__" :
  66. startTime = time.time()
  67. main()
  68. globals.logger.info("Thumbnail refresher script stopped")
  69. # We close all the ressources
  70. globals.conn.close()