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.
 
 
 
 
 

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