256 lines
5.9 KiB
Python
256 lines
5.9 KiB
Python
from settings import username, password, url
|
|
import ox
|
|
import json
|
|
import codecs
|
|
from urllib.request import urlopen
|
|
import csv
|
|
|
|
api = ox.api.API(url)
|
|
user = api.signin({'username': username, 'password': password})
|
|
|
|
if 'errors' in user['data']:
|
|
print("Authentication Failed!")
|
|
|
|
def exportTranslations():
|
|
response = api.findTranslations({
|
|
"keys": ["key", "value", "id"],
|
|
"query": {
|
|
"conditions": [],
|
|
"operator": "&"
|
|
},
|
|
"range": [0, 2000],
|
|
"sort": [{
|
|
"key": "key",
|
|
"operator": "+"
|
|
}]
|
|
})
|
|
with open('translations.csv', 'w') as csvfile:
|
|
fieldnames = ['id', 'key', 'value']
|
|
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
|
|
writer.writeheader()
|
|
for item in response['data']['items']:
|
|
del item['lang']
|
|
writer.writerow(item)
|
|
|
|
|
|
def titleContains(string):
|
|
return api.find({
|
|
'sort': [{
|
|
'key': 'title',
|
|
'operator': '+'
|
|
}],
|
|
'query': {
|
|
'conditions': [
|
|
{
|
|
'key': 'title',
|
|
'value': string,
|
|
'operator': ''
|
|
}
|
|
],
|
|
'operator': '&'
|
|
},
|
|
'keys': ['id', 'title']
|
|
})
|
|
|
|
def getIdFromTitle(string):
|
|
result = api.find({
|
|
'sort': [{
|
|
'key': 'title',
|
|
'operator': '+'
|
|
}],
|
|
'query': {
|
|
'conditions': [
|
|
{
|
|
'key': 'title',
|
|
'value': string,
|
|
'operator': '='
|
|
}
|
|
],
|
|
'operator': '&'
|
|
},
|
|
'keys': ['id', 'title']
|
|
})
|
|
items = result['data']['items']
|
|
if (len(items) > 1):
|
|
raise Exception("more than 1 item matches title")
|
|
elif (len(items) == 0):
|
|
return None
|
|
else:
|
|
return items[0]['id']
|
|
|
|
def addFeaturedPerson(id, person):
|
|
result = api.get({'id': id, 'keys': 'featured'})
|
|
featured = result['data']['featured']
|
|
if person in featured:
|
|
return
|
|
else:
|
|
featured.append(person)
|
|
return api.edit({
|
|
'id': id,
|
|
'featured': featured
|
|
})
|
|
|
|
def dumpUnknownPlaces():
|
|
result = api.findPlaces({
|
|
"query": {
|
|
"conditions": [{
|
|
'key': 'lat',
|
|
'value': None,
|
|
'operator': '='
|
|
}],
|
|
"operator": ""
|
|
},
|
|
"keys": [
|
|
"id",
|
|
"countryCode",
|
|
"type",
|
|
"name",
|
|
"alternativeNames",
|
|
"lat",
|
|
"lng",
|
|
"area",
|
|
"area",
|
|
"geoname",
|
|
"matches",
|
|
"matches"
|
|
],
|
|
"range": [
|
|
0,
|
|
5000
|
|
],
|
|
"sort": [
|
|
{
|
|
"key": "geoname",
|
|
"operator": "+"
|
|
}
|
|
]
|
|
})
|
|
out = codecs.open("unknownplaces.json", "w", encoding="utf-8")
|
|
json.dump(result['data']['items'], out)
|
|
out.close()
|
|
return result
|
|
|
|
def replacePlaceNames(old, new, operator="", start=0, end=100):
|
|
matchedPlaces = findAnnotations(old, 'places', operator, start, end)
|
|
logFile = open("replacedPlacesLog.txt", "a")
|
|
ret = []
|
|
for m in matchedPlaces:
|
|
value = m['value']
|
|
new_value = value.replace(old, new).strip()
|
|
if new_value == '':
|
|
log = "EMPTY VALUE: %s" % m['id']
|
|
logFile.write(log)
|
|
print(log)
|
|
continue
|
|
ret.append(api.editAnnotation({
|
|
'id': m['id'],
|
|
'in': m['in'],
|
|
'out': m['out'],
|
|
'value': new_value
|
|
}))
|
|
log = "%s| %s| %s" % (m['id'], value, new_value,)
|
|
logFile.write(log + "\n")
|
|
print(log)
|
|
logFile.close()
|
|
return ret
|
|
|
|
|
|
def findAnnotations(value, layer, operator="", start=0, end=500):
|
|
result = api.findAnnotations({
|
|
'query': {
|
|
'conditions': [
|
|
{
|
|
'key': 'layer',
|
|
'value': layer,
|
|
'operator': '='
|
|
},
|
|
{
|
|
'key': 'value',
|
|
'value': value,
|
|
'operator': operator
|
|
}
|
|
],
|
|
'operator': '&'
|
|
},
|
|
'keys': [],
|
|
'range': [start, end]
|
|
})
|
|
return result['data']['items']
|
|
|
|
|
|
def dumpKnownPlaces():
|
|
result = api.findPlaces({
|
|
"query": {
|
|
"conditions": [{
|
|
'key': 'lat',
|
|
'value': None,
|
|
'operator': '!'
|
|
}],
|
|
"operator": ""
|
|
},
|
|
"keys": [
|
|
"id",
|
|
"countryCode",
|
|
"type",
|
|
"name",
|
|
"alternativeNames",
|
|
"lat",
|
|
"lng",
|
|
"area",
|
|
"area",
|
|
"geoname",
|
|
"matches",
|
|
"matches"
|
|
],
|
|
"range": [
|
|
0,
|
|
5000
|
|
],
|
|
"sort": [
|
|
{
|
|
"key": "geoname",
|
|
"operator": "+"
|
|
}
|
|
]
|
|
})
|
|
out = codecs.open("knownplaces.json", "w", encoding="utf-8")
|
|
json.dump(result['data']['items'], out)
|
|
out.close()
|
|
return result
|
|
|
|
|
|
#def replaceAnnotation(track, old, new):
|
|
|
|
def getTranscriptsForList(listId='sanj:spaces of theatre'):
|
|
result = api.find({
|
|
'sort': [{
|
|
'key': 'title',
|
|
'operator': '+'
|
|
}],
|
|
'query': {
|
|
'conditions': [
|
|
{
|
|
'key': 'list',
|
|
'value': listId,
|
|
'operator': ''
|
|
}
|
|
],
|
|
'operator': '&'
|
|
},
|
|
'keys': ['id', 'title']
|
|
})
|
|
items = [{'id': item['id'], 'title': item['title']} for item in result['data']['items']]
|
|
outfolder = "out"
|
|
for i in items:
|
|
print(i['title'])
|
|
savePath = "%s/%s_%s.txt" % (outfolder, i['id'], i['title'])
|
|
transcript_url = 'http://pad.ma/%s/transcripts.srt' % i['id']
|
|
txt = urlopen(transcript_url).read()
|
|
outfile = open(savePath, "w")
|
|
outfile.write(txt)
|
|
outfile.close()
|
|
|
|
|
|
def getIds(result):
|
|
return [item.id for item in result['data']['items']]
|