Hi guys,

I'm working on my python script to create the button for tv programs.

I know how to create the variable to get the program title for the programs which I have to pull the data from the sqlite database.

I can also create the variable for the height with size I want, but I have no idea how to work out the program times to get the width size for the button control.

Here is the code I use:

#get the programs list
cur.execute('SELECT channel, title, start_date, stop_date FROM programs WHERE channel="channel"')
programList = list()
programs = cur.fetchall()


for row in programs:
     program = row[1].encode('ascii'), str(row[2]), str(row[3])
     program_title = row[1].encode('ascii')
     program_startdate = str(row[2])
     program_endDate = str(row[3])

     # find nearest half hour
     viewStartDate = datetime.datetime.now()
     viewStartDate -= datetime.timedelta(minutes = viewStartDate.minute % 30, seconds = viewStartDate.second)

     try:
         start_date = datetime.datetime.strptime(program_startdate, "%Y%m%d%H%M%S")
         end_date = datetime.datetime.strptime(program_endDate, "%Y%m%d%H%M%S")
     except TypeError:
         start_date = datetime.datetime.fromtimestamp(time.mktime(time.strptime(program_startdate, "%Y%m%d%H%M%S")))
         end_date = datetime.datetime.fromtimestamp(time.mktime(time.strptime(program_endDate, "%Y%m%d%H%M%S")))


         idx = str(program)
         notificationScheduled = program
         #convert the datetime object between start_date and end_date
         startDelta = start_date - viewStartDate
         stopDelta = end_date - viewStartDate

         program_start = self.secondsToXposition(startDelta.seconds)
         if startDelta.days < 0:
            program_start = self.epgView.left
         program_width = self.secondsToXposition(stopDelta.seconds) - program_start

         if program_start + program_width > self.epgView.right:
             program_width = self.epgView.right - program_start


         title = program_title
         program_height = 38

         programs_controls = xbmcgui.ControlButton(350, program_width * idx, 300, program_height, program_title)
         self.addControl(programs_controls)
     cur.close()

I use this to get the start date and end date format from sqlite database:

program_startdate = str(row[2])
program_endDate = str(row[3])

Here is the start date for each channel:

20140520170000
20140520170000
20140520170000
20140520170000
20140520170000
20140520170000
20140520170000

Here is the end date for each channel:

20140520173000
20140520180000
20140520183000
20140520173000
20140520180000
20140520180000

Do you know how i can work out the program time to get the width size for the button control??

If I understand your problem, you want to scale the button size based on how long a show runs? Kinda like the guide on a cable box? Here is one solution I came up with, I broke it into functions to make it easier to read:

# Needed to parse and work with dates/times.
from datetime import datetime

# Turning these dates into lists of strings because they are
# easy to work with.
# You may need to tweak the functions to accept your data.
startdates = """
20140520170000
20140520170000
20140520170000
20140520170000
20140520170000
20140520170000
""".strip().split('\n')

enddates = """
20140520173000
20140520180000
20140520183000
20140520173000
20140520180000
20140520180000
""".strip().split('\n')

def parse_date(s):
    """ Parse a string date into a datetime. """
    # Parse a string using the format:Year, Month, Day, Hours, Minutes, Seconds.
    # ...no spaces.
    return datetime.strptime(s, '%Y%m%d%H%M%S')

def get_date_diff(startdate, enddate):
    """ Get the difference between two dates, in seconds. """
    # Parse both dates, return the seconds in between.
    start = parse_date(startdate)
    end = parse_date(enddate)
    return (end - start).seconds

def get_button_width(startdate, enddate):
    """ Get the desired width for a button, based on the difference of two dates. """
    # Maximum size allowed for a button.
    maxrunningtime = 5400 # 5400 seconds = 1 hour : 30 minutes
    maxbuttonwidth = 300
    # Get the seconds between the two dates.
    runningtime = get_date_diff(startdate, enddate)
    # Figure out the percentage of runningtime vs. maxrunningtime
    runningpercent = (runningtime / maxrunningtime)
    # Scale button based on runningtime vs. maxrunningtime.
    buttonwidth = runningpercent * maxbuttonwidth
    # Constrain value if needed. (is a minbuttonwidth needed?)
    if buttonwidth > maxbuttonwidth:
        buttonwidth = maxbuttonwidth
    return int(buttonwidth)

def test_dates():
    """ A quick debug test to see what we would get. """
    assert len(startdates) == len(enddates)
    for i, startdate in enumerate(startdates):
        enddate = enddates[i]
        # diff is only retrieved here for debugging
        diff = get_date_diff(startdate, enddate)
        width = get_button_width(startdate, enddate)
        print('{} - {} = {}, width: {}'.format(enddate,startdate, diff, width))
test_dates()

Anyway, this was the output:

20140520173000 - 20140520170000 = 1800, width: 100
20140520180000 - 20140520170000 = 3600, width: 200
20140520183000 - 20140520170000 = 5400, width: 300
20140520173000 - 20140520170000 = 1800, width: 100
20140520180000 - 20140520170000 = 3600, width: 200
20140520180000 - 20140520170000 = 3600, width: 200

Edited 2 Years Ago by chriswelborn

Another simpler method would be like this:

def get_button_width_simple(totalseconds):
    """ Given datetime.timedelta.seconds,
        determine width in chunks of half hours,
        where a half hour == 100 pixels.
    """
    pixelsperhalfhour = 100
    halfhours = totalseconds // 1800
    return halfhours * pixelsperhalfhour
This article has been dead for over six months. Start a new discussion instead.