forked from chuyeow/myanimelist-api
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.rb
179 lines (132 loc) · 4 KB
/
app.rb
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
require 'curb'
require 'nokogiri'
require 'json'
require 'my_anime_list'
# Sinatra settings.
set :sessions, true
JSON_RESPONSE_MIME_TYPE = 'application/json'
mime :json, JSON_RESPONSE_MIME_TYPE
helpers do
include MyAnimeList::Rack::Auth
end
#
# Error handling.
#
error MyAnimeList::NetworkError do
{ :error => "A network error has occurred. Exception message: #{request.env['sinatra.error'].message}" }.to_json
end
error MyAnimeList::UpdateError do
{ :error => "Error updating anime. Exception message: #{request.env['sinatra.error'].message}" }.to_json
end
not_found do
if response.content_type == JSON_RESPONSE_MIME_TYPE
{ :error => response.body }.to_json
end
end
# GET /anime/#{anime_id}
# Get an anime's details.
# Optional parameters:
# * mine=1 - If specified, include the authenticated user's anime details (e.g. user's score, watched status, watched
# episodes). Requires authentication.
get '/anime/:id' do
pass unless params[:id] =~ /^\d+$/
content_type :json
if params[:mine] == '1'
authenticate unless session['cookie_string']
anime = MyAnimeList::Anime.scrape_anime(params[:id], session['cookie_string'])
else
# FIXME Cache this.
anime = MyAnimeList::Anime.scrape_anime(params[:id])
end
anime.to_json
end
# POST /animelist/anime
# Adds an anime to a user's anime list.
post '/animelist/anime' do
content_type :json
authenticate unless session['cookie_string']
# Ensure "anime_id" param is given.
if params[:anime_id] !~ /\S/
status 400
return { :error => 'anime_id-required' }.to_json
end
successful = MyAnimeList::Anime.add(params[:anime_id], session['cookie_string'], {
:status => params[:status],
:episodes => params[:episodes],
:score => params[:score]
})
if successful
nil # Return HTTP 200 OK and empty response body if successful.
else
status 400
{ :error => 'unknown-error' }.to_json
end
end
# PUT /animelist/anime/#{anime_id}
# Updates an anime already on a user's anime list.
put '/animelist/anime/:anime_id' do
content_type :json
authenticate unless session['cookie_string']
successful = MyAnimeList::Anime.update(params[:anime_id], session['cookie_string'], {
:status => params[:status],
:episodes => params[:episodes],
:score => params[:score]
})
if successful
nil # Return HTTP 200 OK and empty response body if successful.
else
status 400
{ :error => 'unknown-error' }.to_json
end
end
# DELETE /animelist/anime/#{anime_id}
# Delete an anime from user's anime list.
delete '/animelist/anime/:anime_id' do
content_type :json
authenticate unless session['cookie_string']
anime = MyAnimeList::Anime.delete(params[:anime_id], session['cookie_string'])
if anime
anime.to_json # Return HTTP 200 OK and the original anime if successful.
else
status 400
{ :error => 'unknown-error' }.to_json
end
end
# Get a user's anime list.
get '/animelist/:username' do
content_type :json
anime_list = MyAnimeList::AnimeList.anime_list_of(params[:username])
anime_list.to_json
end
# GET /anime/search
# Search for anime.
get '/anime/search' do
content_type :json
# Ensure "q" param is given.
if params[:q] !~ /\S/
status 400
return { :error => 'q-required' }.to_json
end
authenticate
results = MyAnimeList::Anime.search(params[:q], :username => auth.username, :password => auth.credentials[1])
results.to_json
end
# GET /anime/top
# Get the top anime.
get '/anime/top' do
content_type :json
anime = MyAnimeList::Anime.top(
:type => params[:type],
:page => params[:page],
:per_page => params[:per_page]
)
anime.to_json
end
# Verify that authentication credentials are valid.
# Returns an HTTP 200 OK response if authentication was successful, or an HTTP 401 response.
# FIXME This should be rate-limited to avoid brute-force attacks.
get '/account/verify_credentials' do
# Authenticate with MyAnimeList if we don't have a cookie string.
authenticate unless session['cookie_string']
nil # Reponse body is empy.
end