Class: DownloadTV::MyEpisodes

Inherits:
Object
  • Object
show all
Defined in:
lib/download_tv/myepisodes.rb

Overview

API wrapper for MyEpisodes

Instance Method Summary collapse

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, save_cookie)
  @agent = Mechanize.new
  @agent.user_agent = DownloadTV::USER_AGENT
  @user = user
  @save_cookie = 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


44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/download_tv/myepisodes.rb', line 44

def load_cookie
  if File.exist? @cookie_path
    @agent.cookie_jar.load @cookie_path
    return @agent if logged_in?

    puts 'The cookie is invalid/has expired.'
  else
    puts 'Cookie file not found'
  end

  
end

#logged_in?Boolean

Returns:

  • (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

#loginObject

Raises:



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 
  pass = prompt_user_data
  page = @agent.get 'https://www.myepisodes.com/login.php'

   = page.forms[1]
  .username = @user
  .password = pass

  page = @agent.submit(, .buttons.first)

  raise InvalidLoginError if page.filename == 'login.php'

  save_cookie if @save_cookie

  @agent
end

#prompt_user_dataObject



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


62
63
64
65
# File 'lib/download_tv/myepisodes.rb', line 62

def save_cookie
  @agent.cookie_jar.save(@cookie_path, session: true)
  @agent
end

#today_showsObject



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