1
1
#!/usr/bin/env python
2
2
# encoding: utf-8
3
- """The module that handles the main interface of loklak"""
3
+ """The module that handles the main interface of loklak. """
4
4
from __future__ import (absolute_import , division ,
5
5
print_function , unicode_literals )
6
6
13
13
14
14
15
15
class Loklak (object ):
16
- """The fields for the Loklak object"""
16
+ """The fields for the Loklak object.
17
+
18
+ Additionaly, it contains methods that can be used for accessing
19
+ the Loklak API services like geocode, markdown, etc.
20
+
21
+ """
22
+
17
23
baseUrl = 'http://loklak.org/'
18
24
name = None
19
25
followers = None
@@ -31,6 +37,12 @@ class Loklak(object):
31
37
data = {}
32
38
33
39
def __init__ (self , baseUrl = 'http://loklak.org/' ):
40
+ """Constructor of the Loklak class.
41
+
42
+ Args:
43
+ baseUrl (str): Base URL for accessing the APIs (default=http://loklak.org).
44
+
45
+ """
34
46
baseUrl = re .findall ('http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+' , baseUrl )
35
47
try :
36
48
if baseUrl [0 ]:
@@ -44,10 +56,11 @@ def __init__(self, baseUrl='http://loklak.org/'):
44
56
pass
45
57
46
58
def getBaseUrl (self ):
59
+ """Return the string value of baseUrl."""
47
60
return self .baseUrl
48
61
49
62
def status (self ):
50
- """Returns the status of the server"""
63
+ """Retrieve a json response about the status of the server. """
51
64
status_application = 'api/status.json'
52
65
url_to_give = self .baseUrl + status_application
53
66
return_to_user = requests .get (url_to_give )
@@ -58,22 +71,22 @@ def status(self):
58
71
return json .dumps (return_to_user )
59
72
60
73
def xmlToJson (self , xmlData = None ):
61
- """Converts XML to JSON as the service"""
74
+ """Convert XML to JSON as the service. """
62
75
jsonData = ''
63
76
if xmlData :
64
77
jsonData = dumps (bf .data (fromstring (xmlData )))
65
78
return jsonData
66
79
67
80
def csvToJson (self , csvData = None , fieldnamesList = None ):
68
- """Converts CSV to JSON as the service"""
81
+ """Convert CSV to JSON as the service. """
69
82
jsonData = ''
70
83
if csvData :
71
84
data = csv .DictReader ( csvData , fieldnames = fieldnamesList )
72
85
jsonData = json .dumps ( [ row for row in data ] )
73
86
return out
74
87
75
88
def hello (self ):
76
- """Gives a hello """
89
+ """Retrieve a json response about the basic status of the server. """
77
90
hello_application = 'api/hello.json'
78
91
url_to_give = self .baseUrl + hello_application
79
92
return_to_user = requests .get (url_to_give )
@@ -84,7 +97,23 @@ def hello(self):
84
97
return json .dumps (return_to_user )
85
98
86
99
def geocode (self , places = None ):
87
- """Gives the geocode"""
100
+ """Provide geocoding of place names to location coordinates.
101
+
102
+ Args:
103
+ places (list): A list of place names.
104
+
105
+ Examples:
106
+ >>> l.geocode(['Barcelona'])
107
+ {u'locations': {u'Barcelona': {u'country': u'Spain',
108
+ u'country_code': u'ES',
109
+ u'location':[2.15898974899153,
110
+ 41.38879005861875],
111
+ ...
112
+
113
+ Returns:
114
+ json: The geocoding results based on the given place(s) name.
115
+
116
+ """
88
117
geo_application = 'api/geocode.json'
89
118
url_to_give = self .baseUrl + geo_application
90
119
params = {}
@@ -98,7 +127,20 @@ def geocode(self, places=None):
98
127
99
128
def get_map (self , latitude , longitude , width = 500 , height = 500 ,
100
129
zoom = 8 , text = "" ):
101
- """Returns a map of size 500x500"""
130
+ """Visualize map using Loklak service.
131
+
132
+ Args:
133
+ latitude (float): Latitude value.
134
+ longtitude (float): Longitude value.
135
+ width (int): Width (default=500).
136
+ height (int): Height (default=500).
137
+ zoom (int): Zoom value (default=8).
138
+ text (str): Value of text like Hello.
139
+
140
+ Returns:
141
+ bytes: An encoded map image.
142
+
143
+ """
102
144
map_application = 'vis/map.png'
103
145
params = {'text' : text , 'mlat' : latitude , 'mlon' : longitude ,
104
146
'width' : width , 'height' : height , 'zoom' : zoom }
@@ -111,7 +153,19 @@ def get_map(self, latitude, longitude, width=500, height=500,
111
153
112
154
def get_markdown (self , text , color_text = "000000" , color_bg = "ffffff" ,
113
155
padding = "10" , uppercase = "true" ):
114
- """Returns a map of size 500x500"""
156
+ """Provide an image with text on it.
157
+
158
+ Args:
159
+ text (str): Text to be printed, markdown possible.
160
+ color_text: 6-character hex code for the color.
161
+ color_bg: 6-character hex code for the color.
162
+ padding: Space around text.
163
+ uppercase: <true|false> by default true. If true the text is printed UPPERCASE.
164
+
165
+ Returns:
166
+ bytes: An encoded image.
167
+
168
+ """
115
169
map_application = 'vis/markdown.png'
116
170
params = {'text' : text , 'color_text' : color_text , 'color_background' : color_bg ,
117
171
'padding' : padding , 'uppercase' : uppercase }
@@ -123,7 +177,7 @@ def get_markdown(self, text, color_text="000000", color_bg="ffffff",
123
177
return ''
124
178
125
179
def peers (self ):
126
- """Gives the peers of a user"""
180
+ """Retrieve the peers of a user. """
127
181
peers_application = 'api/peers.json'
128
182
url_to_give = self .baseUrl + peers_application
129
183
return_to_user = requests .get (url_to_give )
@@ -134,7 +188,19 @@ def peers(self):
134
188
return json .dumps (return_to_user )
135
189
136
190
def push (self , data = None ):
137
- """Push servlet for twitter like messages"""
191
+ """Push servlet for twitter like messages.
192
+
193
+ Note:
194
+ The API of this function has a restrictions
195
+ which only localhost clients are granted.
196
+
197
+ Args:
198
+ data: A search result object.
199
+
200
+ Returns:
201
+ json: Status about the message is pushed or not.
202
+
203
+ """
138
204
push_application = 'api/push.json'
139
205
url_to_give = self .baseUrl + push_application
140
206
headers = {
@@ -162,8 +228,17 @@ def push(self, data=None):
162
228
163
229
164
230
def user (self , name = None , followers = None , following = None ):
165
- """User information, including who they are following, and
166
- who follows them"""
231
+ """Retrieve Twitter user information.
232
+
233
+ Args:
234
+ name (str): Twitter screen name of the user.
235
+ followers (int): Followers of the user.
236
+ following (int): Accounts the user is following.
237
+
238
+ Returns:
239
+ json: User information, including who they are following, and who follows them.
240
+
241
+ """
167
242
user_application = 'api/user.json'
168
243
url_to_give = self .baseUrl + user_application
169
244
self .name = name
@@ -190,7 +265,16 @@ def user(self, name=None, followers=None, following=None):
190
265
return json .dumps (return_to_user )
191
266
192
267
def settings (self ):
193
- """Gives the settings of the application"""
268
+ """Retrieve the settings of the application.
269
+
270
+ Note:
271
+ The API of this function has a restrictions
272
+ which only localhost clients are granted.
273
+
274
+ Returns:
275
+ json: The settings of the application.
276
+
277
+ """
194
278
settings_application = 'api/settings.json'
195
279
url_to_give = self .baseUrl + settings_application
196
280
return_to_user = requests .get (url_to_give )
@@ -203,7 +287,15 @@ def settings(self):
203
287
return json .dumps (return_to_user )
204
288
205
289
def susi (self , query = None ):
206
- """Hits Susi with the required query and returns back the susi response"""
290
+ """Retrieve Susi query response.
291
+
292
+ Args:
293
+ query (str): Query term.
294
+
295
+ Returns:
296
+ json: Susi response.
297
+
298
+ """
207
299
susi_application = 'api/susi.json'
208
300
url_to_give = self .baseUrl + susi_application
209
301
self .query = query
@@ -223,7 +315,19 @@ def susi(self, query=None):
223
315
return json .dumps (return_to_user )
224
316
225
317
def search (self , query = None , since = None , until = None , from_user = None , count = None ):
226
- """Handles the searching"""
318
+ """Handle the searching.
319
+
320
+ Args:
321
+ query (str): Query term.
322
+ since (str): Only messages after the date (including the date), <date>=yyyy-MM-dd or yyyy-MM-dd_HH:mm.
323
+ until (str): Only messages before the date (excluding the date), <date>=yyyy-MM-dd or yyyy-MM-dd_HH:mm.
324
+ from_user (str): Only messages published by the user.
325
+ count (int): The wanted number of results.
326
+
327
+ Returns:
328
+ json: Search results from API.
329
+
330
+ """
227
331
search_application = 'api/search.json'
228
332
url_to_give = self .baseUrl + search_application
229
333
self .query = query
@@ -257,6 +361,20 @@ def search(self, query=None, since=None, until=None, from_user=None, count=None)
257
361
return json .dumps (return_to_user )
258
362
259
363
def suggest (self , query = None , count = None , order = None , orderby = None ,since = None , until = None ):
364
+ """Retrieve a list of queries based on the given criteria.
365
+
366
+ Args:
367
+ query (str): To get a list of queries which match; to get all latest: leave query empty.
368
+ count (int): Number of queries.
369
+ order (str): Possible values: desc, asc; default: desc.
370
+ orderby (str): A field name of the query index schema, i.e. retrieval_next or query_count.
371
+ since (str): Left bound for a query time.
372
+ until (str): Right bound for a query time.
373
+
374
+ Returns:
375
+ json: A list of queries in the given order.
376
+
377
+ """
260
378
suggest_application = 'api/suggest.json'
261
379
url_to_give = self .baseUrl + suggest_application
262
380
params = {}
@@ -283,7 +401,20 @@ def suggest(self, query=None, count=None, order=None, orderby=None,since=None, u
283
401
284
402
def aggregations (self , query = None , since = None , until = None ,
285
403
fields = None , limit = 6 , count = 0 ):
286
- """Gives the aggregations of the application"""
404
+ """Give the aggregations of the application.
405
+
406
+ Args:
407
+ query (str): Query term.
408
+ since (str): Only messages after the date (including the date), <date>=yyyy-MM-dd or yyyy-MM-dd_HH:mm.
409
+ until (str): Only messages before the date (excluding the date), <date>=yyyy-MM-dd or yyyy-MM-dd_HH:mm.
410
+ fields (str): Aggregation fields for search facets, like "created_at,mentions".
411
+ limit (int): A limitation of number of facets for each aggregation.
412
+ count (int): The wanted number of results.
413
+
414
+ Returns:
415
+ json: Aggregations of the application.
416
+
417
+ """
287
418
aggregations_application = 'api/search.json'
288
419
url_to_give = self .baseUrl + aggregations_application
289
420
self .query = query
@@ -322,7 +453,38 @@ def aggregations(self, query=None, since=None, until=None,
322
453
return json .dumps (return_to_user )
323
454
324
455
def account (self , name = None , action = None , data = None ):
325
- """Displays users account"""
456
+ """Provide the storage and retrieval of the user account data.
457
+
458
+ Note:
459
+ The API of this function has a restrictions which only localhost
460
+ clients are granted. If you want to retrieve the user account
461
+ information, just fill the 'name' argument, and do not fill any
462
+ args.
463
+
464
+ If you want to store one or more account objects, fill the
465
+ 'action' argument with "update" then submit that object
466
+ (these objects) inside the 'data' argument.
467
+
468
+ The data object must have this following form:
469
+ {
470
+ "screen_name" : "test", // primary key for the user
471
+ "source_type" : "TWITTER", // the application which created the token, by default "TWITTER"
472
+ "oauth_token" : "abc", // the oauth token
473
+ "oauth_token_secret" : "def", // the oauth token_secret
474
+ "authentication_first" : "2015-06-07T09:39:22.341Z", // optional
475
+ "authentication_latest" : "2015-06-07T09:39:22.341Z", // optional
476
+ "apps" : {"wall" : {"type" : "horizontal"}} // any json
477
+ }
478
+
479
+ Args:
480
+ name (str): Twitter screen name of the user.
481
+ action (str): Proper values are either <empty> or 'update'.
482
+ data (str): An object to store (if you set action=update, you must submit this data object).
483
+
484
+ Returns:
485
+ json: Information about user's account if the 'action' argument is empty (retrieving).
486
+
487
+ """
326
488
account_application = 'account.json'
327
489
url_to_give = 'http://localhost:9000/api/' + account_application
328
490
self .name = name
0 commit comments