Class: Video

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/get_freaky/video.rb

Constant Summary collapse

BASE_URI =
"http://confreaks.tv/api/v1"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(title, abstract, host, embed_code, event_short_code) ⇒ Video

Returns a new instance of Video.



11
12
13
14
15
16
17
18
# File 'lib/get_freaky/video.rb', line 11

def initialize(title, abstract, host, embed_code, event_short_code)
  self.title = title
  self.abstract = abstract
  self.host = host
  self.embed_code = embed_code
  # TODO: This feels like a kludge to me--figure out a better way to deal with the event_short_code for featured videos
  self.event_short_code = event_short_code || nil
end

Instance Attribute Details

#abstractObject

Returns the value of attribute abstract.



9
10
11
# File 'lib/get_freaky/video.rb', line 9

def abstract
  @abstract
end

#embed_codeObject

Returns the value of attribute embed_code.



9
10
11
# File 'lib/get_freaky/video.rb', line 9

def embed_code
  @embed_code
end

#event_short_codeObject

Returns the value of attribute event_short_code.



9
10
11
# File 'lib/get_freaky/video.rb', line 9

def event_short_code
  @event_short_code
end

#hostObject

Returns the value of attribute host.



9
10
11
# File 'lib/get_freaky/video.rb', line 9

def host
  @host
end

#titleObject

Returns the value of attribute title.



9
10
11
# File 'lib/get_freaky/video.rb', line 9

def title
  @title
end

Class Method Details

.find(event_short_code, title) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/get_freaky/video.rb', line 29

def self.find(event_short_code, title)
  slug = self.create_slug(event_short_code, title)
  response = get("/videos/#{slug}.json")
  if response.success?
    self.create_video(response, event_short_code)
  else
    puts slug
    puts title
    puts "#{response["status"]} error: #{response["error"]}"
  end
end


20
21
22
23
24
25
26
27
# File 'lib/get_freaky/video.rb', line 20

def self.find_featured
  response = get("/featured-video.json")
  if response.success?
    self.create_video(response)
  else
    raise response.response
  end
end

Instance Method Details

#downloadObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/get_freaky/video.rb', line 41

def download
  puts "downloading #{title}"
  YoutubeDL.download url, { output: "#{title}.mp4" }
rescue Cocaine::ExitStatusError => e
  regex_errors = [
    /YouTube said\: This video does not exist\./,
    /YouTube said\: Please sign in to view this video\./,
    /Incomplete YouTube ID/,
    /HTTP Error 404\: Not Found/, # for vimeo
  ]

  case e.message
  when *regex_errors
    puts $LAST_MATCH_INFO[0]
    return
  when /content too short/
    puts "#{$LAST_MATCH_INFO[0]} - retry"
    return download
  end

  # raise different uncaught exceptions
  raise e
end

#eventObject



65
66
67
# File 'lib/get_freaky/video.rb', line 65

def event
  Event.find(event_short_code)
end

#to_sObject



69
70
71
# File 'lib/get_freaky/video.rb', line 69

def to_s
  %Q{\n#{Paint['Title:', :green]} #{title}\n#{Paint["Description:", :green]} #{abstract}\n}
end

#urlObject



73
74
75
76
77
78
79
# File 'lib/get_freaky/video.rb', line 73

def url
  if host == "youtube"
    "https://www.youtube.com/watch?v=#{embed_code}"
  elsif host == "vimeo"
    "https://vimeo.com/#{embed_code}"
  end
end