-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwikinews-stats-comm-red.py
91 lines (78 loc) · 4.37 KB
/
wikinews-stats-comm-red.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/env python
# -- coding: utf-8 --
import time
import requests
import login
ua = {"User-agent": "pyBot/wikinews-statpages (toolforge/iluvatarbot; [email protected]) requests"}
API_url = "https://ru.wikinews.org/w/api.php"
text_comment = "{{комментарии2}} <!-- Оставьте эту строчку. Пишите комментарий ниже. -->"
try:
with open("pyBot/wikinews/service/stats-comments-redirects.txt", "r", encoding="utf-8") as f:
lines = f.readlines()
f.close()
if len(lines) == 2:
timestamp = str(lines[0]).rstrip("\n")
page_name = str(lines[1]).rstrip("\n")
except:
timestamp = "1970-01-01T00:00:01Z"
page_name = ""
def check_cats(title):
params = {"action": "raw", "title": title}
r = requests.get(url=API_url, params=params)
data = r.text
if "#REDIRECT" in data or "#перенаправление" in data or "{{перенаправление" in data or "←" in data or \
"{{Категория для категорий" in data or "{{Категория для категория" in data or "{{Метакатегория" in data or \
"{{Categoryredirect" in data or "{{Cr" in data or "{{Сategoryredirect" in data or \
"{{Сategory redirect" in data or "{{Category redirect" in data:
return False
else:
return True
def handler(members, old_title):
if len(members) > 0:
if str(members[0]["title"]) != "":
with open("pyBot/wikinews/service/stats-comments-redirects.txt", "w", encoding="utf-8") as fw:
fw.write(members[0]["timestamp"] + "\n" + str(members[0]["title"]))
fw.close()
params_list = []
for change in members:
if str(change["title"]) != old_title:
title = str(change["title"])
if not title.replace("Категория:", "").startswith('Викиновости:') \
and not title.replace("Категория:", "").startswith("Шаблоны:") \
and not title.replace("Категория:", "").startswith("Люди:") \
and not title.replace("Категория:", "").startswith("Справка:") \
and not title.replace("Категория:", "").startswith("User "):
if change["ns"] == 0:
params_list.append({"action": "edit", "format": "json", "utf8": "1", "createonly": 1, "bot": 1,
"title": "Комментарии:" + title, "text": str(text_comment),
"summary": "Создание страницы комментариев"})
if change["ns"] == 14 and check_cats(title):
params_list.append({"action": "edit", "format": "json", "utf8": "1", "createonly": 1, "bot": 1,
"title": title.replace("Категория:", ""),
"text": "#перенаправление [[" + title + "]]",
"summary": "Создание перенаправления для категории"})
params_list.append({"action": "edit", "format": "json", "utf8": "1", "createonly": 1,
"title": "Викиновости:Статистика страниц/" + title,
"text": "{{Статистика страницы|" + title + "}}",
"summary": "Создание страницы статистики"
})
if len(params_list) > 0:
token, cookies = login.login(server="ru.wikinews")
for param in params_list:
param["token"] = token
requests.post(url=API_url, data=param, cookies=cookies)
time.sleep(5)
def get_data():
try:
params = {
"action": "query", "format": "json", "utf8": "1", "list": "recentchanges", "rcend": timestamp,
"rctype": "new", "rcprop": "title|timestamp", "rcdir": "older", "rcnamespace": "0|14",
"rcshow": "!redirect", "rclimit": 500
}
r = requests.post(url=API_url, data=params, headers=ua).json()["query"]["recentchanges"]
except:
time.sleep(30)
get_data()
else:
handler(r, page_name)
get_data()