-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwikinews-comments[Stream].py
58 lines (48 loc) · 2.95 KB
/
wikinews-comments[Stream].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
#!/usr/bin/env python
# -- coding: utf-8 --
import json
import time
import requests
from sseclient import SSEClient as EventSource
import login
token, cookies = login.login(server="ru.wikipedia")
url = 'https://stream.wikimedia.org/v2/stream/revision-create'
API_url = "https://ru.wikinews.org/w/api.php"
def checkCats(title):
params = {"action": "raw", "title": title}
r = requests.get(url=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(change):
if change["database"] == "ruwikinews" and "rev_parent_id" not in change and change["page_is_redirect"] == False and not str(change["page_title"]).replace("Категория:", "").startswith('Викиновости:') and not str(change["page_title"]).replace("Категория:", "").startswith("Шаблоны:") and not str(change["page_title"]).replace("Категория:", "").startswith("Люди:") and not str(change["page_title"]).replace("Категория:", "").startswith("Справка:"):
if change["page_namespace"] == 0 or change["page_namespace"] == 14:
token, cookies = login.login(server="ru.wikinews")
params = {"action": "edit", "format": "json", "utf8": "1", "createonly": 1, "bot": 1, "token": token}
if change["page_namespace"] == 0:
params["title"] = "Комментарии:" + str(change["page_title"])
params["text"] = "{{комментарии2}} <!-- Оставьте эту строчку. Пишите комментарий ниже. -->"
params["summary"] = "Создание страницы комментариев"
if change["page_namespace"] == 14 and checkCats(str(change["page_title"])):
params["title"] = str(change["page_title"]).replace("Категория:", "")
params["text"] = "#перенаправление [[" + str(change["page_title"]) + "]]"
params["summary"] = "Создание перенаправления для категории"
requests.post(url=API_url, data=params, cookies=cookies)
time.sleep(5)
def start():
try:
for event in EventSource(url, retry=30000):
if event.event == 'message':
try:
change = json.loads(event.data)
except ValueError:
pass
else:
handler(change)
except Exception:
print("HTTP error")
time.sleep(30)
start()
start()