-
Notifications
You must be signed in to change notification settings - Fork 0
/
posting_authors_pages.py
executable file
·207 lines (185 loc) · 7.99 KB
/
posting_authors_pages.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
#!/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_
import db_schema as db
import make_author_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-06-03')
datetime_now = datetime.datetime.now()
tat = db.AllTables
tt = db.Titles
tac = db.authors_with_cat
cols = tac.table.c
ta = db.Authors
tac = db.AuthorsCategories
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 page
else:
return page
def process_page(d: make_author_wikipages.A, update_only=False, update_text_content_only=False):
# tid = d.tid
pagename = d.name_WS
print(f'{pagename=}')
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:
# todo
pass
# 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'
# db.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']:
# db.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):
# db.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 {pagename=}')
# if page.isRedirectPage():
# pass
# else:
# pass
created_before_0603 = page.oldest_revision['timestamp'] < date_of_start_bot_uploading
mybot_creater = page.oldest_revision['user'] == 'TextworkerBot'
if created_before_0603 and not mybot_creater:
print('страница создана не ботом:', pagename)
db.authors.update({
'id': d.author_id,
'already_created': 1,
'pagename_as_uploaded': pagename,
'pid_ws': page.pageid,
}, ['id'])
return
elif not created_before_0603 and mybot_creater:
if page := posting_page(page, d.wikipage_text, summary):
try:
db.authors.update({
'id': d.author_id,
'uploaded': 1,
'pagename_as_uploaded': pagename,
'pid_ws': page.pageid,
'image_filename_wiki': d.image_filename_wiki,
}, ['id'])
# print(f'{d.year_dead=}')
return True
except sa.exc.IntegrityError as e:
return
else:
return
else:
if page := posting_page(page, d.wikipage_text, summary):
db.authors.update({
'id': d.author_id,
'uploaded': 1,
'pagename_as_uploaded': pagename,
'pid_ws': page.pageid,
'image_filename_wiki': d.image_filename_wiki,
}, ['id'])
# print(f'{d.year_dead=}')
return True
else:
print()
def posting():
year_limited = 2022 - 70 - 1
stmt = sa.select(ta, tac.name_ws).outerjoin(tac, ta.litarea == tac.name_site).where(
ta.year_dead <= year_limited,
ta.is_author == 1, ta.do_upload == 1, ta.uploaded == 0, ta.already_created == 0
# ta.id == 12256
) # .limit(10)
res = db.db_.conn.execute(stmt).fetchall()
for r in res:
x = dict(r)
d = make_author_wikipages.A.parse_obj(x)
d.make_wikipage()
process_page(d, update_only=False)
# process_page(d, update_only=True, update_text_content_only=True)
if __name__ == '__main__':
posting()
# db.titles.update({'id': 91593, 'uploaded': True}, ['id'])