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.
 
 
 
 
 

88 lines
2.9 KiB

  1. # -*- coding: utf-8 -*-
  2. from datetime import datetime, timedelta
  3. from json import loads
  4. import random
  5. import re
  6. from earwigbot import exceptions
  7. from flask import g
  8. __all__ = ["set_background"]
  9. def _get_commons_site():
  10. try:
  11. return g.bot.wiki.get_site("commonswiki")
  12. except exceptions.SiteNotFoundError:
  13. return g.bot.wiki.add_site(project="wikimedia", lang="commons")
  14. def _load_file(site, filename):
  15. res = site.api_query(action="query", prop="imageinfo", iiprop="url|size",
  16. titles="File:" + filename)
  17. data = res["query"]["pages"].values()[0]["imageinfo"][0]
  18. url = data["url"]
  19. descurl = data["descriptionurl"]
  20. width = data["width"]
  21. height = data["height"]
  22. return filename.replace(" ", "_"), url, descurl, width, height
  23. def _get_fresh_potd():
  24. site = _get_commons_site()
  25. date = datetime.utcnow().strftime("%Y-%m-%d")
  26. page = site.get_page("Template:Potd/" + date)
  27. regex = ur"\{\{Potd filename\|(?:1=)?(.*?)\|.*?\}\}"
  28. filename = re.search(regex, page.get()).group(1)
  29. return _load_file(site, filename)
  30. def _get_fresh_list():
  31. site = _get_commons_site()
  32. page = site.get_page("User:The Earwig/POTD")
  33. regex = ur"\*\*?\s*\[\[:File:(.*?)\]\]"
  34. filenames = re.findall(regex, page.get())
  35. filename = random.choice(filenames)
  36. return _load_file(site, filename)
  37. def _build_url(screen, filename, url, imgwidth, imgheight):
  38. width = screen["width"]
  39. if float(imgwidth) / imgheight > float(screen["width"]) / screen["height"]:
  40. width = int(float(imgwidth) / imgheight * screen["height"])
  41. if width >= imgwidth:
  42. return url
  43. url = url.replace("/commons/", "/commons/thumb/")
  44. return url + "/" + str(width) + "px-" + filename
  45. _BACKGROUNDS = {
  46. "potd": _get_fresh_potd,
  47. "list": _get_fresh_list
  48. }
  49. def _get_background(selected):
  50. if not g.last_background_updates:
  51. for key in _BACKGROUNDS:
  52. g.last_background_updates[key] = datetime.min
  53. plus_one = g.last_background_updates[selected] + timedelta(days=1)
  54. max_age = datetime(plus_one.year, plus_one.month, plus_one.day)
  55. if datetime.utcnow() > max_age:
  56. update_func = _BACKGROUNDS.get(selected, _get_fresh_list)
  57. g.background_data[selected] = update_func()
  58. g.last_background_updates[selected] = datetime.utcnow()
  59. return g.background_data[selected]
  60. def set_background(selected):
  61. if "CopyviosScreenCache" in g.cookies:
  62. cache = g.cookies["CopyviosScreenCache"].value
  63. try:
  64. screen = loads(cache)
  65. int(screen["width"])
  66. int(screen["height"])
  67. except (ValueError, KeyError):
  68. screen = {"width": 1024, "height": 768}
  69. else:
  70. screen = {"width": 1024, "height": 768}
  71. filename, url, descurl, width, height = _get_background(selected)
  72. bg_url = _build_url(screen, filename, url, width, height)
  73. g.descurl = descurl
  74. return bg_url