Skip to content

Instantly share code, notes, and snippets.

@celsworth
Created Mar 26, 2018
Embed
What would you like to do?
#! /usr/bin/env ruby
# encoding: UTF-8
require 'net/http'
require 'uri'
require 'pathname'
require 'json'
require 'rexml/document'
class TvhToNfo
def initialize(tvh_url:)
@tvh_url = tvh_url
end
def run
grid_finished.each do |entry|
xml = build_xml(entry)
# use entry['filename'] as the output, but replace the file extension
outp = Pathname.new(entry['filename'])
outfile = "#{outp.dirname}/#{outp.basename('.*')}.nfo"
File.open(outfile, 'w:UTF-8') { |fh| xml.write(fh, 4) }
end
end
private
def grid_finished
url = URI.parse("#{@tvh_url}/api/dvr/entry/grid_finished?limit=10000")
r = Net::HTTP.get_response(url)
raise 'tvheadend non-200 response' unless r.code == '200'
JSON.parse(r.body)['entries']
end
def build_xml(entry)
doc = REXML::Document.new
doc << REXML::XMLDecl.new('1.0', 'UTF-8', true)
ed = doc.add_element('episodedetails')
ed.add_element('title').add_text(entry['disp_title'])
season, episode = split_season_episode(entry['episode'])
ed.add_element('season').add_text(season) if season
ed.add_element('episode').add_text(episode) if episode
ed.add_element('plot').add_text(entry['disp_description'])
ed.add_element('runtime').add_text((entry['duration']/60).to_s)
ed.add_element('playcount').add_text(entry['playcount'].to_s)
doc
end
def split_season_episode(str)
if str =~ /Season (\d+).Episode (\d+)/
[$1, $2]
end
end
end
ttn = TvhToNfo.new(tvh_url: 'http://localhost:9981')
ttn.run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment