-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtastekit.py
132 lines (101 loc) · 3.52 KB
/
tastekit.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# -*- coding: utf-8 -*-
"""
:copyright: (c) 2012 by Luis Pallares, for Aquehorajuega.co.
:license: Apache2, see LICENSE for more details.
TasteKit is a recommendation engine API. No need to implement complex algorithms or manage your own recommendations.
To get started, sign up for an API key.
Items and Users are the core models of the API.
Items are the objects in your database that a User likes or dislikes.
When referencing an Item, you simply pass in a unique identifier as an item parameter for each request.
There is no need to dump your entire set of items into the database to begin with, nor is there a create
endpoint for items. This is explained below.
Users are users in your database, referenced by a unique identifier you pass in as the parameter user.
More documentation here: https://tastekit.api-docs.io/v1/
"""
import os
import requests
BASE_URL = "http://www.taste-kit.com/api/v1"
API_KEY = os.environ.get("TASTEKIT_API_KEY", "1aacec95ef775e81")
PARAMS = {
"token": API_KEY
}
LIKES = "like"
DISLIKES = "dislike"
def likes(user_id, item_id, action=LIKES):
"""
Like POST /api/v1/likes/like
When a user "likes" an item.
:param user_id:
:param item_id:
:param action:
:return:
"""
url = "{0}/likes/{1}".format(BASE_URL, action)
data = {
"user": str(user_id),
"item": str(item_id)
}
res = requests.post(url, params=PARAMS, json=data)
if not res.ok:
raise Exception('Http {0}: {1}'.format(res.status_code, res.text))
return True
def dislikes(user_id, item_id):
"""
Dislike POST /api/v1/likes/dislike
When a user "dislikes" an item.
:param user_id:
:param item_id:
:return:
"""
return likes(user_id, item_id, action=DISLIKES)
def recommendations(user_id):
"""
Recommendations GET /api/v1/recommendations
Retrieves the recommendations for a given user.
This returns an array of identifiers for items, limit 10.
Recommendations work based on the likes and dislikes of other users,
no need to dump your entire set of items into the database. For example,
say you're using a movie app that recommends you movies: you like Toy Story and Finding Nemo.
Another user likes Toy Story, Finding Nemo, and Finding Dory. Since you have similar tastes,
Finding Dory will be recommended to you.
:param user_id:
:return:
"""
url = "{0}/recommendations".format(BASE_URL)
data = {
"user": str(user_id)
}
res = requests.get(url, params=PARAMS, json=data)
if not res.ok:
raise Exception('Http {0}: {1}'.format(res.status_code, res.text))
print res.json()
def delete_item(item_id):
"""
Delete Item DELETE /api/v1/items
Deletes an item that any user has liked or disliked before.
:param item_id:
:return:
"""
url = "{0}/items".format(BASE_URL)
data = {
"item": str(item_id)
}
res = requests.delete(url, params=PARAMS, json=data)
if not res.ok and not res.status_code == 404:
raise Exception('Http {0}: {1}'.format(res.status_code, res.text))
return True
def delete_user(user_id):
"""
Delete User DELETE /api/v1/items
Deletes a user that has liked or disliked an item before.
:param user_id:
:return:
"""
url = "{0}/users".format(BASE_URL)
data = {
"user": str(user_id)
}
res = requests.delete(url, params=PARAMS, json=data)
if not res.ok and not res.status_code == 404:
raise Exception('Http {0}: {1}'.format(res.status_code, res.text))
return True