A copyright violation detector running on Wikimedia Cloud Services https://tools.wmflabs.org/copyvios/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

112 lines
4.1 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 flask import g
  9. from .misc import get_globals_db
  10. __all__ = ["set_background"]
  11. def set_background(selected):
  12. conn = get_globals_db()
  13. if "CopyviosScreenCache" in g.cookies:
  14. cache = g.cookies["CopyviosScreenCache"].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. g.descurl = descurl
  30. return bg_url
  31. def _update_url(conn, service, bg_id, callback):
  32. query1 = "SELECT update_time FROM updates WHERE update_service = ?"
  33. query2 = "SELECT 1 FROM background WHERE background_id = ?"
  34. query3 = "DELETE FROM background WHERE background_id = ?"
  35. query4 = "INSERT INTO background VALUES (?, ?, ?, ?, ?, ?)"
  36. query5 = "SELECT 1 FROM updates WHERE update_service = ?"
  37. query6 = "UPDATE updates SET update_time = ? WHERE update_service = ?"
  38. query7 = "INSERT INTO updates VALUES (?, ?)"
  39. query8 = "SELECT * FROM background WHERE background_id = ?"
  40. with conn.cursor() as cursor:
  41. cursor.execute(query1, (service,))
  42. try:
  43. update_time = datetime.utcfromtimestamp(cursor.fetchall()[0][0])
  44. except IndexError:
  45. update_time = datetime.min
  46. plus_one = update_time + timedelta(days=1)
  47. max_age = datetime(plus_one.year, plus_one.month, plus_one.day)
  48. if datetime.utcnow() > max_age:
  49. filename, url, descurl, width, height = callback()
  50. cursor.execute(query2, (bg_id,))
  51. if cursor.fetchall():
  52. cursor.execute(query3, (bg_id,))
  53. cursor.execute(query4, (bg_id, filename, url, descurl, width,
  54. height))
  55. cursor.execute(query5, (service,))
  56. if cursor.fetchall():
  57. cursor.execute(query6, (time(), service))
  58. else:
  59. cursor.execute(query7, (service, time()))
  60. else:
  61. cursor.execute(query8, (bg_id,))
  62. filename, url, descurl, width, height = cursor.fetchone()[1:]
  63. return filename, url, descurl, width, height
  64. def _get_fresh_potd():
  65. site = _get_site()
  66. date = datetime.utcnow().strftime("%Y-%m-%d")
  67. page = site.get_page("Template:Potd/" + date)
  68. regex = ur"\{\{Potd filename\|(?:1=)?(.*?)\|.*?\}\}"
  69. filename = re.search(regex, page.get()).group(1)
  70. return _load_file(site, filename)
  71. def _get_fresh_list():
  72. site = _get_site()
  73. page = site.get_page("User:The Earwig/POTD")
  74. regex = ur"\*\*?\s*\[\[:File:(.*?)\]\]"
  75. filenames = re.findall(regex, page.get())
  76. filename = random.choice(filenames)
  77. return _load_file(site, filename)
  78. def _load_file(site, filename):
  79. res = site.api_query(action="query", prop="imageinfo", iiprop="url|size",
  80. titles="File:" + filename)
  81. data = res["query"]["pages"].values()[0]["imageinfo"][0]
  82. url = data["url"]
  83. descurl = data["descriptionurl"]
  84. width = data["width"]
  85. height = data["height"]
  86. return filename.replace(" ", "_"), url, descurl, width, height
  87. def _get_site():
  88. try:
  89. return g.bot.wiki.get_site("commonswiki")
  90. except exceptions.SiteNotFoundError:
  91. return g.bot.wiki.add_site(project="wikimedia", lang="commons")
  92. def _build_url(screen, filename, url, imgwidth, imgheight):
  93. width = screen["width"]
  94. if float(imgwidth) / imgheight > float(screen["width"]) / screen["height"]:
  95. width = int(float(imgwidth) / imgheight * screen["height"])
  96. if width >= imgwidth:
  97. return url
  98. url = url.replace("/commons/", "/commons/thumb/")
  99. return url + "/" + str(width) + "px-" + filename