-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseriesRSS.rb
84 lines (63 loc) · 1.89 KB
/
seriesRSS.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
#!/usr/bin/env ruby
require 'open-uri'
require 'net/http'
require 'fileutils'
require_relative 'config_loader/config_loader'
# main function:
# - gets the config (which is included in a 'require')
# - dowloads the torrent
def main
# Dummy data
frequency = 60
# loads the file to the configuration object
config_loader = ConfigLoader.new ARGV[0]
sleep_time = config_loader.min_check_time
puts "Our loop will sleep for #{sleep_time} seconds at each iteration (minimum check time found for the all torrents)"
# Gets the home path
# @TODO should the paths be relative to the home path?
home_path = File.expand_path('~')
loop do
# for each series get the torrents
config_loader.torrent_list.each do |serie|
puts "\nDownloading \"#{serie.id}\" (last checked on #{serie.last_checked}) "
serie.get_next_episode do |episode|
# gets the filename
filename = episode.split('/').last
puts filename
get_serie(serie, episode, filename)
end
sleep(sleep_time)
end
end
end
# prints the usage
def usage
end
# gets the file and stores it
# TODO: try for a fixed number of time, in case the torrent download
# fails
def get_serie(serie, url, filename)
tries = 3
while tries > 0
begin
uri = URI.parse(URI.encode(url, "[]"))
puts "Trying to download the following torrent:\n#{uri}."
torrent_content = Net::HTTP.get(uri)
break
rescue
puts "Oops, seems that an error occurred while trying to download the torrent #{url}!\n"
tries -= 1
sleep(3)
end
end
return nil if torrent_content.nil?
puts "Downloaded succeeded! Now saving torrent file on disk."
if not File.directory?(serie.path)
FileUtils.mkdir_p serie.path
end
new_torrent_file = open(File.join(serie.path, filename), 'wb')
new_torrent_file << torrent_content
new_torrent_file.close()
end
# Run da bitch
main