|
1 | | -import requests |
2 | 1 | import json |
| 2 | +import requests |
3 | 3 |
|
4 | 4 | #endpoints still to add. |
5 | 5 | # - Annotations don't seem to be implemented yet. Doesn't seem to work. |
|
8 | 8 | # - Filters (in dev by app.net) |
9 | 9 |
|
10 | 10 | class appdotnet: |
11 | | - ''' |
12 | | - Once access has been given, you don't have to pass through the client_id, client_secret, redirect_uri, or scope. |
13 | | - These are just to get the authentication token. |
14 | | -
|
15 | | - Once authenticated, you can initialise appdotnet with only the access token: ie |
16 | | -
|
17 | | - api = appdotnet(access_token='<insert token here>') |
18 | | - ''' |
19 | | - |
20 | | - def __init__(self,client_id=None,client_secret=None,redirect_uri=None,scope=None,access_token=None): |
21 | | - #for server authentication flow |
22 | | - self.client_id = client_id |
23 | | - self.client_secret = client_secret |
24 | | - self.redirect_uri = redirect_uri |
25 | | - self.scope = scope |
26 | | - |
27 | | - self.access_token = access_token |
28 | | - |
29 | | - self.api_anchor = "alpha.app.net" #for when the versions change |
30 | | - #anchors currently different |
31 | | - self.public_api_anchor = "alpha-api.app.net" #for when the versions change |
32 | | - |
33 | | - #scopes provided by app.net API |
34 | | - self.allowed_scopes = ['stream','email','write_post','follow','messages','export'] |
35 | | - |
36 | | - def generateAuthUrl(self): |
37 | | - url = "https://"+self.api_anchor+"/oauth/authenticate?client_id="+self.client_id+"&response_type=code&redirect_uri="+self.redirect_uri+"&scope=" |
38 | | - |
39 | | - for scope in self.scope: |
40 | | - if scope in self.allowed_scopes: |
41 | | - url += scope + " " |
42 | | - |
43 | | - return url |
44 | | - |
45 | | - def getAuthResponse(self,code): |
46 | | - #generate POST request |
47 | | - url = "https://alpha.app.net/oauth/access_token" |
48 | | - post_data = {'client_id':self.client_id, |
49 | | - 'client_secret':self.client_secret, |
50 | | - 'grant_type':'authorization_code', |
51 | | - 'redirect_uri':self.redirect_uri, |
52 | | - 'code':code} |
53 | | - |
54 | | - r = requests.post(url,data=post_data) |
55 | | - |
56 | | - return r.text |
57 | | - |
58 | | - ''' |
59 | | - API Calls |
60 | | - ''' |
61 | | - |
62 | | - #GET REQUESTS |
63 | | - def getRequest(self,url): |
64 | | - #access token |
65 | | - url = url+"?access_token="+self.access_token |
66 | | - r = requests.get(url) |
67 | | - if r.status_code == requests.codes.ok: |
68 | | - return r.text |
69 | | - else: |
70 | | - j = json.loads(r.text) |
71 | | - return "{'error_code':"+(str)(r.status_code)+",'message':'"+j['error']['message']+"'}" |
72 | | - |
73 | | - |
74 | | - def getUser(self,user_id): |
75 | | - url = "https://"+self.public_api_anchor+"/stream/0/users/"+user_id |
76 | | - return self.getRequest(url) |
77 | | - |
78 | | - def getUserPosts(self,user_id): |
79 | | - url = "https://"+self.public_api_anchor+"/stream/0/users/"+user_id+"/posts" |
80 | | - return self.getRequest(url) |
81 | | - |
82 | | - def getGlobalStream(self): |
83 | | - url = "https://"+self.public_api_anchor+"/stream/0/posts/stream/global" |
84 | | - return self.getRequest(url) |
85 | | - |
86 | | - def getUserStream(self): |
87 | | - url = "https://"+self.public_api_anchor+"/stream/0/posts/stream" |
88 | | - return self.getRequest(url) |
89 | | - |
90 | | - def getUserMentions(self,user_id): |
91 | | - url = "https://"+self.public_api_anchor+"/stream/0/users/"+user_id+"/mentions" |
92 | | - return self.getRequest(url) |
93 | | - |
94 | | - def getPost(self,post_id): |
95 | | - url = "https://"+self.public_api_anchor+"/stream/0/posts/"+post_id |
96 | | - return self.getRequest(url) |
97 | | - |
98 | | - def getPostReplies(self,post_id): |
99 | | - url = "https://"+self.public_api_anchor+"/stream/0/posts/"+post_id+"/replies" |
100 | | - return self.getRequest(url) |
101 | | - |
102 | | - def getPostsByTag(self,tag): |
103 | | - url = "https://"+self.public_api_anchor+"/stream/0/posts/tag/"+tag |
104 | | - return self.getRequest(url) |
105 | | - |
106 | | - def getUserFollowing(self,user_id): |
107 | | - url = "https://"+self.public_api_anchor+"/stream/0/users/"+user_id+"/following" |
108 | | - return self.getRequest(url) |
109 | | - |
110 | | - def getUserFollowers(self,user_id): |
111 | | - url = "https://"+self.public_api_anchor+"/stream/0/users/"+user_id+"/followers" |
112 | | - return self.getRequest(url) |
113 | | - |
114 | | - #POST REQUESTS |
115 | | - def postRequest(self,url,data={}): |
116 | | - url = url |
117 | | - data['access_token'] = self.access_token |
118 | | - r = requests.post(url,data=data) |
119 | | - if r.status_code == requests.codes.ok: |
120 | | - return r.text |
121 | | - else: |
122 | | - try: |
123 | | - j = json.loads(r.text) |
124 | | - return "{'error_code':"+(str)(r.status_code)+",'message':'"+j['error']['message']+"'}" |
125 | | - except: #generic error |
126 | | - print r.text |
127 | | - return "{'error':'There was an error'}" |
128 | | - |
129 | | - |
130 | | - def followUser(self,user_id): |
131 | | - url = "https://"+self.public_api_anchor+"/stream/0/users/"+user_id+"/follow" |
132 | | - return self.postRequest(url) |
133 | | - |
134 | | - #requires: text |
135 | | - #optional: reply_to, annotations, links |
136 | | - def createPost(self,text,reply_to=None,annotations=None,links=None): |
137 | | - url = "https://"+self.public_api_anchor+"/stream/0/posts" |
138 | | - data = {'text':text} |
139 | | - if reply_to != None: |
140 | | - data['reply_to'] = reply_to |
141 | | - if annotations != None: |
142 | | - data['annotations'] = annotations |
143 | | - if links != None: |
144 | | - data['links'] = links |
145 | | - |
146 | | - return self.postRequest(url,data) |
147 | | - |
148 | | - |
149 | | - #DELETE request |
150 | | - def deleteRequest(self,url): |
151 | | - url = url+"?access_token="+self.access_token |
152 | | - r = requests.delete(url) |
153 | | - if r.status_code == requests.codes.ok: |
154 | | - return r.text |
155 | | - else: |
156 | | - try: |
157 | | - j = json.loads(r.text) |
158 | | - return "{'error_code':"+(str)(r.status_code)+",'message':'"+j['error']['message']+"'}" |
159 | | - except: #generic error |
160 | | - print r.text |
161 | | - return "{'error':'There was an error'}" |
162 | | - |
163 | | - |
164 | | - def deletePost(self,post_id): |
165 | | - url = "https://"+self.public_api_anchor+"/stream/0/posts/"+post_id |
166 | | - return self.deleteRequest(url) |
167 | | - |
168 | | - def unfollowUser(self,user_id): |
169 | | - url = "https://"+self.public_api_anchor+"/stream/0/users/"+user_id+"/follow" |
170 | | - return self.deleteRequest(url) |
171 | | - |
172 | | - |
173 | | - |
| 11 | + ''' Once access has been given, you don't have to pass through the |
| 12 | + client_id, client_secret, redirect_uri, or scope. These are just |
| 13 | + to get the authentication token. |
| 14 | +
|
| 15 | + Once authenticated, you can initialise appdotnet with only the |
| 16 | + access token: ie |
| 17 | +
|
| 18 | + api = appdotnet(access_token='<insert token here>') |
| 19 | + ''' |
| 20 | + |
| 21 | + def __init__(self, client_id=None, client_secret=None, redirect_uri=None, |
| 22 | + scope=None, access_token=None): |
| 23 | + #for server authentication flow |
| 24 | + self.client_id = client_id |
| 25 | + self.client_secret = client_secret |
| 26 | + self.redirect_uri = redirect_uri |
| 27 | + self.scope = scope |
| 28 | + |
| 29 | + self.access_token = access_token |
| 30 | + |
| 31 | + self.api_anchor = "alpha.app.net" #for when the versions change |
| 32 | + #anchors currently different |
| 33 | + self.public_api_anchor = "alpha-api.app.net" |
| 34 | + |
| 35 | + #scopes provided by app.net API |
| 36 | + self.allowed_scopes = ['stream', 'email', 'write_post', |
| 37 | + 'follow', 'messages','export'] |
| 38 | + |
| 39 | + def generateAuthUrl(self): |
| 40 | + url = "https://" + self.api_anchor + "/oauth/authenticate?client_id="+\ |
| 41 | + self.client_id + "&response_type=code&redirect_uri=" +\ |
| 42 | + self.redirect_uri + "&scope=" |
| 43 | + |
| 44 | + for scope in self.scope: |
| 45 | + if scope in self.allowed_scopes: |
| 46 | + url += scope + " " |
| 47 | + |
| 48 | + return url |
| 49 | + |
| 50 | + def getAuthResponse(self, code): |
| 51 | + #generate POST request |
| 52 | + url = "https://alpha.app.net/oauth/access_token" |
| 53 | + post_data = {'client_id':self.client_id, |
| 54 | + 'client_secret':self.client_secret, |
| 55 | + 'grant_type':'authorization_code', |
| 56 | + 'redirect_uri':self.redirect_uri, |
| 57 | + 'code':code} |
| 58 | + |
| 59 | + r = requests.post(url,data=post_data) |
| 60 | + |
| 61 | + return r.text |
| 62 | + |
| 63 | + ''' |
| 64 | + API Calls |
| 65 | + ''' |
| 66 | + |
| 67 | + #GET REQUESTS |
| 68 | + def getRequest(self, url): |
| 69 | + #access token |
| 70 | + url = url + "?access_token=" + self.access_token |
| 71 | + r = requests.get(url) |
| 72 | + if r.status_code == requests.codes.ok: |
| 73 | + return r.text |
| 74 | + else: |
| 75 | + j = json.loads(r.text) |
| 76 | + resp = {'error_code': r.status_code, |
| 77 | + 'message' : j['error']['message']} |
| 78 | + return json.dumps(resp) |
| 79 | + |
| 80 | + |
| 81 | + def getUser(self, user_id): |
| 82 | + url = "https://%s/stream/0/users/%i" % (self.public_api_anchor, |
| 83 | + user_id) |
| 84 | + return self.getRequest(url) |
| 85 | + |
| 86 | + def getUserPosts(self, user_id): |
| 87 | + url = "https://%s/stream/0/users/%i/posts" % (self.public_api_anchor, |
| 88 | + user_id) |
| 89 | + return self.getRequest(url) |
| 90 | + |
| 91 | + def getGlobalStream(self): |
| 92 | + url = "https://%s/stream/0/posts/stream/global" % self.public_api_anchor |
| 93 | + return self.getRequest(url) |
| 94 | + |
| 95 | + def getUserStream(self): |
| 96 | + url = "https://%s/stream/0/posts/stream" % self.public_api_anchor |
| 97 | + return self.getRequest(url) |
| 98 | + |
| 99 | + def getUserMentions(self, user_id): |
| 100 | + url = "https://"+self.public_api_anchor+"/stream/0/users/"+user_id+"/mentions" |
| 101 | + return self.getRequest(url) |
| 102 | + |
| 103 | + def getPost(self, post_id): |
| 104 | + url = "https://"+self.public_api_anchor+"/stream/0/posts/"+post_id |
| 105 | + return self.getRequest(url) |
| 106 | + |
| 107 | + def getPostReplies(self, post_id): |
| 108 | + url = "https://"+self.public_api_anchor+"/stream/0/posts/"+post_id+"/replies" |
| 109 | + return self.getRequest(url) |
| 110 | + |
| 111 | + def getPostsByTag(self, tag): |
| 112 | + url = "https://"+self.public_api_anchor+"/stream/0/posts/tag/"+tag |
| 113 | + return self.getRequest(url) |
| 114 | + |
| 115 | + def getUserFollowing(self, user_id): |
| 116 | + url = "https://"+self.public_api_anchor+"/stream/0/users/"+user_id+"/following" |
| 117 | + return self.getRequest(url) |
| 118 | + |
| 119 | + def getUserFollowers(self, user_id): |
| 120 | + url = "https://"+self.public_api_anchor+"/stream/0/users/"+user_id+"/followers" |
| 121 | + return self.getRequest(url) |
| 122 | + |
| 123 | + #POST REQUESTS |
| 124 | + url = url |
| 125 | + data['access_token'] = self.access_token |
| 126 | + r = requests.post(url,data=data) |
| 127 | + if r.status_code == requests.codes.ok: |
| 128 | + return r.text |
| 129 | + else: |
| 130 | + try: |
| 131 | + j = json.loads(r.text) |
| 132 | + resp = {'error_code': r.status_code, |
| 133 | + 'message' : j['error']['message']} |
| 134 | + return resp |
| 135 | + except: #generic error |
| 136 | + print r.text |
| 137 | + return "{'error':'There was an error'}" |
| 138 | + |
| 139 | + |
| 140 | + def followUser(self,user_id): |
| 141 | + url = "https://" + self.public_api_anchor + "/stream/0/users/" +\ |
| 142 | + user_id + "/follow" |
| 143 | + |
| 144 | + return self.postRequest(url) |
| 145 | + |
| 146 | + #requires: text |
| 147 | + #optional: reply_to, annotations, links |
| 148 | + def createPost(self, text, reply_to = None, annotations=None, links=None): |
| 149 | + url = "https://"+self.public_api_anchor+"/stream/0/posts" |
| 150 | + data = {'text':text} |
| 151 | + if reply_to != None: |
| 152 | + data['reply_to'] = reply_to |
| 153 | + if annotations != None: |
| 154 | + data['annotations'] = annotations |
| 155 | + if links != None: |
| 156 | + data['links'] = links |
| 157 | + |
| 158 | + return self.postRequest(url,data) |
| 159 | + |
| 160 | + #DELETE request |
| 161 | + def deleteRequest(self, url): |
| 162 | + url = url + "?access_token=" + self.access_token |
| 163 | + r = requests.delete(url) |
| 164 | + if r.status_code == requests.codes.ok: |
| 165 | + return r.text |
| 166 | + else: |
| 167 | + try: |
| 168 | + j = json.loads(r.text) |
| 169 | + resp = {'error_code': r.status_code, |
| 170 | + 'message' : j['error']['message']} |
| 171 | + return resp |
| 172 | + except: #generic error |
| 173 | + print r.text |
| 174 | + return "{'error':'There was an error'}" |
| 175 | + |
| 176 | + def deletePost(self, post_id): |
| 177 | + url = "https://"+self.public_api_anchor+"/stream/0/posts/"+post_id |
| 178 | + return self.deleteRequest(url) |
| 179 | + |
| 180 | + def unfollowUser(self, user_id): |
| 181 | + url = "https://"+self.public_api_anchor+"/stream/0/users/"+user_id+"/follow" |
| 182 | + return self.deleteRequest(url) |
174 | 183 |
|
0 commit comments