-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathget_tasks.py
More file actions
190 lines (141 loc) · 5.92 KB
/
get_tasks.py
File metadata and controls
190 lines (141 loc) · 5.92 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import arrow
import BeautifulSoup
import ConfigParser
import json
import requests
import sys
import urllib
class AmazonManager():
def __init__(self, email, password, token, list_id):
self.email = email
self.password = password
self.cheddar_access_token = token
self.cheddar_list_id = list_id
self.session = requests.Session()
self.default_headers = {
'User-Agent': 'Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.13) '\
+ 'Gecko/20101206 Ubuntu/10.10 (maverick) Firefox/3.6.13',
'Charset': 'utf-8',
'Origin': 'http://echo.amazon.com',
'Referer': 'http://echo.amazon.com/spa/index.html',
}
self.session.headers.update(self.default_headers)
self.login()
def __del__(self):
self.logout()
def check_list_for_item(self, item):
# The Cheddar API URL for fetching a list
url = "https://api.cheddarapp.com/v1/lists/%s/tasks" % self.cheddar_list_id
# Put the OAuth info into the headers
headers = {
"Authorization": "Bearer %s" % self.cheddar_access_token,
}
cheddar_request = requests.get(url, headers=headers)
list_json = cheddar_request.json()
# Iterate over all the items in the list and check for a match
# to the item we are interested in
for item_dict in list_json:
if item_dict['text'].lower() == item['text'].lower():
return True
# If we don't find a match, return false
return False
def add_item_to_cheddar(self, item):
# The Cheddar API URL for adding a task to a list
url = "https://api.cheddarapp.com/v1/lists/%s/tasks" % self.cheddar_list_id
# The body is the item to add
data = {
"task[text]": item['text'].title(),
}
# Put the OAuth info into the headers
headers = {
"Authorization": "Bearer %s" % self.cheddar_access_token,
}
# Make the request
cheddar_request = requests.post(url, data=data, headers=headers)
def find_csrf_cookie(self):
for cookie in self.session.cookies:
if cookie.name == "csrf":
return cookie.value
return None
def delete_shopping_items(self, items):
# This PUT request needs special headers
headers = {
'Content-type': 'application/json',
'csrf': self.find_csrf_cookie(),
'Accept': 'application/json, text/javascript, */*; q=0.01',
}
# Loop through the items and delete each one
for item in items:
id = urllib.quote_plus(item['itemId'])
item['deleted'] = True
url = 'https://pitangui.amazon.com/api/todos/%s' % id
delete_request = self.session.put(url, data=json.dumps(item), headers=headers)
if not delete_request.status_code == 200:
print "Error deleting item"
def fetch_shopping_items(self):
# Request the shopping list todo API
url = 'https://pitangui.amazon.com/api/todos?type=SHOPPING_ITEM&size=100&completed=false'
shopping_request = self.session.get(url)
data = shopping_request.json()
# Find all the items
items = []
if data.has_key('values'):
for value in data['values']:
items.append(value)
# Return our list of item objects
return items
def logout(self):
self.session.headers.update({ 'Referer': 'http://echo.amazon.com/spa/index.html' })
url = 'https://pitangui.amazon.com/logout'
self.session.get(url)
def login(self):
# Request the login page
login_url = 'https://pitangui.amazon.com'
login_request = self.session.get(login_url)
# Turn the login page into a soup object
login_soup = BeautifulSoup.BeautifulSoup(login_request.text)
# Find the <form> tag and the action from the login page
form_el = login_soup.find('form')
action_attr = form_el.get('action')
# Set up the parameters we will pass to the signin
parameters = {
'email': self.email,
'password': self.password,
'create': 0,
}
# Find all the hidden form elements and stick them in the params
for hidden_el in form_el.findAll(type="hidden"):
parameters[hidden_el['name']] = hidden_el['value']
# Update the session with the new referer
self.session.headers.update({ 'Referer': login_url })
# Post to the login page
login_request = self.session.post(action_attr, data=parameters)
# Make sure it was successful
if login_request.status_code != 200:
sys.exit("Error logging in! Got status %d" % login.status_code)
if __name__ == "__main__":
# Load the config info from the config.txt file
config = ConfigParser.ConfigParser()
config.read("config.txt")
print "%s\tChecking Amazon Shopping List" % arrow.now()
# Make sure we have the items in the config
try:
email = config.get('Amazon', 'email')
password = config.get('Amazon', 'password')
token = config.get('Cheddar', 'Token')
list_id = config.get('Cheddar', 'ListID')
except Exception, e:
sys.exit("Invalid or missing config.txt file.")
# Instantiate the manager
manager = AmazonManager(email, password, token, list_id)
# Get all the items on your shopping list
items = manager.fetch_shopping_items()
for item in items:
print "%s\tFound Item: %s" % (arrow.now(), item['text'])
# Make sure the item is not already in Cheddar
if manager.check_list_for_item(item) == True:
print "%s\tItem already exists in Cheddar. Skipping." % arrow.now()
else:
manager.add_item_to_cheddar(item)
# Delete from the Echo
manager.delete_shopping_items(items)