-
Notifications
You must be signed in to change notification settings - Fork 0
/
site_update.py
99 lines (89 loc) · 3.95 KB
/
site_update.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
from data import Gun, Site, get_google_api
from google.cloud import ndb
client = ndb.Client()
import googlemaps
gmaps = get_google_api()
namespace = "test"
class Update:
def setGuns():
with client.context():
sites = Site.query(namespace = namespace)
for site in sites:
site_id = site.key.id()
guns = Gun.query(Gun.site_id == site_id, namespace = namespace)
site.guns = []
for gun in guns:
site.guns.append(gun.gunid)
if len(site.guns) > 0:
site.put()
else:
print(f"Deleting {site.display_name}")
site.key.delete()
def setUrls():
with client.context():
guns = Gun.query(namespace=namespace)
for gun in guns:
if gun.context:
context = gun.coll_name.split()
for word in context:
if "https" in word:
if True: #"royalarmouries" in word:
#gun.attributions.append("© Royal Armouries ") #f"https://commons.wikimedia.org/wiki/File:{word.split('/')[-1]}")
#gun.attributions.append("https://royalarmouries.org/wp-content/uploads/2018/03/Non-Commercial-Licence.pdf")
#gun.web_links=True
gun.urls.append(word)
context.remove(word)
for w in context:
if "Licence:" in w:
context.remove(w)
gun.context = " ".join(context)
print(f"{gun.attributions} : {gun.coll_name} : {gun.urls}")
#gun.put()
def fixUrls():
with client.context():
guns = Gun.query(namespace=namespace)
for gun in guns:
if len(gun.attributions) > 0 and gun.attributions[0] == "© Royal Armouries " and "royalarmouries" not in gun.urls[0]:
#if gun.collection and gun.coll_name == "National Army Museum":
#gun.attributions = {"© National Army Museum"}
print(f"{gun.site_id} : {gun.context} : {gun.urls}")
#gun.put()
def updateSites():
with client.context():
sites = Site.query(namespace=namespace)
for site in sites:
if site.type != Site.Type.GOOGLE.value:
continue
uDef = None
if site.display_name != site.geocode.get('name'):
uDef = site.display_name
try:
place = gmaps.place(site.place_id)
except Exception as e:
print(f"Site : {site.display_name} -- {str(e)}")
continue
geocode = place.get("result")
address_comps = geocode.get("address_components")
site.populate(
geocode = geocode,
attribution = place.get("html_attributions"),
display_name = geocode.get("name"),
)
if uDef:
site.display_name = uDef
print(site.display_name)
country = [comp.get("long_name") for comp in address_comps if "country" in comp.get("types")]
if len(country) > 0:
site.country = country[0]
site.put()
def deleteGuns():
with client.context():
guns = Gun.query(namespace=namespace)
for gun in guns:
description = gun.description
if description and "delete" in description.lower():
print(gun.description)
gun.key.delete()
Update.updateSites()
Update.setGuns()
Update.deleteGuns()