pandoraApi/utils.py
2012-02-21 02:55:17 +05:30

200 lines
4.3 KiB
Python

from settings import username, password, url
import ox
import json
import codecs
api = ox.api.API(url)
user = api.signin({'username': username, 'password': password})
if user['data'].has_key("errors"):
print "Authentication Failed!"
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()
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 getIds(result):
return [item.id for item in result['data']['items']]