-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathurlcite.py
More file actions
165 lines (142 loc) · 7.17 KB
/
Copy pathurlcite.py
File metadata and controls
165 lines (142 loc) · 7.17 KB
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
from __future__ import annotations
import pywikibot
from pywikibot import pagegenerators
from pywikibot.bot import (
AutomaticTWSummaryBot,
ConfigParserBot,
ExistingPageBot,
SingleSiteBot,
)
import wikitextparser as wtp
import re
import os.path
from urllib.parse import urlparse
from urllib.request import urlopen, Request
#from bs4 import BeautifulSoup
# This is required for the text that is shown when you run this script
# with the parameter -help.
docuReplacements = {'¶ms;': pagegenerators.parameterHelp} # noqa: N816
websites = {}
"""
hdr = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
'Accept-Encoding': 'none',
'Accept-Language': 'uk-UA,uk;q=0.8',
'Connection': 'keep-alive'}
"""
class BasicBot(
# Refer pywikobot.bot for generic bot classes
SingleSiteBot, # A bot only working on one site
ConfigParserBot, # A bot which reads options from scripts.ini setting file
# CurrentPageBot, # Sets 'current_page'. Process it in treat_page method.
# # Not needed here because we have subclasses
ExistingPageBot, # CurrentPageBot which only treats existing pages
AutomaticTWSummaryBot, # Automatically defines summary; needs summary_key
):
use_redirects = False # treats non-redirects only
summary_key = 'basic-changing'
update_options = {
'replace': False, # delete old text and write the new text
'summary': "Виправлення [[:Категорія:Помилки CS1: Сторінки із зовнішнім посиланням у невідповідних параметрах]]", # your own bot summary
'text': 'Test', # add this text from option. 'Test' is default
'top': False, # append text on top of the page
}
def treat_page(self) -> None:
text = self.current_page.text
#print(text)
parsed = wtp.parse(text)
templates = parsed.templates
for template in templates:
template_str = template.string
try:
if "cite " in template.name.lower():
for argument in template.arguments:
#if not "url" in argument.name.strip().lower():
if argument.name.strip().lower() in ["веб-сайт", "видавець", "publisher"]:
if not "http" in argument.value.lower(): continue
try:
hostname = urlparse(argument.value).hostname
except Exception as e:
print(f"Error reading url: {e}")
continue
"""
if hostname in websites:
argument.value = websites[hostname]
else:
"""
print(template_str)
"""
try:
req = Request(argument.value, headers=hdr)
soup = BeautifulSoup(urlopen(req), features="html.parser")
title = soup.title.string
print(f'title of the page: {title}; Press y to accept')
y = input()
except Exception as e:
print(f'Failed to load the page: {e}')
y = "n"
if y.strip().lower() == "y":
argument.value = title
else:
"""
print(f'Argument {argument.name}')
suggested_value = ""
if hostname == None:
link = wtp.parse(argument.value)
if len(link.external_links) != 0:
link = link.external_links[0]
link_text = link.text
if link_text is not None: suggested_value = link_text
elif link.url is not None: suggested_value = urlparse(link.url).hostname
elif "." in hostname: suggested_value = hostname
print(f'suggested value: {suggested_value}')
y = input()
if y.strip().lower() == "y":
argument.value = suggested_value
elif y.strip().lower() == "n":
continue
else:
print("Enter title:")
title = input()
argument.value = title
#if argument.name.strip().lower() in ["website", "вебсайт"]:
#websites[hostname] = argument.value
#print(websites)
text = text.replace(template_str, template.string)
except Exception as e:
print(e)
continue
self.put_current(text, summary=self.opt.summary)
def main(*args: str) -> None:
options = {}
# Process global arguments to determine desired site
local_args = pywikibot.handle_args(args)
# This factory is responsible for processing command line arguments
# that are also used by other scripts and that determine on which pages
# to work on.
gen_factory = pagegenerators.GeneratorFactory()
# Process pagegenerators arguments
local_args = gen_factory.handle_args(local_args)
# Parse your own command line arguments
for arg in local_args:
arg, _, value = arg.partition(':')
option = arg[1:]
if option in ('summary', 'text'):
if not value:
pywikibot.input('Please enter a value for ' + arg)
options[option] = value
# take the remaining options as booleans.
# You will get a hint if they aren't pre-defined in your bot class
else:
options[option] = True
# The preloading option is responsible for downloading multiple
# pages from the wiki simultaneously.
gen = gen_factory.getCombinedGenerator(preload=True)
# check if further help is needed
if not pywikibot.bot.suggest_help(missing_generator=not gen):
# pass generator and private options to the bot
bot = BasicBot(generator=gen, **options)
bot.run() # guess what it does
if __name__ == '__main__':
main()