-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddCoordsFromFile.py
53 lines (45 loc) · 1.53 KB
/
addCoordsFromFile.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
from bs4 import BeautifulSoup
import pywikibot, requests, re, os, json, urllib.parse, sys
from urllib import parse
import pymysql
import pywikibot
from pywikibot import pagegenerators
from pywikibot.exceptions import CoordinateGlobeUnknownException
site = pywikibot.Site('wikidata', 'wikidata')
repo = site.data_repository()
prop = 'P625'
def one_item(article):
item = pywikibot.ItemPage(repo,article[0])
claims = item.get().get('claims')
if prop in claims:
pywikibot.output(u'Item %s already contains coordinates (%s)'
% (item.title(), prop))
return
coord_lat = float(article[1])
coord_long = float(article[2])
precision = 0.00027777777777778#1/100000
target = pywikibot.Coordinate(coord_lat, coord_long, precision=precision, globe_item='http://www.wikidata.org/entity/Q2')
newclaim = pywikibot.Claim(repo, prop)
newclaim.setTarget(target)
pywikibot.output(u'Adding %s, %s to %s' % (coord_lat,
coord_long,
item.title()))
try:
item.addClaim(newclaim)
#importedEnWikipedia = pywikibot.Claim(repo, u'P248')
#enWikipedia = pywikibot.ItemPage(repo, wikiitem)
#$importedEnWikipedia.setTarget(enWikipedia)
#newclaim.addSources([importedEnWikipedia])
except CoordinateGlobeUnknownException as e:
pywikibot.output(u'Skipping unsupported globe: %s' % e.args)
#
file = eval(open("toWDcoordsAREA.txt", "r", encoding='utf-8').read())
counter = 0
for one in file:
one_item(one)
counter += 1
if counter % 50 == 0:
print(counter)
sys.stdout.flush()
#
print('done')