-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustomFuncs.py
171 lines (130 loc) · 4.54 KB
/
customFuncs.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import re, pywikibot, json, requests
import urllib.parse as urlparse
#customFuncs
null = ''
#
def basic_petscan(id):
#http://petscan.wmflabs.org/?psid=157563&format=json
payload = {
'psid': id,
'format': 'json'
}
r = requests.get('http://petscan.wmflabs.org/?', params=payload)
r.encoding = 'utf-8'
json_data = eval(r.text)
#items = [f["item"]["value"].replace('http://www.wikidata.org/entity/','') for f in json_data['results']['bindings']]
return json_data['*'][0]['a']['*']
#
def basic_sparql(query):
payload = {
'query': query,
'format': 'json'
}
r = requests.get('https://query.wikidata.org/bigdata/namespace/wdq/sparql?', params=payload)
r.encoding = 'utf-8'
json_data = eval(r.text)
#items = [f["item"]["value"].replace('http://www.wikidata.org/entity/','') for f in json_data['results']['bindings']]
return json_data['results']['bindings']
def get_quarry_run(page_id):
url = 'https://quarry.wmflabs.org/query/{}'.format(page_id)
res = requests.get(url)
pagetext = res.text
reg = '"qrun_id": (\d+)'
qid = re.search(reg, pagetext)
if qid:
return qid.group(1)
#
def get_quarry(page_id,listing='0'):
url = 'https://quarry.wmflabs.org/query/{}/result/latest/{}/json'.format(page_id,listing)
res = requests.get(url)
data = eval(res.text)
return data["rows"]
'''
#kā sarakstu - json failu kversiju rez - [0,1,2,3]
url = 'https://quarry.wmflabs.org/query/{}'.format(page_id)
res = requests.get(url)
pagetext = res.text
reg = '"qrun_id": (\d+)'
qid = re.search(reg, pagetext)
if qid:
url2 = "https://quarry.wmflabs.org/run/{}/output/{}/json".format(qid.group(1),listing)
url23= requests.get(url2)#.read()
#print('here1fsdfsd')
#pywikibot.output(url2.text)
data = json.loads(url23.text)
#print('here1fsdfssfsdfd')
return data["rows"]
'''
#
def hasprop(prop):
SPARQL = "select ?item {{ ?item p:{} [] . }}".format(prop)
#SELECT ?item { ?item p:P961 [] . }
payload = {
'query': SPARQL,
'format': 'json'
}
r = requests.get('https://query.wikidata.org/bigdata/namespace/wdq/sparql?', params=payload)
r.encoding = 'utf-8'
json_data = json.loads(r.text)
items = [f["item"]["value"].replace('http://www.wikidata.org/entity/','') for f in json_data['results']['bindings']]
return items
#
def haspropVAL(prop):
SPARQL = "SELECT ?item ?value {{ ?item p:{prop} [ ps:{prop} ?value ] . }}".format(prop=prop)
#SELECT ?item ?value { ?item p:P961 [ ps:P961 ?value ] . }
payload = {
'query': SPARQL,
'format': 'json'
}
r = requests.get('https://query.wikidata.org/bigdata/namespace/wdq/sparql?', params=payload)
r.encoding = 'utf-8'
json_data = json.loads(r.text)
itemdata = {}
for item in json_data['results']['bindings']:
wdit = item["item"]["value"].replace('http://www.wikidata.org/entity/','')
wdval =item["value"]["value"]
if wdit in itemdata:
if wdval not in itemdata[wdit]:
itemdata[wdit].append(wdval)
else:
itemdata[wdit] = [wdval]
return itemdata
#
def haspropWiki(prop,lang):
SPARQL = "SELECT ?sitelink ?value {{ ?item p:{prop} [ ps:{prop} ?value ] . ?sitelink schema:about ?item; schema:isPartOf <https://{lang}.wikipedia.org/> . }}".format(prop=prop,lang=lang)
#SELECT ?sitelink ?value { ?item p:P2697 [ ps:P2697 ?value ] . ?sitelink schema:about ?item; schema:isPartOf <https://lv.wikipedia.org/> . }
payload = {
'query': SPARQL,
'format': 'json'
}
r = requests.get('https://query.wikidata.org/bigdata/namespace/wdq/sparql?', params=payload)
r.encoding = 'utf-8'
json_data = json.loads(r.text)
itemdata = {}
makeurlstring = 'https://{}.wikipedia.org/wiki/'.format(lang)
for item in json_data['results']['bindings']:
wdit = urlparse.unquote(item["sitelink"]["value"].replace(makeurlstring,''))
wdval = item["value"]["value"]
if wdit in itemdata:
if wdval not in itemdata[wdit]:
itemdata[wdit].append(wdval)
else:
itemdata[wdit] = [wdval]
return itemdata
#
def chunker(seq, size):
return (seq[pos:pos + size] for pos in range(0, len(seq), size))
#
def pagename(bg):
bg = re.sub('\s*(\([^\(]+)$','',bg)
return bg
#
def sanityvalue(value):
value = re.sub('<!--.*?-->', '',value)#remove comments
value = re.sub('<ref([^>]+)\/>', '',value)#remove self-closing reference tags
value = re.sub('<ref((?!<\/ref>).)*<\/ref>', '',value)#remove references
value = re.sub('<ref([^>]+)>', '',value)#remove reference tags
return value
#