lib/download_tv/myepisodes.rb in download_tv-2.4.7 vs lib/download_tv/myepisodes.rb in download_tv-2.5.0
- old
+ new
@@ -1,5 +1,7 @@
+# frozen_string_literal: true
+
module DownloadTV
##
# API wrapper for MyEpisodes
class MyEpisodes
def initialize(user, save_cookie)
@@ -9,19 +11,11 @@
@save_cookie = save_cookie
@cookie_path = File.join(ENV['HOME'], '.config', 'download_tv', 'cookie')
end
def login
- if !@user || @user == ''
- print 'Enter your MyEpisodes username: '
- @user = STDIN.gets.chomp
- end
-
- print 'Enter your MyEpisodes password: '
- pass = STDIN.noecho(&:gets).chomp
- puts
-
+ pass = prompt_user_data
page = @agent.get 'https://www.myepisodes.com/login.php'
login_form = page.forms[1]
login_form.username = @user
login_form.password = pass
@@ -33,46 +27,70 @@
save_cookie if @save_cookie
@agent
end
+ def prompt_user_data
+ if !@user || @user == ''
+ print 'Enter your MyEpisodes username: '
+ @user = STDIN.gets.chomp
+ end
+
+ print 'Enter your MyEpisodes password: '
+ pass = STDIN.noecho(&:gets).chomp
+ puts
+ pass
+ end
+
def load_cookie
if File.exist? @cookie_path
@agent.cookie_jar.load @cookie_path
- page = @agent.get 'https://www.myepisodes.com/login.php'
- if page.links[1].text == 'Register'
- puts 'The cookie is invalid/has expired.'
- login
- end
- @agent
+ return @agent if logged_in?
+
+ puts 'The cookie is invalid/has expired.'
else
puts 'Cookie file not found'
- login
end
+
+ login
end
+ def logged_in?
+ page = @agent.get 'https://www.myepisodes.com/login.php'
+ page.links[1].text != 'Register'
+ end
+
def save_cookie
@agent.cookie_jar.save(@cookie_path, session: true)
@agent
end
def get_shows(last)
+ return [] if last.nil?
page = @agent.get 'https://www.myepisodes.com/ajax/service.php?mode=view_privatelist'
shows = page.parser.css('tr.past')
- s = shows.select do |i|
+ shows = filter_newer_shows(shows, last)
+
+ build_show_strings(shows)
+ end
+
+ def filter_newer_shows(shows, date)
+ shows.select do |i|
airdate = i.css('td.date')[0].text
- Date.parse(airdate) >= last
+ Date.parse(airdate) >= date
end
+ end
- s.map do |i|
- name = i.css('td.showname').text
+ def build_show_strings(shows)
+ shows.map do |i|
+ sname = i.css('td.showname').text
ep = i.css('td.longnumber').text
ep.insert(0, 'S')
ep.sub!('x', 'E')
- "#{name} #{ep}"
+ "#{sname} #{ep}"
end
end
end
class InvalidLoginError < StandardError; end