pandoraApi/utils.py

233 lines
5.3 KiB
Python
Raw Normal View History

2012-02-19 17:36:43 +00:00
from settings import username, password, url
import ox
import json
import codecs
2018-12-02 14:29:55 +00:00
from urllib.request import urlopen
2012-02-19 17:36:43 +00:00
api = ox.api.API(url)
user = api.signin({'username': username, 'password': password})
2018-12-02 14:29:55 +00:00
if 'errors' in user['data']:
print("Authentication Failed!")
2012-02-19 17:36:43 +00:00
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
2012-02-20 21:25:17 +00:00
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()
2012-02-20 21:39:26 +00:00
if new_value == '':
log = "EMPTY VALUE: %s" % m['id']
logFile.write(log)
2018-12-02 14:29:55 +00:00
print(log)
2012-02-20 21:37:01 +00:00
continue
2012-02-20 21:25:17 +00:00
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")
2018-12-02 14:29:55 +00:00
print(log)
2012-02-20 21:25:17 +00:00
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']
2012-02-19 17:36:43 +00:00
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:
2018-12-02 14:29:55 +00:00
print(i['title'])
savePath = "%s/%s_%s.txt" % (outfolder, i['id'], i['title'])
transcript_url = 'http://pad.ma/%s/transcripts.srt' % i['id']
2018-12-02 14:29:55 +00:00
txt = urlopen(transcript_url).read()
outfile = open(savePath, "w")
outfile.write(txt)
outfile.close()
2012-02-19 17:36:43 +00:00
def getIds(result):
return [item.id for item in result['data']['items']]