A copyright violation detector running on Wikimedia Cloud Services https://tools.wmflabs.org/copyvios/
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

116 lines
4.2 KiB

  1. # -*- coding: utf-8 -*-
  2. from datetime import datetime, timedelta
  3. from json import loads
  4. import random
  5. import re
  6. from time import time
  7. from earwigbot import exceptions
  8. from .misc import get_bot, open_sql_connection
  9. _descurl = None
  10. def set_background(context, cookies, selected):
  11. global _descurl
  12. conn = open_sql_connection(get_bot(), "globals")
  13. if "EarwigScreenCache" in cookies:
  14. cache = cookies["EarwigScreenCache"].value
  15. try:
  16. screen = loads(cache)
  17. int(screen["width"])
  18. int(screen["height"])
  19. except (ValueError, KeyError):
  20. screen = {"width": 1024, "height": 768}
  21. else:
  22. screen = {"width": 1024, "height": 768}
  23. if selected == "potd":
  24. info = _update_url(conn, "background_potd", 1, _get_fresh_potd)
  25. else:
  26. info = _update_url(conn, "background_list", 2, _get_fresh_list)
  27. filename, url, descurl, width, height = info
  28. bg_url = _build_url(screen, filename, url, width, height)
  29. _descurl = descurl
  30. return bg_url
  31. def get_desc_url(context):
  32. return _descurl
  33. def _update_url(conn, service, bg_id, callback):
  34. query1 = "SELECT update_time FROM updates WHERE update_service = ?"
  35. query2 = "SELECT 1 FROM background WHERE background_id = ?"
  36. query3 = "DELETE FROM background WHERE background_id = ?"
  37. query4 = "INSERT INTO background VALUES (?, ?, ?, ?, ?, ?)"
  38. query5 = "SELECT 1 FROM updates WHERE update_service = ?"
  39. query6 = "UPDATE updates SET update_time = ? WHERE update_service = ?"
  40. query7 = "INSERT INTO updates VALUES (?, ?)"
  41. query8 = "SELECT * FROM background WHERE background_id = ?"
  42. with conn.cursor() as cursor:
  43. cursor.execute(query1, (service,))
  44. try:
  45. update_time = datetime.utcfromtimestamp(cursor.fetchall()[0][0])
  46. except IndexError:
  47. update_time = datetime.min
  48. plus_one = update_time + timedelta(days=1)
  49. max_age = datetime(plus_one.year, plus_one.month, plus_one.day)
  50. if datetime.utcnow() > max_age:
  51. filename, url, descurl, width, height = callback()
  52. cursor.execute(query2, (bg_id,))
  53. if cursor.fetchall():
  54. cursor.execute(query3, (bg_id,))
  55. cursor.execute(query4, (bg_id, filename, url, descurl, width,
  56. height))
  57. cursor.execute(query5, (service,))
  58. if cursor.fetchall():
  59. cursor.execute(query6, (time(), service))
  60. else:
  61. cursor.execute(query7, (service, time()))
  62. else:
  63. cursor.execute(query8, (bg_id,))
  64. filename, url, descurl, width, height = cursor.fetchone()[1:]
  65. return filename, url, descurl, width, height
  66. def _get_fresh_potd():
  67. site = _get_site()
  68. date = datetime.utcnow().strftime("%Y-%m-%d")
  69. page = site.get_page("Template:Potd/" + date)
  70. regex = ur"\{\{Potd filename\|(?:1=)?(.*?)\|.*?\}\}"
  71. filename = re.search(regex, page.get()).group(1)
  72. return _load_file(site, filename)
  73. def _get_fresh_list():
  74. site = _get_site()
  75. page = site.get_page("User:The Earwig/POTD")
  76. regex = ur"\*\*?\s*\[\[:File:(.*?)\]\]"
  77. filenames = re.findall(regex, page.get())
  78. filename = random.choice(filenames)
  79. return _load_file(site, filename)
  80. def _load_file(site, filename):
  81. res = site.api_query(action="query", prop="imageinfo", iiprop="url|size",
  82. titles="File:" + filename)
  83. data = res["query"]["pages"].values()[0]["imageinfo"][0]
  84. url = data["url"]
  85. descurl = data["descriptionurl"]
  86. width = data["width"]
  87. height = data["height"]
  88. return filename.replace(" ", "_"), url, descurl, width, height
  89. def _get_site():
  90. bot = get_bot()
  91. try:
  92. return bot.wiki.get_site("commonswiki")
  93. except exceptions.SiteNotFoundError:
  94. return bot.wiki.add_site(project="wikimedia", lang="commons")
  95. def _build_url(screen, filename, url, imgwidth, imgheight):
  96. width = screen["width"]
  97. if float(imgwidth) / imgheight > float(screen["width"]) / screen["height"]:
  98. width = int(float(imgwidth) / imgheight * screen["width"])
  99. if width >= imgwidth:
  100. return url
  101. url = url.replace("/commons/", "/commons/thumb/")
  102. return url + "/" + str(width) + "px-" + filename