-
Notifications
You must be signed in to change notification settings - Fork 6
/
jodelrest.py
156 lines (121 loc) · 5.23 KB
/
jodelrest.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
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
import string
import random
import json
import base64
import requests
__author__ = 'Jan'
'''
Locations are formatted like this.
location = {'latitude' : 0, 'longtitude' : 0, 'city' : 'City'}
'''
class RESTClient(object):
API_URL = 'https://api.go-tellm.com/api/v2'
BASE_HEADERS = {"Connection": "keep-alive",
"Accept-Encoding": "gzip",
"Content-Type": "application/json; charset=UTF-8"}
USER_AGENT = 'Jodel/65000 Dalvik/2.1.0 (Linux; U; Android 5.1.1; D6503 Build/23.4.A.1.232)'
def new_device_uid(self):
return ''.join(random.choice(string.ascii_lowercase + string.digits) for _ in range(63))
def get_access_token(self):
city = self.location['city']
latitude = self.location['latitude']
longtitude = self.location['longtitude']
payload = {"client_id": "81e8a76e-1e02-4d17-9ba0-8a7020261b26",
"device_uid": self.new_device_uid(),
"location":
{"loc_accuracy": 19.0,
"city": city,
"loc_coordinates":
{"lat": latitude,
"lng": longtitude},
"country": "DE"}
}
json_payload = json.dumps(payload)
return self.do_post("/users/", json_payload).json()
def get_karma(self):
return self.do_get("/users/karma").json()['karma']
def get_posts(self):
return self.do_get("/posts/").json()['posts']
def get_posts_raw(self):
return self.do_get("/posts/").content
def do_delete(self, url):
return requests.delete(url, headers=self.headers)
def set_pos(self, longtitude, latitude, city, country="DE"):
payload = {"location":
{"loc_accuracy": 19.0,
"city": city,
"loc_coordinates":
{"lat": latitude,
"lng": longtitude},
"country": country}
}
self.location = {'latitude' : latitude, 'longtitude' : longtitude, 'city' : city}
json_payload = json.dumps(payload)
return self.do_put("/users/place", json_payload).content
def post(self, text, country="DE", color="DD5F5F"):
city = self.location['city']
latitude = self.location['latitude']
longtitude = self.location['longtitude']
payload = {"color": color,
"location":
{"loc_accuracy": 10.0,
"city": city,
"loc_coordinates":
{"lat": latitude,
"lng": longtitude},
"country": country,
"name": "41"},
"message": text
}
json_payload = json.dumps(payload)
return self.do_post("/posts/", json_payload)
def delete(self, postid):
return self.do_delete('/posts/%s' % postid).content
def post_image(self, file, country="DE", color="DD5F5F"):
city = self.location['city']
latitude = self.location['latitude']
longtitude = self.location['longtitude']
with open(file, "rb") as image_file:
encoded_string = base64.b64encode(image_file.read())
payload = {"color": color,
"image": encoded_string,
"location":
{"loc_accuracy": 10.0,
"city": city,
"loc_coordinates":
{"lat": latitude,
"lng": longtitude},
"country": country,
"name": "41"},
"message": "photo"
}
json_payload = json.dumps(payload)
return self.do_post("/posts/", json_payload)
def get_my_posts(self):
return self.do_get("/posts/mine/").content
def upvote(self, postid):
return self.do_put("/posts/%s/upvote" % postid, None).json()
def downvote(self, postid):
return self.do_put("/posts/%s/downvote" % postid, None).json()
def new_acc(self):
access = self.get_access_token()
self.set_pos(self.location['longtitude'], self.location['latitude'], self.location['city'])
return "Bearer %s" % access['access_token']
def __init__(self, location, auth=None):
self.headers = dict(self.BASE_HEADERS)
self.headers['User-Agent'] = self.USER_AGENT
self.location = location
if auth is None:
auth = self.new_acc()
self.headers['Authorization'] = auth
self.auth = auth
def do_post(self, url, payload):
return requests.post("%s%s" % (self.API_URL, url), data=payload, headers=self.headers)
def do_get(self, url):
return requests.get("%s%s" % (self.API_URL, url), headers=self.headers)
def do_put(self, url, payload=None):
return requests.put("%s%s" % (self.API_URL, url), data=payload, headers=self.headers)
def do_post(self, url, payload):
return requests.post("%s%s" % (self.API_URL, url), data=payload, headers=self.headers)
def close(self):
requests.session().close()