Ruby Code

Discussion about Schedules Direct grabber code and data formats.

Ruby Code

Postby jppti81 » Fri May 08, 2009 8:04 am

Here is a Ruby program which downloads raw XML schedule data. It depends on httpclient.rb gem.

Code: Select all
#!/usr/bin/env ruby
# tmsfetch.rb

require 'rubygems'
require 'httpclient'


User = 'username'
Pswd = 'password'



class Fixnum
    def hours
        self * 60 * 60
    end
    def days
        self * 24 * 60 * 60
    end
end

def log(msg)
    puts("# %s %s" % [Time.now.strftime('%Y-%m-%d %H:%M:%S'), msg])
end



TMSWebService = 'http://webservices.schedulesdirect.tmsdatadirect.com/schedulesdirect/tvlistings/xtvdService'


ZULU_FORMAT = '%Y-%m-%dT%H:%M:%SZ'
now = Time.now.gmtime
start_time = (now - 4.hours).strftime(ZULU_FORMAT)
end_time = (now + 14.days).strftime(ZULU_FORMAT)

TMS_SOAP = <<__EOX__
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
        xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <SOAP-ENV:Body>
        <m:download xmlns:m="urn:TMSWebServices"
                SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
            <startTime xsi:type="xsd:dateTime">#{start_time}</startTime>
            <endTime xsi:type="xsd:dateTime">#{end_time}</endTime>
        </m:download>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>
__EOX__

log('Retrieving DataDirect TV Schedules')
log("Requesting #{start_time} to #{end_time}")
tms_client = HTTPClient.new
tms_client.set_auth(TMSWebService, User, Pswd)

seek_xml = true
seek_buffer = ''
progress = 0
Progress_fmt = "\b" * 10 + '%d kB'
open('tmsdata.xml', 'wb') do |outfd|
    tms_client.post_content(TMSWebService, TMS_SOAP) do |chunk|
        if seek_xml
            seek_buffer << chunk
            start_of_xml = seek_buffer.index('<?xml ')
            next if ! start_of_xml
            chunk = seek_buffer[start_of_xml .. -1]
            seek_xml = false
        end
        progress += chunk.length
        printf(Progress_fmt, progress / 1024)
        outfd.write(chunk)
    end
    puts
end
log('Done')
jppti81
 
Posts: 1
Joined: Fri May 08, 2009 7:54 am

Return to Developers Corner

Who is online

Users browsing this forum: No registered users and 4 guests

cron