-
Notifications
You must be signed in to change notification settings - Fork 0
/
posting_to_ws.py
executable file
·219 lines (190 loc) · 8.51 KB
/
posting_to_ws.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#!/usr/bin/env python3
import re
import time
import dateutil.parser
import datetime
from pathlib import Path
import uuid
from typing import Optional, Union, Sequence, List, Tuple
from pydantic import BaseModel, ValidationError, Field, validator, root_validator, Extra
from pydantic.dataclasses import dataclass
import pywikibot as pwb
import sqlalchemy as sa
from sqlalchemy.sql import or_
from db import *
import make_work_wikipages
from db_set_titles_ws import Level
SITE = pwb.Site('ru', 'wikisource', user='TextworkerBot')
summary = '[[Викитека:Проект:Импорт текстов/Lib.ru]]'
year_limited = 2022 - 70 - 1
date_of_start_bot_uploading = dateutil.parser.parse('2022-03-26')
datetime_now = datetime.datetime.now()
ta = AllTables
tt = Titles
class D(BaseModel):
class Config:
# validate_assignment = True
extra = Extra.allow
# arbitrary_types_allowed = True
def posting_page(page: pwb.Page, text_new: str, summary: str):
# if page.text != text_new:
if page.text != text_new: # or len(page.text) - len(text_new):
# len1, len2 = len(page.text), len(text_new) # or len(page.text) - len(text_new):
# if len1 != len2:
page.text = text_new
try:
page.save(summary=summary)
except pwb.exceptions.OtherPageSaveError as e:
print(e)
Path(f'error_wikipages_texts', uuid.uuid4().hex + '.wiki').write_text(f'{page.title()}\n{text_new}')
except Exception as e:
print(e)
Path(f'error_wikipages_texts', uuid.uuid4().hex + '.wiki').write_text(f'{page.title()}\n{text_new}')
else:
return True
else:
return True
def check_moved_target(page):
# переименованная страница без редиректа
try:
moved_target = page.moved_target()
return moved_target
except Exception as e:
pass
def process_page(d: make_work_wikipages.X, update_only=False, update_text_content_only=False):
tid = d.tid
if update_only:
pagename = d.title_ws_as_uploaded
else:
pagename = d.title_ws_as_uploaded or d.title_ws_as_uploaded or d.title_ws_proposed
if pagename is None:
print('pagename is None')
return
if len(pagename.encode()) >= 255:
print(f'title length > 255 bytes, {pagename=}')
return
elif re.search(r'[\[\]]', pagename):
print(f'illegal char(s) in {pagename=}')
return
page = pwb.Page(SITE, pagename)
# moved_target = page.moved_target() # переименованная страница без редиректа
# page.has_deleted_revisions() # определение удалённых страниц, или только удалённых ревизий?
# contributors = page.contributors()
# page.oldest_revision['timestamp'] < date_of_start_bot_uploading
# page.oldest_revision['user']
if update_only:
if page.exists(): # todo: to reposting
created_before_0326 = page.oldest_revision['timestamp'] < date_of_start_bot_uploading
mybot_creater = page.oldest_revision['user'] == 'TextworkerBot'
dbd.titles.update({
'id': tid,
# 'created_before_0326': created_before_0326,
'mybot_creater': mybot_creater
}, ['id'])
if created_before_0326 and not mybot_creater:
print('страница создана не ботом:', pagename)
return
if page.latest_revision['user'] == 'TextworkerBot' and not d.time_update:
update_text_content_only = False
if page.latest_revision['user'] in ['ButkoBot', 'Lozman'] and not d.time_update:
update_text_content_only = True
# if page.latest_revision['user'] == ['TextworkerBot', 'ButkoBot'] and not d.time_update:
# update_text_content_only = True
text_new = re.sub(r'(<div class="text">).*?(</div>\s*(?:{{PD.*?}})?\s*\[\[Категория)',
r'\1\n' + d.wikified.replace('\u005c', r'\\') + r'\n\2', page.text, flags=re.DOTALL) \
if update_text_content_only else d.wikipage_text
# if page.text != text_new:
# print()
if page.latest_revision['user'] not in ['TextworkerBot', 'ButkoBot', 'Lozman']:
dbd.titles.update({'id': tid,
'uploaded': True,
# 'updated_as_named_proposed': True,
'title_ws_as_uploaded': pagename,
# 'time_update': datetime_now,
'is_lastedit_by_user': True,
}, ['id'])
print("page.latest_revision['user'] not in ['TextworkerBot', 'ButkoBot']")
else:
if ok := posting_page(page, text_new, summary):
dbd.titles.update({'id': tid,
'uploaded': True,
'updated_as_named_proposed': True,
'title_ws_as_uploaded': pagename,
'time_update': datetime_now,
}, ['id'])
print(f'{tid=}, {d.year_dead=}')
return True
else:
print('страница не сущ.:', pagename)
else:
if page.exists(): # todo: to reposting
print(f'page exists {tid=}, {pagename=}')
# if page.isRedirectPage():
# pass
# else:
# pass
else:
while not page.exists():
if target_page := check_moved_target(page):
# переименованная страница без редиректа todo
continue
elif page.has_deleted_revisions():
# страница удалялась и нет перенаправлений
break
else:
print(f'{pagename=}')
if ok := posting_page(page, d.wikipage_text, summary):
dbd.titles.update({'id': tid,
'uploaded': True,
'title_ws_as_uploaded': pagename,
'time_update': datetime_now,
'title_ws_proposed_identical_level': Level.identical,
'created_before_0326': False,
'mybot_creater': True},
['id'])
print(f'{tid=}, {d.year_dead=}')
return True
def make_wikipages_to_db():
offset = 0
limit = 100
while True:
stmt = sa.select(AllTables).where(
ta.banned == 1,
ta.do_upload == 1,
# do_update_as_named_proposed=True,
ta.uploaded_text == 0,
# do_update_2=True,
# is_wikified=True,
ta.is_wikified == 1,
# ta.is_new_text_differed == 1,
# wiki_differ_wiki2=1,
# tid={'>':150000},
# tid=94652,
# updated_as_named_proposed=False,
# is_same_title_in_ws_already=False,
# ta.wikified.isnot(None),
ta.title_ws_proposed.isnot(None),
# ta.title_ws_as_uploaded.isnot(None),
# ta.time_update.is_(None),
# or_(ta.time_update.is_(None), ta.time_update < dateutil.parser.parse("2022-05-01 11:30")),
# ta.is_lastedit_by_user == 1,
or_(ta.text_len < 2048, ta.text_len.is_(None)),
or_(ta.year_dead <= year_limited, ta.year <= 1917),
# ta.title_ws_proposed.like('Будущая порода людей%'),
# ta.oo == 1,
# cola.wiki.like('%.da.ru%'),
# cola.wikified.not_like('%feb-web.ru%'),
# col.lang.isnot(None),
).limit(limit).offset(offset)
res = dbs.execute(stmt).scalars().fetchall()
for r in res:
d = make_work_wikipages.X.from_orm(r)
d.make_wikipage()
process_page(d, update_only=False)
# process_page(d, update_only=True, update_text_content_only=True)
if len(res) < limit:
break
offset += limit
if __name__ == '__main__':
wiki_text = make_wikipages_to_db()
# db.titles.update({'id': 91593, 'uploaded': True}, ['id'])