ttwatcher 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8ec6308ff2f79f2c368dc65efd430ceabdf8847a
4
+ data.tar.gz: 18ff67d744a22fb27aab3b0a9e69b242642e6521
5
+ SHA512:
6
+ metadata.gz: 4d4b39a6cab76f1f18723ba858649a60e19d9f3c5cb99ee572a2e1c4d1893eb346e8f40db6f0bb29dafe42c5a73855040cc9c5dd22b44c17cc7c5a0f2cd4f304
7
+ data.tar.gz: 1ebb4a466a013a91fe4ab48153fa09a9a9b5148647183dcc949321967006c2c2d11488cc0598454a1fd0135433e3a0e973de7bf88472a23a77f77a60c2a4ac54
data/Gemfile ADDED
@@ -0,0 +1,20 @@
1
+ # encoding: utf-8
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ gemspec
6
+
7
+ group :development do
8
+ gem 'rake', "~> 12.0.0", require: false
9
+ gem 'webmock', "~> 2.3.2", require: false
10
+ gem 'rspec', '~> 3.5.0', require: false
11
+ end
12
+
13
+ group :guard do
14
+ gem 'rb-readline', require: false
15
+ gem 'guard-rspec', require: false
16
+ end
17
+
18
+ group :test do
19
+ gem 'coveralls', require: false
20
+ end
data/LICENSE ADDED
@@ -0,0 +1,9 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) <2016> Kuzichev Michael
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,70 @@
1
+ # encoding: utf-8
2
+ # frozen_string_literal: true
3
+ #
4
+ # === TTWatcher
5
+ #
6
+ # Torrents search tool for [rutor|unionpeer|megashara|zooqle] sites.
7
+ #
8
+ # @example Detailed sample
9
+ # require 'ttwatcher'
10
+ #
11
+ # torrent_name = 'happy new year'
12
+ #
13
+ # torrents = TTWatcher.search torrent_name
14
+ #
15
+ # puts '-----' * 20
16
+ # puts "request: #{torrent_name}"
17
+ # puts "totally found: #{torrents.count}", ""
18
+ # puts "from megashara: #{torrents.select { |t| t.tracker == :megashara }.count }"
19
+ # puts "from rutor.org: #{torrents.select { |t| t.tracker == :rutor }.count }"
20
+ # puts "from unionpeer: #{torrents.select { |t| t.tracker == :unionpeer }.count }"
21
+ # puts "from zooqle: #{torrents.select { |t| t.tracker == :zooqle }.count }"
22
+ # puts '-----' * 20
23
+ #
24
+ # torrents.each do |t|
25
+ # puts "name: #{t.name}" if t.name
26
+ # puts "url: #{t.url}" if t.url
27
+ # puts "size: #{t.size}" if t.size
28
+ # puts "added_date: #{t.added_date}" if t.added_date
29
+ # puts "tracker: #{t.tracker}" if t.tracker
30
+ #
31
+ # if t.download_url
32
+ # puts "download_url: #{t.download_url}"
33
+ # else
34
+ # puts "magnet_url: #{t.magnet_url}"
35
+ # end
36
+ # puts '-----' * 20
37
+ # end
38
+ #
39
+ # For further details, please @see Readme.md
40
+ #
41
+ # @author Kuzichev Michael
42
+ # @license MIT
43
+ module TTWatcher
44
+ require_relative 'ttwatcher/project_structure'
45
+
46
+ if ENV['development']
47
+ Logger.level = :development
48
+ end
49
+
50
+ ##
51
+ # Searches torrents.
52
+ #
53
+ # @example Expansive sample
54
+ # torrents = TTWatcher.search 'happy new year', from: [:rutor, :zooqle]
55
+ # puts torrents.count
56
+ #
57
+ # @param [String] name
58
+ # Torrent name.
59
+ #
60
+ # @param [Hash] params
61
+ # @option params [Array<Symbol, String>, Symbol, String] :from
62
+ # Site(s) where application should search torrents.
63
+ #
64
+ # @return [Array<Torrent>]
65
+ # Array (homogeneous) with torrents. Can be empty if nothing was found.
66
+
67
+ def self.search(name, params ={})
68
+ TorrentAgent.search name, params
69
+ end
70
+ end # module TTWatcher
@@ -0,0 +1,24 @@
1
+ # encoding: utf-8
2
+ # frozen_string_literal: true
3
+ module TTWatcher
4
+ module Helpers
5
+ extend self
6
+
7
+ ##
8
+ # Returns symbolized class name.
9
+ #
10
+ # @example
11
+ # site = TTWatcher::Sites:Rutor.new
12
+ # H::class_name(site) # ==> :rutor
13
+ #
14
+ # @return [Symbol]
15
+
16
+ def class_name(klass)
17
+ klass_name =
18
+ klass.class.name.to_s.split('::').last.downcase.to_sym
19
+
20
+ return klass_name
21
+ end
22
+ end # module Helpers
23
+ H = Helpers
24
+ end # module TTWatcher
@@ -0,0 +1,40 @@
1
+ # encoding: utf-8
2
+ # frozen_string_literal: true
3
+ module TTWatcher
4
+ module Logger
5
+ def self.extended(mod)
6
+ @level = :standard
7
+ end
8
+ extend self
9
+
10
+ # @return [Symbol] (:normal)
11
+ # Verbose level, possible values :development, :standard, :disabled:
12
+
13
+ attr_accessor :level
14
+
15
+ ##
16
+ # Prints customized message with backtrace to last exception.
17
+ #
18
+ # @param [String] msg
19
+
20
+ def with_backtrace(msg)
21
+ puts "\n", msg
22
+
23
+ puts "\n", "**** Backtrace ****".center(100), "\n"
24
+ puts $@.join("\n"), "\n"
25
+ puts "**** End ****".center 100
26
+ end
27
+
28
+ def logger
29
+ @logger || $stdout
30
+ end
31
+
32
+ def logger=(new_logger)
33
+ @logger = new_logger
34
+ end
35
+
36
+ def <<(*args)
37
+ Array(args).each { |arg| logger.puts arg}
38
+ end
39
+ end # module Logger
40
+ end # module TTWatcher
@@ -0,0 +1,50 @@
1
+ # encoding: utf-8
2
+ # frozen_string_literal: true
3
+ module TTWatcher
4
+ # Dependencies from core lib
5
+
6
+ require 'forwardable'
7
+ require 'json'
8
+ require 'date'
9
+ require 'yaml'
10
+
11
+ # Dependencies from rubygems.org
12
+
13
+ require "addressable/uri"
14
+ require 'nokogiri'
15
+ require 'rest-client'
16
+ require 'deep_merge'
17
+
18
+ # Project structure
19
+
20
+ # Core class for all exceptions that can be raised in TTWatcher.
21
+
22
+ class TTWError < StandardError; end
23
+
24
+ ##
25
+ # Loads *.rb files in requested order
26
+
27
+ def self.load(**params)
28
+ params[:files].each do |f|
29
+ require File.join(__dir__, params[:folder].to_s, f)
30
+ end
31
+ end
32
+ private_class_method :load
33
+
34
+ load files: %w(helpers logger torrent torrent_list)
35
+
36
+ load folder: 'sites/connection',
37
+ files: %w(scheme url)
38
+
39
+ load folder: "sites",
40
+ files: %w(connection config)
41
+
42
+ load folder: 'sites/parsers',
43
+ files: %w(abstract_parser simple_parser rutor_parser megashara_parser
44
+ unionpeer_parser zooqle_parser)
45
+
46
+ load folder: 'sites',
47
+ files: %w(site torrent_site rutor megashara unionpeer zooqle)
48
+
49
+ load files: %w(sites torrent_agent)
50
+ end # module TTWatcher
@@ -0,0 +1,68 @@
1
+ # encoding : utf-8
2
+ # frozen_string_literal: true
3
+ module TTWatcher
4
+ module Sites # no-doc
5
+ extend self
6
+
7
+ ##
8
+ # Returns all possible pairs:
9
+ # :name => TorrentSite child class (with associated +:name+)
10
+ #
11
+ # @example
12
+ # torrent_sites[:megashara] # ==> Sites::Megashara
13
+ #
14
+ # @return [Hash<Symbol:Class>]
15
+
16
+ def torrent_sites
17
+ @known_torrent_sites ||=
18
+ begin
19
+ sites =
20
+ Sites.constants.select do |const_name|
21
+ const = Sites.const_get const_name
22
+ const.class == Class && const < TorrentSite ? const : nil
23
+ end # ==> [Array<Symbols>]
24
+
25
+ hsh = Hash.new
26
+ sites.each { |site| hsh[site.downcase] = Sites.const_get(site) }
27
+
28
+ hsh
29
+ end
30
+ end
31
+
32
+ ##
33
+ # Returns a TorrentSite child class.
34
+ #
35
+ # @param [String, Symbol] name
36
+ # Site name.
37
+ #
38
+ # @exception TorrentSiteUnknown
39
+ # Raised when TorrentSite child class associated with +name+ not found.
40
+ #
41
+ # @return [TorrentSite]
42
+
43
+ def fetch_torrent_site(name)
44
+ site = torrent_sites[name.downcase.to_sym]
45
+
46
+ if site.nil?
47
+ raise TorrentSiteUnknown, name
48
+ else
49
+ return site
50
+ end
51
+ end
52
+
53
+ ##
54
+ # Returns list of known torrent sites.
55
+ #
56
+ # @return [Array<Symbols>]
57
+
58
+ def list
59
+ @torrent_sites_keys = torrent_sites.keys
60
+ end
61
+
62
+ ##
63
+ # Raised when TorrentSite child class not found by it's name.
64
+
65
+ class TorrentSiteUnknown < TTWError
66
+ def initialize(name); super "Program does not know how to work with #{name} site"; end; end
67
+ end # module Sites
68
+ end # module TTWatcher
@@ -0,0 +1,20 @@
1
+ # encoding : utf-8
2
+ # frozen_string_literal: true
3
+ module TTWatcher
4
+ module Sites
5
+ module Configuration
6
+ def self.extended(mod)
7
+ begin
8
+ settings = YAML.load_file File.join __dir__, 'config.yml'
9
+
10
+ rescue Errno::ENOENT
11
+ Logger << "File +config.yml+ not found! Program going to be closed"
12
+ exit!
13
+ end
14
+
15
+ TTWatcher.const_set "S", settings.freeze
16
+ end
17
+ extend self
18
+ end # module Configuration
19
+ end # module Sites
20
+ end # module TTWatcher
@@ -0,0 +1,29 @@
1
+ # encoding: utf-8
2
+ ---
3
+ :zooqle:
4
+ :domain_name: zooqle.com
5
+
6
+ :rutor:
7
+ :domain_name: rutor.is
8
+
9
+ :megashara:
10
+ :domain_name: megashara.com
11
+ :connection_settings:
12
+ :url:
13
+ :force_scheme: :http
14
+
15
+ :unionpeer:
16
+ :domain_name: unionpeer.org
17
+ :connection_settings:
18
+ :url:
19
+ :force_scheme: :http
20
+ :encoding: Windows-1251
21
+ :parser_settings:
22
+ :encoding: Windows-1251
23
+
24
+ :defaults:
25
+ :connection_settings:
26
+ :url:
27
+ :force_scheme: :https
28
+ :redirect_allowed: true
29
+ :encoding: utf-8
@@ -0,0 +1,82 @@
1
+ # encoding: utf-8
2
+ # frozen_string_literal: true
3
+ module TTWatcher
4
+ module Sites
5
+ class Connection # no-doc
6
+ include InternetConnection
7
+
8
+ # @return [Site]
9
+ # Site that uses current connection.
10
+
11
+ attr_reader :site
12
+
13
+ # @return [InternetConnection::Url]
14
+ # Last used url.
15
+
16
+ attr_reader :url
17
+
18
+ # @return [Hash]
19
+ # Default settings for the connection.
20
+
21
+ def settings
22
+ @settings ||=
23
+ begin
24
+ defaults = S[:defaults][:connection_settings].dup || {}
25
+ defaults.deep_merge! S[site.name][:connection_settings]
26
+ defaults
27
+ end
28
+ end
29
+
30
+ ##
31
+ # Creates new Connection instance.
32
+ #
33
+ # @return [Sites::Connection]
34
+
35
+ def initialize(site)
36
+ @site = site
37
+ end
38
+
39
+ ##
40
+ # Downloads requested url.
41
+ #
42
+ # @param [String] url
43
+ # Url in any form: "https://site.com", "site.com", "www.site.com"
44
+ #
45
+ # @param [Hash] request_params
46
+ #
47
+ # @return [String, NilClass]
48
+ # when 'OK' it returns requested page (html, string).
49
+ # when 'Error' it returns +nil+.
50
+
51
+ def download(url, request_params = {})
52
+ with_exceptions_control do
53
+ request = settings.dup.deep_merge request_params
54
+ @url = Url.new url, request[:url]
55
+ response = RestClient::Request.execute method: :get, url: url.to_s
56
+
57
+ return response.body
58
+ end
59
+ end
60
+
61
+ private
62
+
63
+ def with_exceptions_control # no-doc
64
+ yield
65
+
66
+ rescue Errno::ECONNREFUSED # ==> Scheme not supported. Try another one.
67
+ url.scheme_switch
68
+
69
+ retry
70
+ rescue RestClient::Forbidden
71
+ Logger << "Connection for '#{url.to_s}' Forbidden."
72
+
73
+ return nil
74
+ rescue Exception => exception
75
+ Logger.with_backtrace "Unexpected exception (#{exception.inspect}) " \
76
+ "has been raised for '#{url.to_s}' url."
77
+
78
+ return nil
79
+ end
80
+ end # class Connection
81
+ end # module Sites
82
+ end # module TTWatcher
@@ -0,0 +1,79 @@
1
+ # encoding: utf-8
2
+ # frozen_string_literal: true
3
+ module TTWatcher
4
+ module Sites
5
+ # TODO REFACTOR
6
+ module InternetConnection
7
+
8
+ #
9
+ # <<Breaking news>> Some cites still using http.
10
+ #
11
+ # This module designed to switch&normalizate scheme for +url+ instance, if default (https)
12
+ # scheme not supported.
13
+ #
14
+ module Scheme
15
+ def self.included?(url, scheme = nil)
16
+ if scheme
17
+ (url =~ Regexp.new(scheme.to_s + '://')) == 0
18
+ else
19
+ SCHEMES.any? { |p| included?(url, p) }
20
+ end
21
+ end
22
+
23
+ def self.add_scheme!(url, scheme)
24
+ if included?(url, scheme)
25
+ url
26
+ else
27
+ url.replace "#{scheme.to_s}://#{url}"
28
+ end
29
+ end
30
+
31
+ def scheme
32
+ @scheme
33
+ end
34
+
35
+ def scheme_switch
36
+ @allowed_schemes -= [ @scheme ]
37
+ if @allowed_schemes.length > 0
38
+ @scheme = @allowed_schemes.first
39
+ else
40
+ MessageWarn.send "Unknown scheme requested: #{@scheme}!"
41
+ end
42
+ @switched = true
43
+
44
+ normalization!
45
+ end
46
+
47
+ private
48
+
49
+ SCHEMES = [ :https, :http ]
50
+ DEFAULT = SCHEMES.first
51
+
52
+ def set_scheme(scheme = nil)
53
+ @scheme = scheme || DEFAULT
54
+ @allowed_schemes = SCHEMES.dup
55
+ @switched = false
56
+
57
+ normalization!
58
+ end
59
+
60
+ def normalization!
61
+ unless @switched
62
+ return encode_url if Scheme.included?(@url)
63
+ end
64
+ return encode_url if Scheme.included?(@url, @scheme)
65
+
66
+ strip_schemes_from_url!
67
+ Scheme.add_scheme!(@url, @scheme)
68
+ end
69
+
70
+ def strip_schemes_from_url!
71
+ SCHEMES.each do |scheme|
72
+ @url.gsub!(scheme.to_s + '://', '') if Scheme.included?(@url, scheme)
73
+ end
74
+ end
75
+ end # module Scheme
76
+ end # module InternetConnection
77
+ end # module Sites
78
+ end # module TTWatcher
79
+