-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcc_http.py
78 lines (66 loc) · 2.78 KB
/
cc_http.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
# Interface to the catchat server http requests
import requests
import json
import datetime
import hashlib
class cc_http(object):
server_url = 'http://107.150.59.253/'
username='james'
password='computer'
userid=0
def md5(self, fname):
hash_md5 = hashlib.md5()
with open(fname, "rb") as f:
for chunk in iter(lambda: f.read(4096), b""):
hash_md5.update(chunk)
return hash_md5.hexdigest()
def get_chat_from_id_range(self, chat_id, after=0, before=""):
r = requests.post(self.server_url + 'getchat.php', data={ 'chatroom': str(chat_id),
'rangetype': "id",
'after': str(after),
'before': str(before)
} )
return json.loads(r.text)
def say(self, chat_id, message):
now = datetime.datetime.now()
r = requests.post(self.server_url + 'writechat.php', data={ 'chatroom': str(chat_id),
'message':message,
'msg_written': str(now),
'message_type' : 0,
'creator_id' : self.userid
})
def say_picture(self, chat_id, hash):
now = datetime.datetime.now()
r = requests.post(self.server_url + 'writechat.php', data={ 'chatroom': str(chat_id),
'message':hash,
'msg_written': str(now),
'message_type' : 1,
'creator_id' : self.userid
})
def send_file(self, chat_id, filename):
md5_file = self.md5(filename)
now = datetime.datetime.now()
files = {'userfile':open(filename, 'rb')}
datas = { 'creator_id':self.userid,
'msg_written': str(now),
'chatroom': str(chat_id),
}
r = requests.post(self.server_url + 'writechatfile.php', data=datas, files=files)
def check_hash_exists(self, hash):
datas = { 'hash':hash
}
r = requests.post(self.server_url + "check_hash_exists.php", data=datas)
reply = json.loads(r.text)
return reply['exists']
def get_picture_thumbnail(self, hash):
r = requests.post(self.server_url + "thumbnails/" + hash)
if(r.headers['content-type'] == "image/png"):
open("thumbnails/" + hash, 'wb').write(r.content)
return True
else:
print "get_picture_thumbnail failed!, Status Code: " + str(r.status_code) #+ str(r.headers.items()) + "\n"
return r.status_code
# text/plain
# image/png
#CaseInsensitiveDict({'content-length': '42790', 'accept-ranges': 'bytes', 'server': 'Apache/2.4.6 (CentOS) OpenSSL/1.0.1e-fips mod_fcgid/2.3.9 PHP/5.4.16', 'last-modified': 'Mon, 18 Apr 2016 08:49:53 GMT', 'etag': '"a726-530be71b95ffb"', 'date': 'Mon, 18 Apr 2016 08:52:44 GMT', 'content-type': 'image/png'})
#http://azenguard.com/CatChat/thumbnails/cc9f2d22efdd1a8a161860a27eadc72f