Exemplo n.º 1
0
class Account(object):
    """A vcast account.
    
    This class handles communication with Vcast FAUCET PVR Server
    """

    def __init__(self, username, password):
        """Set up a REST connection to Vcast Server
        
        Returns id_usr user id or raises exception"""
        self.username = username
        self.password = password

        url = 'http://www.vcast.it/faucetpvr/api/1.0/server_rest.php'
        self.connection = Connection(url)
        self.connection.add_rest_credentials(username, password)
        
        c = self.connection.request_get('/faucetid')
        if c['body'] == 'Access Denied':
            raise Exception('Wrong credentials')
        self.id_usr = simplejson.loads(c['body'])['id_usr']

    def get_channels(self):
        """Return channels.
        
        The function returns channel as a list of dictionaries.
        Each element of the list is a dictionary with two keys,
        - type, whose value is a string that can be "video" or "audio";
        - name, whose value is a string.

        """
        try:
            reply = self.connection.request_get('/channels')
            return simplejson.loads(reply['body'])
        except:
            print reply

    def get_recordings(self):
        """Return recordings.

        The function returns recordings as a list of dictionaries.
        Each element has the following keys whose value is a unicode
        string (type: unicode).
        - id_rec
        - title
        - channel
        - channel_type (can be 'audio' or 'video')
        - format
        - from_time
        - rec_time
        - to_time
        - retention
        - repeat
        - faucetacc (ignore it)

        """
        try:
            reply =  self.connection.request_get('/recordings')
            return simplejson.loads(reply['body'])
        except:
            print reply

    def get_download_urls(self):
        """Return the urls of avaible recordings"""
        feed = self.connection.request_get('/feed')['body']
        feed = re.sub(r'\\(.)', r'\1', feed)[13:-3]
        f = feedparser.parse(feed)
        urls = []
        for i in f.entries:
            # print i['enclosures'][0]['href']
            urls.append(i['enclosures'][0]['href'])
        return urls

    def new_recording(self, recording):
        """Invia al server una nuova programmazione"""
        json = recording.toJson()
        print json
        a = self.connection.request_post('/recordings', body=json)
        print a

    def delete_recording(self, id):
        repl = self.connection.request_get('/delete_recording',
                args={'id_rec':str(id)})
        print repl