Class: Main

Inherits:
Object
  • Object
show all
Defined in:
lib/etvnet_seek/main.rb

Constant Summary collapse

ENV['HOME'] + "/.etvnet-seek"

Instance Method Summary collapse

Constructor Details

#initializeMain

Returns a new instance of Main.



17
18
19
20
# File 'lib/etvnet_seek/main.rb', line 17

def initialize
  @commander = Commander.new
  @accessor = Accessor.new COOKIE_FILE_NAME, lambda { get_credentials }
end

Instance Method Details

#access_or_media(item, folder) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/etvnet_seek/main.rb', line 110

def access_or_media item, folder
  if folder
    process_folder "media", item.link
  else
    result = @accessor.access item

    while(@accessor.try_again? or result.nil?) do
      result = @accessor.access item
    end

    result
  end
end

#audio(root) ⇒ Object



101
102
103
104
105
106
107
108
# File 'lib/etvnet_seek/main.rb', line 101

def audio root
  process_items "audio", root do |item1, _|
    process_items "radio", item1.link do |item2, _|
      media_info = MediaInfo.new item2.link
      LinkInfo.new(item2, media_info)
    end
  end
end

#best_hundredObject



69
70
71
72
73
# File 'lib/etvnet_seek/main.rb', line 69

def best_hundred
  process_items "best_hundred" do |item, _|
    process_group item, item.link =~ /best100/
  end
end

#channelsObject



61
62
63
64
65
66
67
# File 'lib/etvnet_seek/main.rb', line 61

def channels
  process_items "channels" do |item, user_selection|
    link = user_selection.catalog? ? item.catalog_link : item.link

    process_folder "media", link
  end
end

#display_bottom_menu_part(mode) ⇒ Object



160
161
162
163
# File 'lib/etvnet_seek/main.rb', line 160

def display_bottom_menu_part mode
  puts "<number> => today; <number> c => catalog" if mode == 'channels'
  puts "q. to exit"
end

#display_items(items) ⇒ Object



150
151
152
153
154
155
156
157
158
# File 'lib/etvnet_seek/main.rb', line 150

def display_items items
  if items.size == 0
    puts "Empty search result."
  else
    items.each_with_index do |item, index|
      puts "#{index+1}. #{item}"
    end
  end
end

#folder?(item) ⇒ Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/etvnet_seek/main.rb', line 91

def folder? item
  item.link =~ /(catalog|tv_channel)/ || item.folder?
end

#get_credentialsObject



124
125
126
127
128
129
# File 'lib/etvnet_seek/main.rb', line 124

def get_credentials
  username = ask("Enter username :  ")
  password = ask("Enter password : ") { |q| q.echo = '*' }

  [username, password]
end


165
166
167
168
169
170
171
# File 'lib/etvnet_seek/main.rb', line 165

def launch_link link
  if RUBY_PLATFORM =~ /(win|w)32$/
    `start wmplayer #{link}`
  elsif RUBY_PLATFORM =~ /darwin/
    `open '#{link}'`
  end
end

#mainObject



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/etvnet_seek/main.rb', line 48

def main
  process_items "main" do |item, _|
    case item.link
      when /tv_channels/ then
        channels
      when /(aired_today|catalog)/ then
        process_folder "media", item.link
      when /audio/ then
        audio item.link
    end
  end
end

#process(*params) ⇒ Object



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
# File 'lib/etvnet_seek/main.rb', line 22

def process *params
  mode = @commander.get_initial_mode

  case mode
    when /(search|translit)/ then
      keywords = read_keywords(*params)
      puts "Keywords: #{keywords}" if @commander.translit_mode?

      process_folder "search", keywords
    when 'main' then
      main
    when 'channels' then
      channels
    when 'catalog' then
      process_folder "catalog"
    when 'best_hundred' then
      best_hundred
    when 'top_this_week' then
      top_this_week
    when 'premiere' then
      process_folder "premiere"
    when 'new_items' then
      process_folder "new_items"
  end
end

#process_folder(name, *params) ⇒ Object



95
96
97
98
99
# File 'lib/etvnet_seek/main.rb', line 95

def process_folder name, *params
  process_items name, *params do |item, _|
    access_or_media item, folder?(item)
  end
end

#process_group(item, next_group) ⇒ Object



81
82
83
84
85
86
87
88
89
# File 'lib/etvnet_seek/main.rb', line 81

def process_group item, next_group
  if next_group
    process_items "media", item.link do |media_item, _|
      access_or_media item, (folder?(media_item) or not media_item.access_page?)
    end
  else
    access_or_media item, (folder?(item) or not item.access_page?)
  end
end

#process_items(mode, *params) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/etvnet_seek/main.rb', line 131

def process_items mode, *params
  page = ItemsPageFactory.create mode, *params

  items = page.items

  if items.size > 0
    display_items items
    display_bottom_menu_part(mode)

    user_selection = read_user_selection items

    if not user_selection.quit?
      current_item = items[user_selection.index]

      yield(current_item, user_selection) if block_given?
    end
  end
end

#top_this_weekObject



75
76
77
78
79
# File 'lib/etvnet_seek/main.rb', line 75

def top_this_week
  process_items "top_this_week" do |item, _|
    process_group item, item.link =~ /top_this_week/
  end
end