Skip to content

Commit c2d793b

Browse files
committed
Improves CLI for python API to add GUI
1 parent 6eed79a commit c2d793b

File tree

1 file changed

+90
-46
lines changed

1 file changed

+90
-46
lines changed

bin/loklak

+90-46
Original file line numberDiff line numberDiff line change
@@ -4,50 +4,94 @@ import argparse
44
from loklak import Loklak
55
from pprint import pprint
66
import os
7+
def main():
8+
parser = argparse.ArgumentParser()
79

8-
parser = argparse.ArgumentParser(description="Python wrapper around the loklak API.")
9-
parser.add_argument('-s', '--search', nargs='+', help='Search API Wrapper which helps to query loklak for JSON results.')
10-
parser.add_argument('-t', '--status', action='store_true', help='Status API Wrapper for the loklak status check.')
11-
parser.add_argument('-st', '--suggest', nargs='+', help='Suggestions API Wrapper , Works better with local loklak instance.')
12-
parser.add_argument('-c', '--crawler', action='store_true', help='Crawler API Wrapper on Loklak to crawl for tweets for a particular crawl depth.')
13-
parser.add_argument('-hl', '--hello', action='store_true', help='Loklak status check API.')
14-
parser.add_argument('-g', '--geocode', help='Geocode API for geolocation based information.')
15-
parser.add_argument('-p', '--peers', action='store_true', help='Loklak API for peers connected on the distributed network.')
16-
parser.add_argument('-pg', '--pushgeojson', action='store_true', help='Public API to push geojson objects to the loklak server.')
17-
parser.add_argument('-u', '--user', help='User API to show twitter user information.')
18-
parser.add_argument('-m', '--map', help='Map Visualization render using Loklak service.')
19-
parser.add_argument('-md', '--markdown', help='Markdown conversion API to render markdown as image using Loklak.')
20-
args = parser.parse_args()
21-
22-
loklak = Loklak()
23-
if args.search:
24-
pprint(loklak.search(args.search))
25-
elif args.status:
26-
pprint(loklak.status())
27-
elif args.suggest:
28-
print(args.suggest)
29-
pprint(loklak.suggest(args.suggest))
30-
elif args.crawler:
31-
pass
32-
elif args.hello:
33-
pprint(loklak.hello())
34-
elif args.geocode:
35-
pprint(loklak.geocode(args.geocode.split(', ')))
36-
elif args.peers:
37-
pprint(loklak.peers())
38-
elif args.pushgeojson:
39-
pass
40-
elif args.user:
41-
params = args.user.split()
42-
pprint(loklak.search(*params))
43-
elif args.map:
44-
data = loklak.get_map(args.map.split(',')[0],
45-
args.map.split(',')[1].strip())
46-
with open(os.path.join(os.getcwd(), 'map.png'), 'wb') as f:
47-
f.write(data)
48-
elif args.markdown:
49-
data = loklak.get_markdown(args.markdown)
50-
with open(os.path.join(os.getcwd(), 'markdown.png'), 'wb') as f:
51-
f.write(data)
52-
else:
53-
print('Choose API method.')
10+
subparsers = parser.add_subparsers(help='Python wrapper around the loklak API', dest='command')
11+
12+
search_parser = subparsers.add_parser('search', help='Search API Wrapper which helps to query loklak for JSON results.')
13+
search_parser.add_argument('query', action='store', help='Query term')
14+
search_parser.add_argument('--since', action='store', help='Only messages after the date (including the date) <date>=yyyy-MM-dd or yyyy-MM-dd_HH:mm')
15+
search_parser.add_argument('--until', action='store', help='Only messages before the date (excluding the date) <date>=yyyy-MM-dd or yyyy-MM-dd_HH:mm')
16+
search_parser.add_argument('--from_user', action='store', help='Only messages published by the user')
17+
search_parser.add_argument('--count', action='count', help='Result count')
18+
19+
status_parser = subparsers.add_parser('status', help='Status API Wrapper for the loklak status check.')
20+
status_parser.add_argument('--get_status', action='store_true', default="true", help='Get complete status')
21+
22+
suggest_parser = subparsers.add_parser('suggest', help='Suggestions API Wrapper , Works better with local loklak instance.')
23+
suggest_parser.add_argument('query', action='store', help='Query term')
24+
suggest_parser.add_argument('--since', action='store', help='Only messages after the date (including the date) <date>=yyyy-MM-dd or yyyy-MM-dd_HH:mm')
25+
suggest_parser.add_argument('--until', action='store', help='Only messages before the date (excluding the date) <date>=yyyy-MM-dd or yyyy-MM-dd_HH:mm')
26+
suggest_parser.add_argument('--order', choices=['asc', 'desc'], default='asc', help='Result count')
27+
suggest_parser.add_argument('--orderby', choices=['retrieval_next', 'query_count'], help='A field name of the query index schema')
28+
suggest_parser.add_argument('--count', action='count', help='Result count')
29+
30+
crawler_parser = subparsers.add_parser('crawler', help='Crawler API Wrapper on Loklak to crawl for tweets for a particular crawl depth.')
31+
crawler_parser.add_argument('--crawl', action='store_true', default="true", help='Get status of loklak API')
32+
33+
hello_parser = subparsers.add_parser('hello', help='Loklak status check API')
34+
hello_parser.add_argument('--get_hello_status', action='store_true', default="true", help='Get Loklak API health status')
35+
36+
geocode_parser = subparsers.add_parser('geocode', help='Geocode API for geolocation based information.')
37+
geocode_parser.add_argument('--places', action='store', help='<comma-separated strings>, a list of place names')
38+
39+
peer_parser = subparsers.add_parser('peers', help='Loklak API for peers connected on the distributed network.')
40+
peer_parser.add_argument('--get_peers', action='store_true', default="true", help='Get Loklak peers connected')
41+
42+
hello_parser = subparsers.add_parser('pushgeojson', help='Public API to push geojson objects to the loklak server')
43+
hello_parser.add_argument('--get_pushgeojson', action='store_true', default="true", help='Push objects')
44+
45+
user_parser = subparsers.add_parser('user', help='User API to show twitter user information.')
46+
user_parser.add_argument('screen_name', action='store', help='Twitter screen name of the user')
47+
user_parser.add_argument('--followers', action='store', help='Followers of the user')
48+
user_parser.add_argument('--following', action='store', help='Accounts the user is following')
49+
50+
map_parser = subparsers.add_parser('map', help='Map Visualization render using Loklak service.')
51+
map_parser.add_argument('latitude', action='store', help='Latitude value')
52+
map_parser.add_argument('longitude', action='store', help='Longitude value')
53+
map_parser.add_argument('--width', action='store', default='500', help='Width')
54+
map_parser.add_argument('--height', action='store', default='500', help='Height')
55+
map_parser.add_argument('--zoom', action='count', default='8', help='Zoom value')
56+
map_parser.add_argument('text', action='store', help='Value of text like Hello')
57+
58+
markdown_parser = subparsers.add_parser('markdown', help='Markdown conversion API to render markdown as image using Loklak.')
59+
markdown_parser.add_argument('text', action='store', help='Text to be printed, markdown possible')
60+
markdown_parser.add_argument('--color_text', action='store', default="000000", help='6-character hex code for the color')
61+
markdown_parser.add_argument('--color_background', action='store', default="ffffff", help='6-character hex code for the color')
62+
markdown_parser.add_argument('--padding', action='count', default="10", help='Space around text')
63+
markdown_parser.add_argument('--uppercase', action='store_true', default="true", help='If true the text is printed UPPERCASE')
64+
65+
args = parser.parse_args()
66+
loklak = Loklak()
67+
if args.command == "search":
68+
pprint(loklak.search(args.query, args.since, args.until, args.from_user, args.count))
69+
elif args.command == "status":
70+
pprint(loklak.status())
71+
elif args.command == "suggest":
72+
pprint(loklak.suggest(args.query, args.count, args.order, args.orderby, args.since, args.until))
73+
elif args.command == "crawler":
74+
pass
75+
elif args.command == "hello":
76+
pprint(loklak.hello())
77+
elif args.command == "geocode":
78+
pprint(loklak.geocode(args.places.split(', ')))
79+
elif args.command == "peers":
80+
pprint(loklak.peers())
81+
elif args.command == "pushgeojson":
82+
pass
83+
elif args.command == "user":
84+
pprint(loklak.search(args.screen_name.split(), args.followers, args.following))
85+
elif args.command == "map":
86+
data = loklak.get_map(args.latitude, args.longitude, args.width, args.height, args.zoom, args.text)
87+
with open(os.path.join(os.getcwd(), 'map.png'), 'wb') as f:
88+
f.write(data)
89+
elif args.command == "markdown":
90+
data = loklak.get_markdown(args.text, args.color_text, args.color_background, args.padding, args.uppercase)
91+
with open(os.path.join(os.getcwd(), 'markdown.png'), 'wb') as f:
92+
f.write(data)
93+
else:
94+
print('Choose API method.')
95+
96+
if __name__ == '__main__':
97+
main()

0 commit comments

Comments
 (0)