-
Notifications
You must be signed in to change notification settings - Fork 2
/
tweeteats.py
executable file
·83 lines (59 loc) · 1.88 KB
/
tweeteats.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
#!/usr/bin/python
import tweepy
import config_twitter
import sqlite3
import dbhandler
import os
import re
auth = tweepy.OAuthHandler(config_twitter.API_CONSUMER_KEY, config_twitter.API_SECRET)
#auth = tweepy.auth.BasicAuthHandler(config_twitter.USER, config_twitter.PASSWORD, secure=True)
def authorize():
redirect_url = auth.get_authorization_url()
print 'Go here: ' + redirect_url
verifier = raw_input('Verifier')
auth.get_access_token(verifier)
return (auth.access_token.key, auth.access_token.secret)
def initialize(key, secret):
auth.set_access_token(key, secret)
def tweet( message ):
api = tweepy.API(auth)
api.update_status(message)
def tweetTopOfQueue(cursor):
newRestaurant = dbhandler.getTopOfQueue(cursor)
message = getMessage(newRestaurant)
tweet( message )
dbhandler.deleteFromQueue(cursor, newRestaurant)
def getMessage(newRestaurant):
nkfmNameRE = re.compile("^NKFM-(.*)$")
restoName = newRestaurant.Name
search = nkfmNameRE.search(restoName)
if search:
restoName = "{name} (Kitchener Farmer's Market)".format(name=search.group(1))
message = "{name}: {address}, {city}".format(
name = restoName,
address = newRestaurant.Address,
city = newRestaurant.City)
return message
def main():
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--authorize", action="store_true",
help="Authorize this script to update your Twitter account.")
parser.add_argument("--database", type=dbhandler.dbArgType,
default='restaurants.db',
help="SQLite database file for storing updates")
args = parser.parse_args()
db = args.database
cursor = db.cursor()
if args.authorize:
(key, secret) = authorize()
print (key, secret)
dbhandler.storeKey(cursor, key, secret)
else:
(key, secret) = dbhandler.getKey(cursor)
initialize(key, secret)
tweetTopOfQueue(cursor)
db.commit()
db.close()
if __name__ == "__main__":
main()