-
Notifications
You must be signed in to change notification settings - Fork 0
/
ir_r_py3_v2.py
213 lines (173 loc) · 5.88 KB
/
ir_r_py3_v2.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
import requests
import sys
print(sys.version)
print(sys.executable)
sys.path.append('/data/project/edgars/pywikibot/')
sys.path.append('/data/project/edgars/pwb_helpers/toolforge/')
import json
import urllib.parse
import pywikibot, re
import toolforge
from datetime import datetime
from customFuncs import basic_sparql as sparql
from customFuncs import get_quarry as quarry
lvsite = pywikibot.Site("lv", "wikipedia")
conn = toolforge.connect('lvwiki_p')
"""
SELECT * WHERE {
?item ^schema:about ?article .
{?article wikibase:badge wd:Q17437796} union {?article wikibase:badge wd:Q17437798} .
?item ^schema:about ?articleLV .
?articleLV schema:isPartOf <https://lv.wikipedia.org/>
}
SELECT ?item ?articleLV (GROUP_CONCAT(?article; separator=", ") as ?insts) WHERE {
?item ^schema:about ?article .
{?article wikibase:badge wd:Q17437796} union {?article wikibase:badge wd:Q17437798} .
?item ^schema:about ?articleLV .
?articleLV schema:isPartOf <https://lv.wikipedia.org/> .
}
group by ?item ?articleLV
"""
finalquery = """SELECT ?item ?articleLV ?article WHERE {
?articleLV schema:about ?item; schema:isPartOf <https://lv.wikipedia.org/> .
?item ^schema:about ?article .
{?article wikibase:badge wd:Q17437796} union {?article wikibase:badge wd:Q17437798} .
}
"""
SQL = """select p.page_title, count(l.ll_lang), p.page_len
from langlinks l
join page p on p.page_id=l.ll_from and p.page_namespace=0
group by l.ll_from
having count(l.ll_lang)>99
order by p.page_len asc"""
def encode_if_necessary(b):
if type(b) is bytes:
return b.decode('utf8')
return b
def run_query():
#query = query.encode('utf-8')
#print(query)
try:
cursor = conn.cursor()
cursor.execute(SQL)
rows = cursor.fetchall()
except KeyboardInterrupt:
sys.exit()
return rows
#
articlereg = re.compile('(https:\/\/..\.[^\.]+\.org\/wiki\/([^ ]+))')
articlerep = re.compile('(https:\/\/[^\.]+\.[^\.]+\.org\/wiki\/)([^ ]+)')
reg1 = 'https:\/\/([^\.]+)\.wikipedia\.org\/wiki\/'
def list_to_dict(json_data):
bigm = {}
for data in json_data:
lvname = urllib.parse.unquote(data['articleLV']['value'].replace('https://lv.wikipedia.org/wiki/','')).replace(' ','_')
insts = data['article']['value'] if 'article' in data else ''
if lvname in bigm:
bigm[lvname].append(insts)
else:
bigm[lvname] = [insts]
return bigm
#
def addToDict(json_data,currDict):
blah = currDict
parsed = list_to_dict(json_data)
#{"item":{"type":"uri","value":"http://www.wikidata.org/entity/Q1"},"articleLV":{"type":"uri","value":"https://lv.wikipedia.org/wiki/Visums"},"insts":{"type":"literal","value":"https://fi.wikipedia.org/wiki/Maailmankaikkeus, https://nap.wikipedia.org/wiki/Annevierzo, https://scn.wikipedia.org/wiki/Universu, https://ba.wikipedia.org/wiki/%D2%92%D0%B0%D0%BB%D3%99%D0%BC, https://en.wikipedia.org/wiki/Universe, https://nah.wikipedia.org/wiki/Cemanahuac, https://si.wikipedia.org/wiki/%E0%B7%80%E0%B7%92%E0%B7%81%E0%B7%8A%E0%B7%80%E0%B6%BA"}}
for data in parsed:
articles = parsed[data]
goodlangs = []
for fa in articles:
languagename = re.search(reg1,fa)
if languagename:
extracted = languagename.group(1)
if len(extracted)==2:
goodlangs.append(extracted)
#
if len(goodlangs)==0:
continue
#articles = [urllib.parse.unquote(re.sub(articlerep,r'\2',blah12)) for blah12 in articles if 'wikipedia.org' in blah12]
#pywikibot.output(item+lvname+plname+coords+insts)
#pywikibot.output(data)
#pywikibot.output(articles)
#if len(articles)==0:
# continue
#print('*'*60)
#thisdict.update{}
"""
if lvname in blah:
if item not in blah[lvname]:
#blah[lvname].append([item, coords, insts])
blah[lvname][0].append(insts)
#print('here2')
#else:
# print('here')
else:
blah[lvname] = [coords, insts]
#print('here3')
"""
blah[data] = articles
#print('*'*660)
#pywikibot.output(blah)
return blah
"""
lvwiki1 = 'quarry-14884-user_treisijs-isakie-raksti-run135974.json'
wikidata1 = 'fa ga wd.json'
lvwiki = json.loads(open(lvwiki1, "r", encoding='utf-8').read())
wikidata = json.loads(open(wikidata1, "r", encoding='utf-8').read())
lvwiki = lvwiki['rows']
wikidata = wikidata['results']['bindings']
"""
lvwiki = run_query()#quarry('14884','1')
wikidata = sparql(finalquery)
#["Keisija_gr\u0113da", "P625", -67.78333333, 62.2]
#lv = {d[0]:[d[1],d[2]] for d in lvwiki}
dsd = {}
wd = addToDict(wikidata,dsd)
rows = []
counter=1
for_list = []
for article in lvwiki:
#lvdata = lvwiki[article]
article = [encode_if_necessary(b) for b in article]
name = article[0].replace(' ','_')
#if name=='NASA': continue
if name not in wd:
continue
if '._gads' in name:
continue
wddata = wd[name]
row = "|-\n| {}. || [[{}]] || {} || {}".format(counter, name.replace('_',' '), article[2], len(wddata))
counter += 1
rows.append(row)
for_list.append([name, article[2],wddata])
#counter +=1
#if counter==150:
# break
"""
while counter<150:
for article in lvwiki:
#lvdata = lvwiki[article]
name = article[0].replace('_',' ')
if name not in wd:
continue
if '. gads' in name:
continue
wddata = wd[name]
row = "|-\n| [[{}]] || {} || {}".format(name, article[2], len(wddata))
rows.append(row)
counter +=1
"""
cur_date = datetime.now().strftime("%Y-%m-%d")
table = '\n'.join(rows[:200])
reportpage = """Atjaunināts: {}
{{| class="wikitable sortable"
|-
! Nr.p.k. !! Raksts !! Raksta garums !! Vērtīgo/labo rakstu<br>skaits citu valodu<br>Vikipēdijās
{}
|}}
""".format(cur_date,table)
with open("isakieraksti.txt", "w", encoding='utf-8') as file:
file.write(str(for_list))
lvpage = pywikibot.Page(lvsite,'Dalībnieks:Edgars2007/Īsākie raksti')
lvpage.text = reportpage
lvpage.save(summary='Bots: atjaunināts', botflag=False, minor=False)