def latest_events(count=25): if count < 1: return json_error("need a non-zero count") if count > max_records: count = max_records sql = cyql.connect(dsn) try: events = sql.all(''' select address, city, url, type, location, time from events limit %(count)s ''') data = [] for event in events: loc = [x.strip() for x in event[4].strip('()').split(',')] data.append({ 'desc': "%s, %s" % (event[0], event[1]), 'url': event[2], 'type': event[3], 'lat': loc[0], 'long': loc[1], 'time': event[5] }) except Exception, e: return json_error(str(e))
def event_types(): sql = cyql.connect(dsn) types = {} try: data = sql.all(''' select id, type from eventtypes ''') for item in data: if item[0] in types: return json_error("error when making type list") types[item[0]] = item[1] except Exception, e: return json_error(str(e))
def latest_events(count=25): if count < 1: return json_error("need a non-zero count") if count > max_records: count = max_records sql = cyql.connect(dsn) try: events = sql.all(''' select address, city, url, type, location, time from events limit %(count)s ''') data = [] for event in events: loc = [x.strip() for x in event[4].strip('()').split(',')] data.append({'desc': "%s, %s" % (event[0], event[1]), 'url': event[2], 'type': event[3], 'lat': loc[0], 'long': loc[1], 'time': event[5]}) except Exception, e: return json_error(str(e))
def do_search(params): sql = cyql.connect(dsn) return params.do_query(sql)
(eventtype,), = sql.all(''' insert into eventtypes (type) VALUES (%(event)s) RETURNING id ''') try: sql.run(''' insert into events (id, address, city, url, type, location, time) VALUES (%(tweetid)s, %(address)s, %(city)s, %(url)s, %(eventtype)s, %(location)s, %(timestamp)s) ''') except psycopg2.IntegrityError: pass except Exception, e: print e rejects.append(tweet) if __name__ == "__main__": sql = cyql.connect(dsn) rejects = [] for i in range(0,16): try: tweets = twitter.get("http://api.twitter.com/1/statuses/user_timeline.json", screen_name='SBCFireDispatch', count=200, page=i) eat_tweets(tweets, rejects) except Exception, e: print e time.sleep(15) cPickle.dump(rejects, open("rejects", "w")) #tweets = cPickle.load(open("stuff"))
''') try: sql.run(''' insert into events (id, address, city, url, type, location, time) VALUES (%(tweetid)s, %(address)s, %(city)s, %(url)s, %(eventtype)s, %(location)s, %(timestamp)s) ''') except psycopg2.IntegrityError: pass except Exception, e: print e rejects.append(tweet) if __name__ == "__main__": sql = cyql.connect(dsn) rejects = [] for i in range(0, 16): try: tweets = twitter.get( "http://api.twitter.com/1/statuses/user_timeline.json", screen_name='SBCFireDispatch', count=200, page=i) eat_tweets(tweets, rejects) except Exception, e: print e time.sleep(15) cPickle.dump(rejects, open("rejects", "w"))