A copyright violation detector running on Wikimedia Cloud Services https://tools.wmflabs.org/copyvios/
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

sites.py 4.5 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. # -*- coding: utf-8 -*-
  2. from time import time
  3. from urlparse import urlparse
  4. from earwigbot import exceptions
  5. def get_site(bot, lang, project, name, all_projects):
  6. if project not in [proj[0] for proj in all_projects]:
  7. return None
  8. if project == "wikimedia" and name: # Special sites:
  9. try:
  10. return bot.wiki.get_site(name=name)
  11. except exceptions.SiteNotFoundError:
  12. try:
  13. return bot.wiki.add_site(lang=lang, project=project)
  14. except (exceptions.APIError, exceptions.LoginError):
  15. return None
  16. try:
  17. return bot.wiki.get_site(lang=lang, project=project)
  18. except exceptions.SiteNotFoundError:
  19. try:
  20. return bot.wiki.add_site(lang=lang, project=project)
  21. except (exceptions.APIError, exceptions.LoginError):
  22. return None
  23. def get_sites(bot):
  24. max_staleness = 60 * 60 * 24 * 7
  25. conn = open_sql_connection(bot, "globals")
  26. query1 = "SELECT update_time FROM updates WHERE update_service = ?"
  27. query2 = "SELECT lang_code, lang_name FROM language"
  28. query3 = "SELECT project_code, project_name FROM project"
  29. with conn.cursor() as cursor:
  30. cursor.execute(query1, ("sites",))
  31. try:
  32. time_since_update = int(time() - cursor.fetchall()[0][0])
  33. except IndexError:
  34. time_since_update = time()
  35. if time_since_update > max_staleness:
  36. update_sites(bot.wiki.get_site(), cursor)
  37. cursor.execute(query2)
  38. langs = []
  39. for code, name in cursor.fetchall():
  40. if "\U" in name:
  41. name = name.decode("unicode_escape")
  42. langs.append((code, name))
  43. cursor.execute(query3)
  44. projects = cursor.fetchall()
  45. return langs, projects
  46. def update_sites(site, cursor):
  47. matrix = site.api_query(action="sitematrix")["sitematrix"]
  48. del matrix["count"]
  49. languages, projects = set(), set()
  50. for site in matrix.itervalues():
  51. if isinstance(site, list): # Special sites
  52. bad_sites = ["closed", "private", "fishbowl"]
  53. for special in site:
  54. if all([key not in special for key in bad_sites]):
  55. full = urlparse(special["url"]).netloc
  56. if full.count(".") == 1: # No subdomain, so use "www"
  57. lang, project = "www", full.split(".")[0]
  58. else:
  59. lang, project = full.rsplit(".", 2)[:2]
  60. code = u"{0}::{1}".format(lang, special["dbname"])
  61. name = special["code"].capitalize()
  62. languages.add((code, u"{0} ({1})".format(lang, name)))
  63. projects.add((project, project.capitalize()))
  64. continue
  65. this = set()
  66. for web in site["site"]:
  67. if "closed" in web:
  68. continue
  69. project = "wikipedia" if web["code"] == u"wiki" else web["code"]
  70. this.add((project, project.capitalize()))
  71. if this:
  72. code = site["code"]
  73. if "\U" in site["name"].encode("unicode_escape"):
  74. name = site["name"].encode("unicode_escape")
  75. else:
  76. name = site["name"]
  77. languages.add((code, u"{0} ({1})".format(code, name)))
  78. projects |= this
  79. save_site_updates(cursor, languages, projects)
  80. def save_site_updates(cursor, languages, projects):
  81. query1 = "SELECT lang_code, lang_name FROM language"
  82. query2 = "DELETE FROM language WHERE lang_code = ? AND lang_name = ?"
  83. query3 = "INSERT INTO language VALUES (?, ?)"
  84. query4 = "SELECT project_code, project_name FROM project"
  85. query5 = "DELETE FROM project WHERE project_code = ? AND project_name = ?"
  86. query6 = "INSERT INTO project VALUES (?, ?)"
  87. query7 = "SELECT 1 FROM updates WHERE update_service = ?"
  88. query8 = "UPDATE updates SET update_time = ? WHERE update_service = ?"
  89. query9 = "INSERT INTO updates VALUES (?, ?)"
  90. synchronize_sites_with_db(cursor, languages, query1, query2, query3)
  91. synchronize_sites_with_db(cursor, projects, query4, query5, query6)
  92. cursor.execute(query7, ("sites",))
  93. if cursor.fetchall():
  94. cursor.execute(query8, (time(), "sites"))
  95. else:
  96. cursor.execute(query9, ("sites", time()))
  97. def synchronize_sites_with_db(cursor, updates, q_list, q_rmv, q_update):
  98. removals = []
  99. cursor.execute(q_list)
  100. for site in cursor:
  101. updates.remove(site) if site in updates else removals.append(site)
  102. cursor.executemany(q_rmv, removals)
  103. cursor.executemany(q_update, updates)