-
Notifications
You must be signed in to change notification settings - Fork 0
/
IFTTT.py
119 lines (98 loc) · 3.25 KB
/
IFTTT.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
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Date : 2016-08-30 00:25:44
# @Author : Tom Hu ([email protected])
# @Link : http://h1994st.com
# @Version : 1.0
import time
import requests
from lxml import html
import Config
class IFTTT(object):
'''
Default user: covertsan ([email protected])
'''
def __init__(self,
username=Config.IFTTT('email1'),
password=Config.IFTTT('password')):
super(IFTTT, self).__init__()
self.username = username
self._is_login = False
self._session_requests = requests.session()
self._login(username, password)
@property
def is_login(self):
return self._is_login
def _login(self, username, password):
# Get authenticity token
login_page_url = 'https://ifttt.com/login'
result = self._session_requests.get(login_page_url)
tree = html.fromstring(result.text)
authenticity_token = list(
set(tree.xpath("//input[@name='authenticity_token']/@value")))[0]
payload = {
'utf8': '✓',
'user[username]': username,
'user[password]': password,
'commit': 'Sign in',
'authenticity_token': authenticity_token,
'return_to': ''
}
# Log in
login_url = 'https://ifttt.com/session'
result = self._session_requests.post(
login_url,
data=payload,
headers=dict(referer=login_url)
)
if result.status_code != 200:
print 'Login failed!'
return
self._is_login = True
url = 'https://ifttt.com/my_applets'
result = self._session_requests.get(
url,
data=payload,
headers=dict(referer=url)
)
tree = html.fromstring(result.content)
bucket_elems = tree.cssselect(
'li.my-web-applet-card.enabled_for_user.web-applet-card')
# Get ids (applet-*)
self.applet_ids = [elem.get('id')[7:] for elem in bucket_elems]
def force_check(self, applet_id):
if not self.is_login:
raise Exception('Not login')
# Get CSRF token
applet_page_url = 'https://ifttt.com/applets/%s' % applet_id
result = self._session_requests.get(applet_page_url)
tree = html.fromstring(result.text)
authenticity_token = list(
set(tree.xpath("//meta[@name='csrf-token']/@content")))[0]
check_url = 'https://ifttt.com/services/feed/applets/%s/check' % (
applet_id)
result = self._session_requests.post(
check_url,
headers={
'referer': applet_page_url,
'X-CSRF-Token': authenticity_token
})
print result.status_code
def force_check_all(self):
if not self.is_login:
raise Exception('Not login')
for applet_id in self.applet_ids:
self.force_check(applet_id)
if __name__ == '__main__':
ifttt = IFTTT()
print ifttt.applet_ids
i = 0
while True:
i += 1
try:
ifttt.force_check_all()
except Exception as e:
print e
finally:
print '%d: Sleep 2 seconds' % i
time.sleep(2)