-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathslack_api.py
More file actions
104 lines (74 loc) · 2.41 KB
/
slack_api.py
File metadata and controls
104 lines (74 loc) · 2.41 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
import os, json, logging, requests, re
from utils import default_config_path
from sys import argv, exit
from time import sleep
def init_slack(config, config_path=None):
if config_path is None:
config_path = default_config_path()
from fabric.operations import prompt
if 'slack' not in config.keys():
config['slack'] = {}
for credential in ['api_token']:
if credential not in config['slack'].keys():
config['slack'][credential] = prompt("Slack %s" % credential)
try:
with open(config_path, 'wb+') as C:
C.write(json.dumps(config))
return True
except Exception as e:
logging.error("Could not save config: [%s, %s]" % (type(e), e))
return False
def __get_api_token(config_path=None):
if config_path is None:
config_path = default_config_path()
try:
with open(config_path, 'rb') as C:
return json.loads(C.read())['slack']['api_token']
except Exception as e:
logging.error("Could not get API token from config: [%s, %s]" % (type(e), e))
return None
def request_file_for_user(user_id):
api_token = __get_api_token()
if api_token is None:
logging.error("Could not get Slack API token.")
return None
try:
sleep(45)
r = requests.get('https://slack.com/api/files.list?token=%s&count=2&user=%s&types=images' % \
(api_token, user_id))
print r
print r.url
r = json.loads(r.content)
if not r['ok'] or 'files' not in r.keys():
logging.error("Not OK")
return None
no_rx = r'file|dream_\d+.+'
file_to_download = r['files'][0]['url_download']
if re.match(no_rx, file_to_download.split("/")[-1]):
#logging.error("Ignoring file %s, it is a dream already" % file_to_download)
return None
return file_to_download
except Exception as e:
logging.error("Could not get any files for user: [%s, %s]" % (type(e), e))
return None
def put_file_in_dropbox(permalink):
try:
r = requests.get(permalink)
if r.status_code != 200 or not r.content:
logging.error("Sorry, no file")
return False
from dropbox_api import send_to_dropbox
p = permalink.split('/')
return send_to_dropbox("_".join([p[-3], p[-1]]), raw_data=r.content)
except Exception as e:
logging.error("Could not pull file from Slack: [%s, %s]" % (type(e), e))
return False
if __name__ == "__main__":
res = False
print argv
if len(argv) >= 3:
if argv[1] == "request_file":
res = request_file_for_user(argv[2])
elif argv[1] == "send_file":
res = put_file_in_dropbox(argv[2])
exit(res)