-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelixir_sips.rb
91 lines (81 loc) · 2.26 KB
/
elixir_sips.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
require 'mechanize'
module ElixirSips
def self.download
SipsDown.new.download
end
class Settings
attr_reader :hash
def initialize
path = File.expand_path('settings.yml', __dir__)
@hash = YAML.load(File.read(path))
end
def [](key)
hash[key]
end
end
class SipsDown < Mechanize
def secrets
@secrets ||= Settings.new
end
def login
puts 'Login in...'
login_page = get('https://elixirsips.dpdcart.com/subscriber/logout')
login_form = login_page.forms.first
login_form.username = secrets['username']
login_form.password = secrets['password']
@list_page = submit(login_form, login_form.buttons.first)
end
def list_page
@list_page ||= login
end
def show_links
@show_links ||= list_page.links_with(href: /\/subscriber\/post\?id=\d+#files/).reverse
end
def download
show_links.each do |link|
transact do
show = ShowPage.new(click(link), secrets)
puts "Inside ShowPage #{show.title}"
show.files_to_download.each do |stupdi_file|
local_file = ::File.join(show.folder, stupdi_file.text)
if ::File.exist?(local_file)
puts "File Already exists #{local_file}"
next
end
dowload_file(
show.folder,
stupdi_file,
stupdi_file.text
)
end
end
end
end
private
def dowload_file(folder, link, file_name)
unless ['.markdown', '.mp4', '.md', '.tar.gz', '.txt'].any?{ |ext| file_name.include?(ext) }
puts "Skiping file: #{file_name}"
return
end
unless Dir.exists?(folder)
puts "Creating folder #{folder}"
FileUtils.mkdir_p(folder)
end
puts "Downloading #{file_name}"
file = click(link)
file.save("#{folder}/#{file_name}")
puts "Download FINISHED_________________________________________________"
end
end
ShowPage = Struct.new(:page, :secrets) do
def title
page.title
end
def files_to_download
page.links_with(href: /\/subscriber\/download\?file_id=\d/)
end
def folder
File.join(secrets['download_folder'], title).gsub(' | Elixir Sips', '')
end
end
end