Class: SpeakerText

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

Constant Summary collapse

VERSION =
'1.0.3'

Instance Method Summary collapse

Constructor Details

#initialize(api_key) ⇒ SpeakerText

Returns a new instance of SpeakerText.



10
11
12
13
# File 'lib/speaker_text.rb', line 10

def initialize(api_key)
  self.class.basic_auth api_key,'x' 
  @verbose=false
end

Instance Method Details

#fetch_dfxp_transcript(args) ⇒ Object



126
127
128
129
# File 'lib/speaker_text.rb', line 126

def fetch_dfxp_transcript(args)
  args.merge!(format: 'dfxp')
  transcript(args)
end

#fetch_html_transcript(args) ⇒ Object



121
122
123
124
# File 'lib/speaker_text.rb', line 121

def fetch_html_transcript(args)
  args.merge!(format: 'html')
  transcript(args)
end

#fetch_text_transcript(args) ⇒ Object



116
117
118
119
# File 'lib/speaker_text.rb', line 116

def fetch_text_transcript(args)
  args.merge!(format: 'txt')
  transcript(args)
end

#fetch_transcript(args) ⇒ Object



107
108
109
# File 'lib/speaker_text.rb', line 107

def fetch_transcript(args)
  transcript(args)
end

#fetch_xml_transcript(args) ⇒ Object



111
112
113
114
# File 'lib/speaker_text.rb', line 111

def fetch_xml_transcript(args)
  args.merge!(format: 'xml')
  transcript(args)
end

#generate_idObject



19
20
21
# File 'lib/speaker_text.rb', line 19

def generate_id()
  UUIDTools::UUID.random_create.to_s
end

#remote_file_exists(url) ⇒ Object



23
24
25
26
27
# File 'lib/speaker_text.rb', line 23

def remote_file_exists(url)
  response = HTTParty.head(url)
  return true if response.code == 200
  false
end

#submit_transcription_request(query) ⇒ Object



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

def submit_transcription_request(query)
  puts query if @verbose
  response = self.class.post("/transcripts", :query => query)
  return [false, "Unauthorized: API key is incorrect"] if response.code==401
  return [false, "SpeakerText Internal Error"] if response.code==500

  fields = JSON.parse response.body

  return [false, fields['message']] if response.code != 201
  return [true, fields['transcript_ids'][0]]
end

#transcribe(args) ⇒ Object



84
85
86
87
88
# File 'lib/speaker_text.rb', line 84

def transcribe(args)
  return transcribe_url(args) if args.include?(:url)
  return transcribe_platform(args) if args.include?(:platform)
  "Missing arguments to transcribe request"
end

#transcribe_platform(args) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/speaker_text.rb', line 41

def transcribe_platform(args)
  puts "Transcribe Platform" if @verbose
  platform = args[:platform]
  source_id = args[:id]
  return [false, "Source id from #{platform} required"] unless source_id && source_id!=''
  source_annotation = args[:annotation] || ''

  sources = {:platform => platform, 
             :video_id => source_id, 
             :annotation => source_annotation
            }

  query = {}
  query[:sources] = sources.to_json
  query[:pingback_url] = args[:pingback_url] if args.include?(:pingback_url) 
  submit_transcription_request(query)
end

#transcribe_url(args) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/speaker_text.rb', line 59

def transcribe_url(args)
  puts "Transcribe URL" if @verbose
  url = args[:url]
  return [false, "Cannot access remote file #{url}."] if !remote_file_exists(url)
  source_id = args[:id] || generate_id()
  source_title = args[:title] || File.basename(url)
  source_thumbnail_url = args[:thumb_url] || ''
  return [false, "Cannot access remote thumbnail file #{url}."] if args.include?(:thumb_url) && 
                                                         args[:thumb_url]!='' && 
                                                         !remote_file_exists(thumb_url)
  source_annotation = args[:annotation] || ''

  sources = {:url => url, 
             :ref_id => source_id, 
             :title => source_title,
             :thumb_url => source_thumbnail_url,
             :annotation => source_annotation
            }

  query = {}
  query[:sources] = sources.to_json
  query[:pingback_url] = args[:pingback_url] if args.include?(:pingback_url) 
  submit_transcription_request(query)
end

#transcript(args) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/speaker_text.rb', line 90

def transcript(args)
  id = args[:id]
  return [false, 'transcript id required'] unless id && id!=''

  format = args[:format] || 'xml'
 
  response = self.class.get("/transcripts/#{id}", :query => { format: format })
  return [false, "Unauthorized: API key is incorrect"] if response.code==401
  return [false, "SpeakerText Internal Error"] if response.code==500

  fields = JSON.parse response.body
 
  return [false, fields['message']] if response.code != 200
  return [false, fields['status']] if fields['content']==''
  return [true, fields['content']]
end

#transcript_status(args) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
# File 'lib/speaker_text.rb', line 131

def transcript_status(args)
  id = args[:id]
 
  response = self.class.get("/transcripts/#{id}")
  return [false, "SpeakerText Internal Error"] if response.code==500

  fields = JSON.parse response.body

  return [false, fields['message']] if response.code != 200
  return [true, fields['status']]
end

#verboseObject



15
16
17
# File 'lib/speaker_text.rb', line 15

def verbose
  @verbose=true
end