This repository has been archived by the owner on Dec 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mal_network_service.rb
190 lines (137 loc) · 4.98 KB
/
mal_network_service.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
180
181
182
183
184
185
186
187
188
189
190
require 'curl'
require_relative 'keys'
require_relative '../utilities/endpoints'
require_relative '../utilities/redirectable_nokogiri'
module Railgun
class MALNetworkService
def self.myanimelist_host
Endpoints.myanimelist_host
end
# Generates a simple Curl object, bundled with a User-Agent.
# returns a Curl object.
def self.create_request(url)
# Before we make a request, check to make sure the host exists.
if (url.include? self.myanimelist_host) == false
puts "url #{url} does not include the host, adding that now..."
url = myanimelist_host + url
end
curl = Curl::Easy.new(url)
curl.headers['User-Agent'] = Keys.myanimelist_api_key
curl
end
def self.nokogiri_from_request(url)
curl = create_request(url)
begin
curl.perform
rescue Exception => e
raise Railgun::NetworkError.new("An error occurred while performing #{url}. \n\n Original exception: #{e.message}.", e)
end
response = curl.body_str
# Check if the resource does not exist.
raise Railgun::NotFoundError.new("Resource at url #{url} doesn't exist.", nil) if response =~ /This page doesn't exist./i
doc = Nokogiri::HTML(response)
doc
end
def self.nokogiri_from_redirectable_request(url)
begin
response = Net::HTTP.start('myanimelist.net', 80) do |http|
http.get(url, { 'User-Agent' => Keys.myanimelist_api_key })
end
case response
when Net::HTTPRedirection
redirected = true
# Strip everything after the anime ID - in cases where there is a non-ASCII character in the URL,
# MyAnimeList.net will return a page that says "Access has been restricted for this account".
redirect_url = response['location'].sub(%r{(http[s]?://myanimelist.net/.*/\d+)/?.*}, '\2')
response = Net::HTTP.start('myanimelist.net', 80) do |http|
http.get(redirect_url, { 'User-Agent' => Keys.myanimelist_api_key })
end
end
rescue Exception => e
raise Railgun::NetworkError.new("Error searching anime with query '#{query}'. Original exception: #{e.message}", e)
end
nokogiri = Nokogiri::HTML(response.body)
doc = Railgun::RedirectableNokogiri.new
doc.nokogiri = nokogiri
doc.redirected = redirected
doc
end
### Convenience Methods
def self.anime_request_for_id(id)
"https://myanimelist.net/anime/#{id}"
end
def self.anime_search_request_with_query(query)
"https://myanimelist.net/anime.php?q=#{query}"
end
def self.anime_request_for_season(year, season)
if year.nil? == false and season.nil? == false
"https://myanimelist.net/anime/season/#{year}/#{season}"
else
'https://myanimelist.net/anime/season'
end
end
def self.anime_rank_request(type, page)
request = 'https://myanimelist.net/topanime.php'
if rank_type_is_acceptable_for_anime_request(type)
case type
when 'popular'
request += '?type=bypopularity'
else
request += "?type=#{type}"
end
else
request += '?type=all'
end
request += "&page=#{page}" if page && (page.is_a? Integer)
request
end
def self.manga_request_for_id(id)
"https://myanimelist.net/manga/#{id}"
end
def self.manga_search_request_with_query(query)
"https://myanimelist.net/manga.php?q=#{query}"
end
def self.manga_rank_request(type, page)
request = 'https://myanimelist.net/topmanga.php'
if rank_type_is_acceptable_for_manga_request(type)
case type
when 'popular'
request += '?type=bypopularity'
when 'doujinshi'
request += '?type=doujin'
else
request += "?type=#{type}"
end
else
request += '?type=all'
end
request += "&page=#{page}" if page && (page.is_a? Integer)
request
end
def self.character_request_for_id(id)
"https://myanimelist.net/character/#{id}"
end
def self.person_request_for_id(id)
"https://myanimelist.net/people/#{id}"
end
def self.user_request_for_id(id)
"https://myanimelist.net/profile/#{id}"
end
def self.user_friend_list_request_for_id(id)
"https://myanimelist.net/profile/#{id}/friends"
end
### Parameter Checking Methods
def self.rank_type_is_acceptable_for_anime_request(type)
accepted_types = %w(all airing upcoming tv movie ova special popular favorite)
acceptable_type = false
acceptable_type = true if type and accepted_types.include? type
acceptable_type
end
def self.rank_type_is_acceptable_for_manga_request(type)
accepted_types = %w(all manga novels oneshots doujinshi manhwa manhua popular favorite)
acceptable_type = false
acceptable_type = true if type and accepted_types.include? type
acceptable_type
end
end
end