Class: DownloadTV::MyEpisodes
- Inherits:
-
Object
- Object
- DownloadTV::MyEpisodes
- Defined in:
- lib/download_tv/myepisodes.rb
Overview
API wrapper for MyEpisodes
Instance Method Summary collapse
- #build_show_strings(shows) ⇒ Object
-
#filter_newer_shows(shows, date) ⇒ Object
Only keep the shows that have aired since the given date.
- #get_shows_since(last) ⇒ Object
-
#initialize(user, save_cookie) ⇒ MyEpisodes
constructor
A new instance of MyEpisodes.
- #load_cookie ⇒ Object
- #logged_in? ⇒ Boolean
- #login ⇒ Object
- #prompt_user_data ⇒ Object
- #save_cookie ⇒ Object
- #today_shows ⇒ Object
Constructor Details
#initialize(user, save_cookie) ⇒ MyEpisodes
Returns a new instance of MyEpisodes.
7 8 9 10 11 12 13 |
# File 'lib/download_tv/myepisodes.rb', line 7 def initialize(user, ) @agent = Mechanize.new @agent.user_agent = DownloadTV::USER_AGENT @user = user @save_cookie = @cookie_path = File.join(ENV['HOME'], '.config', 'download_tv', 'cookie') end |
Instance Method Details
#build_show_strings(shows) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/download_tv/myepisodes.rb', line 88 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') "#{sname} #{ep}" end end |
#filter_newer_shows(shows, date) ⇒ Object
Only keep the shows that have aired since the given date
81 82 83 84 85 86 |
# File 'lib/download_tv/myepisodes.rb', line 81 def filter_newer_shows(shows, date) shows.select do |i| airdate = i.css('td.date')[0].text Date.parse(airdate) >= date end end |
#get_shows_since(last) ⇒ Object
67 68 69 70 71 72 |
# File 'lib/download_tv/myepisodes.rb', line 67 def get_shows_since(last) page = @agent.get 'https://www.myepisodes.com/ajax/service.php?mode=view_privatelist' shows = page.parser.css('tr.past') shows = filter_newer_shows(shows, last) build_show_strings(shows) end |
#load_cookie ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/download_tv/myepisodes.rb', line 44 def if File.exist? @cookie_path @agent..load @cookie_path return @agent if logged_in? puts 'The cookie is invalid/has expired.' else puts 'Cookie file not found' end login end |
#logged_in? ⇒ Boolean
57 58 59 60 |
# File 'lib/download_tv/myepisodes.rb', line 57 def logged_in? page = @agent.get 'https://www.myepisodes.com/login.php' page.links[1].text != 'Register' end |
#login ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/download_tv/myepisodes.rb', line 15 def login 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 page = @agent.submit(login_form, login_form..first) raise InvalidLoginError if page.filename == 'login.php' if @save_cookie @agent end |
#prompt_user_data ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/download_tv/myepisodes.rb', line 32 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 |
#save_cookie ⇒ Object
62 63 64 65 |
# File 'lib/download_tv/myepisodes.rb', line 62 def @agent..save(@cookie_path, session: true) @agent end |
#today_shows ⇒ Object
74 75 76 77 78 |
# File 'lib/download_tv/myepisodes.rb', line 74 def today_shows page = @agent.get 'https://www.myepisodes.com/ajax/service.php?mode=view_privatelist' shows = page.parser.css('tr.today') build_show_strings(shows) end |