A copyright violation detector running on Wikimedia Cloud Services https://tools.wmflabs.org/copyvios/
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 

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