-
Notifications
You must be signed in to change notification settings - Fork 2
/
prototype.py
executable file
·61 lines (53 loc) · 1.8 KB
/
prototype.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
#! /usr/bin/env python3
import json
import uuid
import sys
from dateutil.parser import parse
from apis import index, ai
if len(sys.argv) != 2:
print("Unrecognized argument: %s." % " ".join(sys.argv[1:]))
print("Usage: %s QUERY")
exit(1)
else:
query = sys.argv[1]
print('Q: "%s"' % query)
req = ai.text_request()
req.session_id = str(uuid.uuid4())
req.query = query
response = req.getresponse().read().decode('utf-8')
json_obj = json.loads(response)
result = json_obj['result']
action = result['action']
if action == "search":
search_term = None
params = {}
hits = {}
songs = []
artist = result['parameters']['artist']
if len(artist) is 0:
artist = None
period = result['parameters']['period']
if isinstance(period, list):
period = " / ".join(period)
period_original = result['contexts'][0]['parameters']['period.original']
if not artist and not period:
print("NOTHING!!1! (%s)" % result['parameters'])
else:
if artist:
search_term = artist
if period:
(start, end) = period.split("/")
date_start = parse(start)
date_end = parse(end)
params['filters'] = 'release_timestamp: ' + str(date_start.timestamp()) + ' TO ' + str(date_end.timestamp())
artist_text = " by %s" % artist if artist is not None else ""
period_text = " %s" % period_original if period is not None else ""
print("Searching for songs" + artist_text + period_text)
hits = index.search(search_term, params)['hits']
songs = [hit['trackName'] for hit in hits]
if len(songs):
print("Found %s songs: %s." % (len(hits), ", ".join(songs)))
else:
print("I'm afraid I know no song like this :(")
else:
print("Weird action: %s." % result['action'])