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.
 
 
 
 
 

92 lines
3.1 KiB

  1. # -*- coding: utf-8 -*-
  2. from datetime import datetime, timedelta
  3. from json import loads
  4. import random
  5. import re
  6. import urllib
  7. from earwigbot import exceptions
  8. from flask import g
  9. from .misc import cache
  10. __all__ = ["set_background"]
  11. def _get_commons_site():
  12. try:
  13. return cache.bot.wiki.get_site("commonswiki")
  14. except exceptions.SiteNotFoundError:
  15. return cache.bot.wiki.add_site(project="wikimedia", lang="commons")
  16. def _load_file(site, filename):
  17. data = site.api_query(
  18. action="query", prop="imageinfo", iiprop="url|size|canonicaltitle",
  19. titles="File:" + filename)
  20. res = data["query"]["pages"].values()[0]["imageinfo"][0]
  21. name = res["canonicaltitle"][len("File:"):].replace(" ", "_")
  22. return name, res["url"], res["descriptionurl"], res["width"], res["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. # Ensure all workers share the same background each day:
  36. random.seed(datetime.utcnow().strftime("%Y%m%d"))
  37. filename = random.choice(filenames)
  38. return _load_file(site, filename)
  39. def _build_url(screen, filename, url, imgwidth, imgheight):
  40. width = screen["width"]
  41. if float(imgwidth) / imgheight > float(screen["width"]) / screen["height"]:
  42. width = int(float(imgwidth) / imgheight * screen["height"])
  43. if width >= imgwidth:
  44. return url
  45. url = url.replace("/commons/", "/commons/thumb/")
  46. return "%s/%dpx-%s" % (url, width, urllib.quote(filename))
  47. _BACKGROUNDS = {
  48. "potd": _get_fresh_potd,
  49. "list": _get_fresh_list
  50. }
  51. def _get_background(selected):
  52. if not cache.last_background_updates:
  53. for key in _BACKGROUNDS:
  54. cache.last_background_updates[key] = datetime.min
  55. plus_one = cache.last_background_updates[selected] + timedelta(days=1)
  56. max_age = datetime(plus_one.year, plus_one.month, plus_one.day)
  57. if datetime.utcnow() > max_age:
  58. update_func = _BACKGROUNDS.get(selected, _get_fresh_list)
  59. cache.background_data[selected] = update_func()
  60. cache.last_background_updates[selected] = datetime.utcnow().date()
  61. return cache.background_data[selected]
  62. def set_background(selected):
  63. if "CopyviosScreenCache" in g.cookies:
  64. screen_cache = g.cookies["CopyviosScreenCache"].value
  65. try:
  66. screen = loads(screen_cache)
  67. int(screen["width"])
  68. int(screen["height"])
  69. except (ValueError, KeyError):
  70. screen = {"width": 1024, "height": 768}
  71. else:
  72. screen = {"width": 1024, "height": 768}
  73. filename, url, descurl, width, height = _get_background(selected)
  74. bg_url = _build_url(screen, filename, url, width, height)
  75. g.descurl = descurl
  76. return bg_url