Class: Nanny::Search
- Inherits:
-
Object
- Object
- Nanny::Search
- Defined in:
- lib/nanny/search.rb
Constant Summary collapse
- SIZE_UNITS =
%w(b kb mb gb tb)
- FEED_ITEMS_XPATH =
'//rss/channel/item'
- FEED_TEMPLATE_URL =
Addressable::Template.new("http://torrentz.eu/feed?q={query}")
- FEED_DESC_REGEXP =
/ Size: \s* (?<size>\d+) \s* (?<size_unit>[KMGT]?B) .* Seeds: \s* (?<seeds>[\d,]+) \s+ Peers: \s* (?<peers>[\d,]+) \s+ Hash: \s* (?<hash>[a-f0-9]+) /xi
Instance Method Summary collapse
- #document_for_query(query) ⇒ Object
- #search_torrents(query) ⇒ Object
- #torrent_from_item(item) ⇒ Object
Instance Method Details
#document_for_query(query) ⇒ Object
42 43 44 45 |
# File 'lib/nanny/search.rb', line 42 def document_for_query(query) url = FEED_TEMPLATE_URL.(query: query).to_s Nokogiri::XML HTTPClient.get(url) end |
#search_torrents(query) ⇒ Object
20 21 22 23 24 25 |
# File 'lib/nanny/search.rb', line 20 def search_torrents(query) doc = document_for_query(query) doc.xpath(FEED_ITEMS_XPATH).map do |item| torrent_from_item(item) end end |
#torrent_from_item(item) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/nanny/search.rb', line 27 def torrent_from_item(item) desc = item.at_xpath('.//description').text matches = desc.match(FEED_DESC_REGEXP) exp = SIZE_UNITS.index(matches['size_unit'].downcase) size = matches['size'].to_i * (1024 ** exp) Torrent.new( title: item.at_xpath('.//title').text, url: item.at_xpath('.//link').text, seeds: matches['seeds'].gsub(",", "").to_i, peers: matches['peers'].gsub(",", "").to_i, size: size, hash: matches['hash'] ) end |