11#!/usr/bin/env python
22# encoding: utf-8
3- """The module that handles the main interface of loklak"""
3+ """The module that handles the main interface of loklak. """
44from __future__ import (absolute_import , division ,
55 print_function , unicode_literals )
66
1313
1414
1515class 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+
1723 baseUrl = 'http://loklak.org/'
1824 name = None
1925 followers = None
@@ -31,6 +37,12 @@ class Loklak(object):
3137 data = {}
3238
3339 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+ """
3446 baseUrl = re .findall ('http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+' , baseUrl )
3547 try :
3648 if baseUrl [0 ]:
@@ -44,10 +56,11 @@ def __init__(self, baseUrl='http://loklak.org/'):
4456 pass
4557
4658 def getBaseUrl (self ):
59+ """Return the string value of baseUrl."""
4760 return self .baseUrl
4861
4962 def status (self ):
50- """Returns the status of the server"""
63+ """Retrieve a json response about the status of the server. """
5164 status_application = 'api/status.json'
5265 url_to_give = self .baseUrl + status_application
5366 return_to_user = requests .get (url_to_give )
@@ -58,22 +71,22 @@ def status(self):
5871 return json .dumps (return_to_user )
5972
6073 def xmlToJson (self , xmlData = None ):
61- """Converts XML to JSON as the service"""
74+ """Convert XML to JSON as the service. """
6275 jsonData = ''
6376 if xmlData :
6477 jsonData = dumps (bf .data (fromstring (xmlData )))
6578 return jsonData
6679
6780 def csvToJson (self , csvData = None , fieldnamesList = None ):
68- """Converts CSV to JSON as the service"""
81+ """Convert CSV to JSON as the service. """
6982 jsonData = ''
7083 if csvData :
7184 data = csv .DictReader ( csvData , fieldnames = fieldnamesList )
7285 jsonData = json .dumps ( [ row for row in data ] )
7386 return out
7487
7588 def hello (self ):
76- """Gives a hello """
89+ """Retrieve a json response about the basic status of the server. """
7790 hello_application = 'api/hello.json'
7891 url_to_give = self .baseUrl + hello_application
7992 return_to_user = requests .get (url_to_give )
@@ -84,7 +97,23 @@ def hello(self):
8497 return json .dumps (return_to_user )
8598
8699 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+ """
88117 geo_application = 'api/geocode.json'
89118 url_to_give = self .baseUrl + geo_application
90119 params = {}
@@ -98,7 +127,20 @@ def geocode(self, places=None):
98127
99128 def get_map (self , latitude , longitude , width = 500 , height = 500 ,
100129 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+ """
102144 map_application = 'vis/map.png'
103145 params = {'text' : text , 'mlat' : latitude , 'mlon' : longitude ,
104146 'width' : width , 'height' : height , 'zoom' : zoom }
@@ -111,7 +153,19 @@ def get_map(self, latitude, longitude, width=500, height=500,
111153
112154 def get_markdown (self , text , color_text = "000000" , color_bg = "ffffff" ,
113155 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+ """
115169 map_application = 'vis/markdown.png'
116170 params = {'text' : text , 'color_text' : color_text , 'color_background' : color_bg ,
117171 'padding' : padding , 'uppercase' : uppercase }
@@ -123,7 +177,7 @@ def get_markdown(self, text, color_text="000000", color_bg="ffffff",
123177 return ''
124178
125179 def peers (self ):
126- """Gives the peers of a user"""
180+ """Retrieve the peers of a user. """
127181 peers_application = 'api/peers.json'
128182 url_to_give = self .baseUrl + peers_application
129183 return_to_user = requests .get (url_to_give )
@@ -134,7 +188,19 @@ def peers(self):
134188 return json .dumps (return_to_user )
135189
136190 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+ """
138204 push_application = 'api/push.json'
139205 url_to_give = self .baseUrl + push_application
140206 headers = {
@@ -162,8 +228,17 @@ def push(self, data=None):
162228
163229
164230 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+ """
167242 user_application = 'api/user.json'
168243 url_to_give = self .baseUrl + user_application
169244 self .name = name
@@ -190,7 +265,16 @@ def user(self, name=None, followers=None, following=None):
190265 return json .dumps (return_to_user )
191266
192267 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+ """
194278 settings_application = 'api/settings.json'
195279 url_to_give = self .baseUrl + settings_application
196280 return_to_user = requests .get (url_to_give )
@@ -203,7 +287,15 @@ def settings(self):
203287 return json .dumps (return_to_user )
204288
205289 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+ """
207299 susi_application = 'api/susi.json'
208300 url_to_give = self .baseUrl + susi_application
209301 self .query = query
@@ -223,7 +315,19 @@ def susi(self, query=None):
223315 return json .dumps (return_to_user )
224316
225317 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+ """
227331 search_application = 'api/search.json'
228332 url_to_give = self .baseUrl + search_application
229333 self .query = query
@@ -257,6 +361,20 @@ def search(self, query=None, since=None, until=None, from_user=None, count=None)
257361 return json .dumps (return_to_user )
258362
259363 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+ """
260378 suggest_application = 'api/suggest.json'
261379 url_to_give = self .baseUrl + suggest_application
262380 params = {}
@@ -283,7 +401,20 @@ def suggest(self, query=None, count=None, order=None, orderby=None,since=None, u
283401
284402 def aggregations (self , query = None , since = None , until = None ,
285403 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+ """
287418 aggregations_application = 'api/search.json'
288419 url_to_give = self .baseUrl + aggregations_application
289420 self .query = query
@@ -322,7 +453,38 @@ def aggregations(self, query=None, since=None, until=None,
322453 return json .dumps (return_to_user )
323454
324455 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+ """
326488 account_application = 'account.json'
327489 url_to_give = 'http://localhost:9000/api/' + account_application
328490 self .name = name
0 commit comments