Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bravia recordings #16

Open
Piffo77 opened this issue Jul 2, 2017 · 2 comments
Open

Bravia recordings #16

Piffo77 opened this issue Jul 2, 2017 · 2 comments

Comments

@Piffo77
Copy link

Piffo77 commented Jul 2, 2017

Hi, I was very glad i found the library because I've tried to implement the same functionality to communicate with my Bravia from a microcontroller by sniffing the protocol between the tv and android application. Now I can use my Raspberry Pi 3 instead but I am very new to Python.

One goal is to add recording time since the tv doesn't include this feature and often I miss the end of a recording if the show is delayed by a fem minutes. This is implemented by reading the recording list and if the program title doesn't include an asterisk/*, the recording in deleted, five minutes are added to the duration time and recording is added to the list again but now with an asterisk in its title so the script wont add another five minutes next time. I also check if there are a recording following and in this case I don't add time, just an asterisk to the title. Three new functions has been added, but I don't have the knowledge yet to handle the error control:

def get_recordings(self):
"""Returns list of recordings - Max 100"""
original_content_list = []
content_index = 0
while True:
resp = self.bravia_req_json("sony/recording",
self._jdata_build("getScheduleList", {"cnt":100,"stIdx":0}))
if not resp.get('error'):
if len(resp.get('result')[0]) == 0:
break
original_content_list.extend(resp.get('result')[0])
else:
break
break
return original_content_list

def add_schedule(self,datarow):
"""Add a recording to the list"""
original_content_list = []
content_index = 0
while True:
# ["addSchedule",["{"type":"string","uri":"string","title":"string","startDateTime":"string","durationSec":"int","repeatType":"string","quality":"string"}"],["{"annotation":"int"}"],"1.0"],

    resp = self.bravia_req_json("sony/recording",
                                self._jdata_build("addSchedule", datarow))
    if not resp.get('error'):
        if len(resp.get('result')[0]) == 0:
            break
        original_content_list.extend(resp.get('result')[0])
    else:
        break
    break
return original_content_list

def delete_schedule(self, datarow):
"""Delete a scheduled recording"""
while True:
#["deleteSchedule",["{"id":"string","type":"string","uri":"string","title":"string","startDateTime":"string","durationSec":"int"}"],[],"1.0"],

    resp = self.bravia_req_json("sony/recording",
                                self._jdata_build("deleteSchedule", datarow))
    break
return 

To add the time, I use the demo code to connect to the tv:

# Get the recordings
tv_rec = braviarc.get_recordings()

# Go through all entries
for line in tv_rec:	
  # Check entries without asterisk
  if line['title'].find('*') < 0 :
       # Edit the start time format so a datetime variable can be used
       Date1 = line['startDateTime'].split('+')
       Date = datetime.strptime(Date1[0], "20%y-%m-%dT%H:%M:%S")
       print("Start:",Date)
       Date = Date + timedelta(seconds=line['durationSec'])
       print("End:",Date.strftime("20%y-%m-%dT%H:%M:%S+0200"))
       # Ensure there aren't a recording following this one
       found = 0
       for line2 in tv_rec:	
          if line2['startDateTime'] == Date.strftime("20%y-%m-%dT%H:%M:%S+0200") :
            print("Collision")
            found = 1
       # Unless the recording has been started - edit the entry     
       if line['recordingStatus'] == 'notStarted' :
         # Delete the entry
         braviarc.delete_schedule(line)
         # Don't add any time if there are a recording following this one
         if found == 0 :
           line.update({'durationSec':(300+line['durationSec'])})
         # Update the title 
         line.update({'title': line['title']+'*'})
         # Add the recording to the list again
         braviarc.add_schedule(line)
@Piffo77
Copy link
Author

Piffo77 commented Jul 2, 2017

The recordings are returned as a list:
[{'channelName': 'Kanal 5', 'id': '7', 'overlapStatus': 'notOverlapped', 'durationSec': 3600, 'repeatType': '1', 'startDateTime': '2017-06-19T23:00:00+0200', 'recordingStatus': 'notStarted', 'type': 'recording', 'title': 'Criminal Minds, Lockdown', 'uri': 'tv:dvbc?trip=40999.36.1314&srvName=Kanal%205', 'quality': 'DR'},{'channelName': 'Kanal 5', 'id': '8', 'overlapStatus': 'notOverlapped', 'durationSec': 3600, 'repeatType': '1', 'startDateTime': '2017-06-20T00:00:00+0200', 'recordingStatus': 'notStarted', 'type': 'recording', 'title': "CSI, Leapin' Lizards", 'uri': 'tv:dvbc?trip=40999.36.1314&srvName=Kanal%205', 'quality': 'DR'}]

@Gh0stWires
Copy link

You could try using a try/except statement to handle the errors.
http://www.pythonforbeginners.com/error-handling/python-try-and-except

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants