Solved Convert day of the week to a proper date?
#1
Somebody could give me a hand with this? I'm running an addon that isn't developed any more and I've got some problems with certain pvr timers that use days of the week instead of dates in yyyy-mm-dd format.

Part of the script that gets pvr timers and converts them:

Code:
    def _parseSVDRP(self, raw):
        # empty result list
        timers = {}
        # loop thru lines
        for line in raw.splitlines():
            # as we know timers getting returned with status 250 (ok)
            if line.startswith("250"):
                try:
                    # get into the fields
                    fields = line[4:].split(":")
                    # check the timer status (flags 1: enabled, 2, instant record, 4, vps, 8: active)
                    timer_status = fields[0].split(" ")[1]
                    # decode starting time
                    timer_start = int(time.mktime(time.strptime(fields[2]+fields[3], "%Y-%m-%d%H%M")))
                    # fill the timer dictonary
                    if timer_start>0:
                        timers[timer_start] = int(timer_status)
                except:
                    # some lines may fail
                    print "vdr.powersave: unable to parse line '%s' " % (line)
        self._timers = timers

Output of svdrpsend for a timer with days of the week and a "normal" one with a fixed date:

Code:
250-1 1:137:M---F--:0550:0630:49:50:test:
250-2 1:2:2013-11-15:2210:2320:99:99:sportaktuell:

Complete script: http://pastebin.com/7CGFeDPy

I'm not experienced at all with python (or any coding for that matter), I can do some small changes by stealing bits of code here and there but changing this myself is above my skill set I think. I also do not know how much work this would be so any informations would be much appreciated.
Reply
#2
How does that relate to the schedule ie I'm assuming it means it will record on Monday and Friday from 5:50 to 6:30 but does this mean it will record next Monday and Friday at those times?

It shouldn't be too difficult to parse the days, compare the days to the current date and give you the dates I.e. if it's Friday today and the schedule says Monday then you need to add 3 days to the current date.

Haven't looked at the full script yet, and probably won't have much time to help on this at the moment. Sorry.

Also, I'm not a python guru so someone else may have a far simpler solution!
BBC Live Football Scores: Football scores notifications service.
script.squeezeinfo: shows what's playing on your Logitech Media Server
Reply
#3
Yes exactly, my back-end will record this every week on Monday and Friday until I delete the timer. Not sure how it is handled internally but I would imagine it checks the timers and then assigns recordings for the week.

What the addon does is basically check the timers on my pvr back-end, then wakes up the htpc for recordings or keeps xbmc from shutting down if a recording is running.

Thanks for the reply. Smile
Reply
#4
I thought that was what your add-on did. I wrote one for my myth tv box a few years ago (see my signature) although I should really update it as my python skills are much better now!

I may have a little bit of time sometime today and I'll try to take a look for you.
BBC Live Football Scores: Football scores notifications service.
script.squeezeinfo: shows what's playing on your Logitech Media Server
Reply
#5
Will try to take a look today.
BBC Live Football Scores: Football scores notifications service.
script.squeezeinfo: shows what's playing on your Logitech Media Server
Reply
#6
Ok - try this:
Code:
def _parseSVDRP(self, raw):
    # empty result list
    timers = {}
    inttoday = datetime.datetime.today().weekday()
    # loop thru lines
    for line in raw.splitlines():
        timer_start = 0
        # as we know timers getting returned with status 250 (ok)
        if line.startswith("250"):

            # get into the fields
            fields = line[4:].split(":")
            # check the timer status (flags 1: enabled, 2, instant record, 4, vps, 8: active)
            timer_status = fields[0].split(" ")[1]
            # decode starting time
            if len(fields[2])>7:
                try:
                    timer_start = int(time.mktime(time.strptime(fields[2]+fields[3], "%Y-%m-%d%H%M")))
                except:
                    # some lines may fail
                    print "vdr.powersave: unable to parse line '%s' " % (line)
                finally:
                    if timer_start>0:
                        timers[timer_start] = int(timer_status)
            
            elif len(fields[2]) == 7:
                # Split the string into 7 days and enumerate through it
                # Conveniently the first day is Monday (with index of 0)
                # which matches how Python treats week days
                for i, day in enumerate(list(fields[2])):
                    # Check if the day is flagged for recording
                    if not day == "-":
                        # Put the index of the day into a new variable
                        recday = i
                        # if the weekday is less than the current weekday
                        # then the recording is next week
                        if recday < inttoday:
                            # So we need to add 7 days
                            recday = recday + 7
                        # If the recording day is the same as today's date
                        # then we need to check if the recording time is less
                        # than the current time
                        elif recday == inttoday:
                            timenow = int(datetime.datetime.now().strftime("%H%M"))
                            rectime = int(fields[3])
                            # if it is, then the recording is next week
                            if rectime <= timenow:
                                recday = recday + 7
                        
                        # Now let's get the offset from today's date
                        recday = recday - inttoday
                        # Work out what the date is 'recday' days from now
                        recordday = datetime.datetime.now().date() + datetime.timedelta(days=recday)
                        # Make a time object out of the time
                        recordtime = datetime.time(int(fields[3][0:2]), int(fields[3][2:4]))
                        # Combine the date and time into a single object
                        recordtime = datetime.datetime.combine(recordday, recordtime)
                        # Convert the date and time into UTC
                        timer_start = int(time.mktime(recordtime.timetuple()))
                        # Add it to our dictionary
                        if timer_start>0:
                            timers[timer_start] = int(timer_status)
                
            
            else:
                print "unknown line format"
                
    # fill the timer dictonary
    self._timers = timers
you'll also need to add datetime to the imports at the top of the script.

NB this assumes that the alternative format is always 7 chars long - ie. "MTWTFSS" and that there's no differentiation for Tuesday/Thursday or Saturday/Sunday. If that's not the case, then we'll need to tweak the above slightly.

Have a look and let me know how you get on.
BBC Live Football Scores: Football scores notifications service.
script.squeezeinfo: shows what's playing on your Logitech Media Server
Reply
#7
Lovely, thanks a lot! I will give this a try once my recording for tonight is done. I really appreciate you taking the time thanks again. Smile

edit: so far so good, I don't seem to get any errors as of yet, couldn't test shutting down and waking up yet because my GF is on the htpc.
Reply

Logout Mark Read Team Forum Stats Members Help
Convert day of the week to a proper date?0