forked from ccie14023/sparkcfg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspark.py
124 lines (78 loc) · 3.18 KB
/
spark.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
120
121
122
123
124
import requests
from requests.auth import HTTPBasicAuth
from requests.packages.urllib3.exceptions import InsecureRequestWarning
import json
bot_token = "NTlmZGY4NWYtNWNjOC00OTFkLTlhNzEtZmI5NTFjOWRhODNhNGFlYTEwOWUtYjE3"
proxies = {
"http":"http://173.36.224.108:8080",
"https":"https://173.36.224.108:8080"
}
def create_room(room_name, token):
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
headers = {'Authorization':'Bearer '+token,
'Content-Type':'application/json'}
body = json.dumps({'title':room_name})
resp = requests.post('https://api.ciscospark.com/v1/rooms',
verify=False,headers=headers,data=body)
print resp
def list_rooms(token):
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
headers = {'Authorization':'Bearer '+token,
'Content-Type':'application/json'}
resp = requests.get('https://api.ciscospark.com/v1/rooms',
verify=False,headers=headers)
return resp
def get_room_id(room_name, token):
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
id = ""
headers = {'Authorization':'Bearer '+token,
'Content-Type':'application/json'}
resp = requests.get('https://api.ciscospark.com/v1/rooms',
verify=False,headers=headers,proxies=proxies)
if resp.status_code == 200:
rooms = json.loads(resp.text)['items']
for room in rooms:
if room['title'] == room_name:
id = room['id']
return id
def list_messages(room_id, token):
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
headers = {'Authorization':'Bearer '+token,
'Content-Type':'application/json'}
resp = requests.get('https://api.ciscospark.com/v1/messages?roomId={}'.format(room_id),
verify=False,headers=headers)
return resp.text
def post_message(message_text, room_id, token, markdown=False):
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
headers = {'Authorization':'Bearer '+token,
'Content-Type':'application/json'}
if markdown:
body = json.dumps({'roomId':room_id,'markdown':message_text})
else:
body = json.dumps({'roomId':room_id,'text':message_text})
resp = requests.post('https://api.ciscospark.com/v1/messages',
verify=False,headers=headers,data=body,proxies=proxies)
return resp
def post_message_with_image(message_text, img_url, room_id, token):
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
headers = {'Authorization':'Bearer '+token,
'Content-Type':'application/json'}
body = json.dumps({'roomId':room_id,'text':message_text, 'files':img_url})
resp = requests.post('https://api.ciscospark.com/v1/messages',
verify=False,headers=headers,data=body)
return resp
def cleanup_room(room_id, token):
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
messages = json.loads(list_messages(room_id, token))
headers = {'Authorization':'Bearer '+token,
'Content-Type':'application/json'}
for message in messages['items']:
resp = requests.delete('https://api.ciscospark.com/v1/messages/{}'.format(message['id']),
verify=False,headers=headers)
print resp.status_code
def main():
room=get_room_id('jeff test room')
print post_message('Testing 6:32pm',room).status_code
list_messages(room)
if __name__ == "__main__":
main()