Integrate DirecTV show information on control page

Posted on
Mon Apr 09, 2012 11:15 pm
gtreece offline
Posts: 171
Joined: Sep 26, 2011

Integrate DirecTV show information on control page

Proof of concept. Been playing around with this a bit. It's working, though admittedly not elegant. Maybe it will generate some ideas.

Taking a note from my audio control page, I wondered if I could get current TV show information onto my control page. I found that networked DirecTV receivers respond to an html request, with some info about the current tuned channel. I successfully pulled that data with Applescript, and populated Indigo variables for use on the control pages. I then wondered if more information about the current show could be gathered from IMDb.

Because I was looking for quick and dirty, I utilized 'JSON Helper' app from the app store, to easily parse JSON data with Applescript. Requires it be running as a background app, but overhead is low so far. I also utilized the IMDbPY tool for IMDb lookups. Must be installed on whatever machine you're going to make your IMDb calls from. To facilitate this hacked approach, I created a python script based on one of the examples included with IMDbPY, to accept a movie title, and return a JSON string containing Year, Rating and Trivia elements.

The whole solution will be cleaner when I figure out Python enough to be able to write the whole thing in it. Someday...

With that lead in, how am I using it on control pages? Screen shot below. On the keypad layout, the display window shows info taken directly from the DirecTV receiver (show title, rating, start time, length, channel number and name). The drop bar just above the keypad layout rotates with the year of release, the user rating and a piece of trivia.

This script is scheduled to run every 30 seconds, as long as the video source selected is my DirecTV receiver.

IMG_0052.png
IMG_0052.png (337.11 KiB) Viewed 5332 times


Here's what the display bar shows for year released:

Year.png
Year.png (41.24 KiB) Viewed 5332 times


And here's what it shows for rating:

Rating.png
Rating.png (44.35 KiB) Viewed 5332 times




And here's the script:

Code: Select all
-- getDTVData.scpt
-- April 2012
-- gtreece@swiftcom.com

-- script to get current channel and show information from networked DirecTV receiver
-- updates variables in Indigo for use on control pages
-- also performs an IMDb lookup based on title, to get rating, year released and trivia
-- IMDb data is also stored in Indigo for use on control pages

-- script requires 'JSON Helper' app from the app store
-- script requires IMDbPY package for IMDb lookups

-- URL to query DirecTV
set dtvURL to "http://10.50.0.102:8080/tv/getTuned"

-- build and execute curl statement
-- depricated in favor of JSON Helper approach
-- set curlGetDTV to "curl"
-- set curlGetDTV to curlGetDTV & " " & dtvURL
-- set tunedInfo to do shell script curlGetDTV

-- parse returned string into list
-- JSON Helper is available in the App Store
-- read JSON from uses list as source
-- fetch JSON from uses a URL that returns JSON as source
tell application "JSON Helper"
   --set dtvList to read JSON from (tunedInfo)
   set dtvList to fetch JSON from dtvURL
end tell

-- get a few data points from Indigo for comparison later
tell application "IndigoServer"
   set lastTitle to value of variable "LR_DTV_ShowTitle"
   set lastDataCount to value of variable "LR_DTV_ShowDataCount"
end tell

-- get data for the current show from the returned list
set rating to rating of dtvList
set channelNum to major of dtvList
set channelShortName to callsign of dtvList
set showTitle to title of dtvList
set isRecording to isRecording of dtvList
set showLength to (round ((duration of dtvList) / 60))
set started to startTime of dtvList

-- get full channel name
-- optional, DTV returns 4 character channel name, may be good enough for some
set channelName to channelLookup(channelNum)

-- determine start time of show
-- DTV returns unix timestap for start time
-- Applescript compiler is nice enough to automatically convert that to scientific notitation
-- need to get start time in standard date/time string format
set startedClean to number_to_string(started)
set myStartedHours to my epoch2datetime(startedClean)
set showStart to time string of date myStartedHours

-- has the show changed since last run of script?
-- if so, get new data from IMDb
if showTitle is not equal to lastTitle then
   -- get IMDb data
   try
      set movieResults to do shell script "python ~/Documents/scripts/get_movie_info.py " & quoted form of showTitle
      tell application "JSON Helper"
         set movieResultsList to read JSON from movieResults
      end tell
      set movieYear to IMDBYear of movieResultsList
      set imdbRating to imdbRating of movieResultsList
      set imdbTrivia to imdbTrivia of movieResultsList
   on error
      set movieYear to "No Data"
      set imdbRating to "No Data"
      set imdbTrivia to "No Data"
   end try
   -- I use 3 variables to hold current extended show data from IMDb
   tell application "IndigoServer"
      set the value of variable "LR_DTV_ShowData1" to "Year Released: " & movieYear
      set the value of variable "LR_DTV_ShowData2" to "IMDb Rating: " & imdbRating
      set the value of variable "LR_DTV_ShowData3" to "Trivia: " & imdbTrivia
   end tell
end if

-- update Indigo variable with the data obtained about the current show
-- the routine called, also updates a variable that appears on a control page
-- by copying the contents of one of the 3 ShowData variables into it
-- contents of that variable rotate each time the script runs
try
   updateIndigo(showStart, showLength, rating, showTitle, channelNum, channelName, isRecording, lastDataCount)
end try

-- routine to convert unix timestamp to Applescript time
on epoch2datetime(epochseconds)
   set myshell1 to "date -r "
   set myshell2 to " \"+%m/%d/%Y %I:%M %p\""
   set theDateTime to do shell script (myshell1 & epochseconds & myshell2)
   return theDateTime
end epoch2datetime

-- routine to convert scientific notation number to real number
on number_to_string(this_number)
   set this_number to this_number as string
   if this_number contains "E+" then
      set x to the offset of "." in this_number
      set y to the offset of "+" in this_number
      set z to the offset of "E" in this_number
      set the decimal_adjust to characters (y - (length of this_number)) thru ¬
         -1 of this_number as string as number
      if x is not 0 then
         set the first_part to characters 1 thru (x - 1) of this_number as string
      else
         set the first_part to ""
      end if
      set the second_part to characters (x + 1) thru (z - 1) of this_number as string
      set the converted_number to the first_part
      repeat with i from 1 to the decimal_adjust
         try
            set the converted_number to ¬
               the converted_number & character i of the second_part
         on error
            set the converted_number to the converted_number & "0"
         end try
      end repeat
      return the converted_number
   else
      return this_number
   end if
end number_to_string

on updateIndigo(trackBegin, trackTime, trackRating, trackTitle, trackChannelNum, trackChannelName, trackRecording, lastDC)
   tell application "IndigoServer"
      set the value of variable "LR_DTV_ShowStart" to trackBegin
      set the value of variable "LR_DTV_ShowDuration" to trackTime
      set the value of variable "LR_DTV_ShowRating" to trackRating
      set the value of variable "LR_DTV_ShowTitle" to trackTitle
      set the value of variable "LR_DTV_ShowChannelNum" to trackChannelNum
      set the value of variable "LR_DTV_ShowChannelName" to trackChannelName
      set the value of variable "LR_DTV_ShowRecording" to trackRecording
      -- this if statement rotates content in the DisplayData variable
      if lastDC is equal to "1" then
         set the value of variable "LR_DTV_DisplayData" to the value of variable "LR_DTV_ShowData2"
         set the value of variable "LR_DTV_ShowDataCount" to 2
      else if lastDC is equal to "2" then
         set the value of variable "LR_DTV_DisplayData" to the value of variable "LR_DTV_ShowData3"
         set the value of variable "LR_DTV_ShowDataCount" to 3
      else
         set the value of variable "LR_DTV_DisplayData" to the value of variable "LR_DTV_ShowData1"
         set the value of variable "LR_DTV_ShowDataCount" to 1
      end if
   end tell
end updateIndigo

-- this routine gives me full names for common channels
-- looking for alternate lookup method that I don't need to maintain
-- could also be maintained in a list variable rather than if statement
on channelLookup(chanNum)
   set chanName to ""
   if chanNum is equal to 285 then
      set chanName to "Investigation Discovery"
   else if chanNum is equal to 246 then
      set chanName to "TruTV"
   else if chanNum is equal to 231 then
      set chanName to "Food Network"
   else if chanNum is equal to 501 then
      set chanName to "HBO"
   else if chanNum is equal to 502 then
      set chanName to "HBO 2"
   else if chanNum is equal to 503 then
      set chanName to "HBO Signature"
   else if chanNum is equal to 504 then
      set chanName to "HBO West"
   else if chanNum is equal to 505 then
      set chanName to "HBO 2 West"
   else if chanNum is equal to 506 then
      set chanName to "HBO Comedy"
   else if chanNum is equal to 507 then
      set chanName to "HBO Family"
   else if chanNum is equal to 508 then
      set chanName to "HBO Fmaily West"
   else if chanNum is equal to 509 then
      set chanName to "HBO Zone"
   else if chanNum is equal to 2 then
      set chanName to "CBS"
   else if chanNum is equal to 4 then
      set chanName to "ABC"
   else if chanNum is equal to 8 then
      set chanName to "NBC"
   else if chanNum is equal to 11 then
      set chanName to "Fox"
   else if chanNum is equal to 248 then
      set chanName to "FX"
   else if chanNum is equal to 249 then
      set chanName to "Comedy Central"
   else if chanNum is equal to 247 then
      set chanName to "TBS"
   else if chanNum is equal to 245 then
      set chanName to "TNT"
   else if chanNum is equal to 244 then
      set chanName to "SyFy"
   else if chanNum is equal to 242 then
      set chanName to "USA"
   else if chanNum is equal to 241 then
      set chanName to "Spike"
   end if
   return chanName
end channelLookup

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 2 guests

cron