def post_request_(self, command, xml_string):
     try:
         auth_string = "Basic " + base64.b64encode(self.user_name_ + ":" +
                                                   self.password_)
         headers = {
             "Content-type": "application/x-www-form-urlencoded",
             "Accept": "text/plain",
             "Authorization": auth_string
         }
         connection = httplib.HTTPConnection(self.address_ + ":" +
                                             str(self.port_))
         post_request = urllib.urlencode({
             self.CMD_PARAM_: command,
             self.XML_PARAM_: xml_string
         })
         connection.request(self.METHOD_POST_, self.URL_SUFFIX_,
                            post_request, headers)
         response = connection.getresponse()
         if self.STATUS_UNAUTHORISED_ == response.status:
             helper.log_error(
                 'HttpDataProvider.post_request_. Unauthorized error')
             raise DVBLinkError(DVBLinkStatus.STATUS_UNAUTHORISED)
         data = response.read()
         connection.close()
         return data
     except socket.error, error:
         helper.log_error('HttpDataProvider.post_request_. Socket error')
         raise DVBLinkError(DVBLinkStatus.STATUS_CONNECTION_ERROR)
 def get_schedules(self):
     request = schedules.SchedulesRequest()
     xml_schedules = self.get_data_(self.GET_SCHEDULES_CMD_,
                                    request.to_xml())
     if xml_schedules == None or len(xml_schedules) == 0:
         raise DVBLinkError(DVBLinkStatus.STATUS_INVALID_DATA)
     return schedules.Schedules(xml_schedules)
 def get_recordings(self):
     request = recordings.RecordingsRequest()
     xml_recordings = self.get_data_(self.GET_RECORDINGS_CMD_,
                                     request.to_xml())
     if xml_recordings == None or len(xml_recordings) == 0:
         raise DVBLinkError(DVBLinkStatus.STATUS_INVALID_DATA)
     return recordings.Recordings(xml_recordings)
 def get_xml_string_(self, response):
     status = DVBLinkStatus.STATUS_INVALID_DATA
     try:
         document = parseString(response)
         status = int(
             document.getElementsByTagName(STATUS_CODE_NODE)
             [0].firstChild.data)
     except Exception, error:
         helper.log_error('HttpDataProvider.get_xml_string_. %s' %
                          str(error))
         raise DVBLinkError(DVBLinkStatus.STATUS_INVALID_DATA)
 def play_channel(self, request_stream):
     xml_streamer = self.get_data_(self.PLAY_CHANNEL_CMD_,
                                   request_stream.to_xml())
     if xml_streamer == None or len(xml_streamer) == 0:
         raise DVBLinkError(DVBLinkStatus.STATUS_INVALID_DATA)
     return streamer.Streamer(xml_streamer)
 def search_epg(self, epg_searcher):
     xml_programs = self.get_data_(self.SEARCH_EPG_CMD_,
                                   epg_searcher.to_xml())
     if xml_programs == None or len(xml_programs) == 0:
         raise DVBLinkError(DVBLinkStatus.STATUS_INVALID_DATA)
     return programs.ChannelsIdWithPrograms(xml_programs)
 def get_stream_info(self, request_stream_info):
     xml_info = self.get_data_(self.GET_STREAM_INFO_CMD_,
                               request_stream_info.to_xml())
     if xml_info == None or len(xml_info) == 0:
         raise DVBLinkError(DVBLinkStatus.STATUS_INVALID_DATA)
     return stream_info.StreamInfoList(xml_info)
 def get_channels(self):
     request = channels.ChannelsRequest()
     xml_channels = self.get_data_(self.GET_CHANNELS_CMD_, request.to_xml())
     if xml_channels == None or len(xml_channels) == 0:
         raise DVBLinkError(DVBLinkStatus.STATUS_INVALID_DATA)
     return channels.Channels(xml_channels)
 def get_data_(self, command, xml_string):
     response = self.post_request_(command, xml_string)
     if len(response) == 0:
         helper.log_error('HttpDataProvider.get_data_. Response is empty')
         raise DVBLinkError(DVBLinkStatus.STATUS_CONNECTION_ERROR)
     return self.get_xml_string_(response)
 def get_object(self, object_requester):
     xml_objects = self.get_data_(self.GET_OBJECTS_CMD_,
                                  object_requester.to_xml())
     if xml_objects == None or len(xml_objects) == 0:
         raise DVBLinkError(DVBLinkStatus.STATUS_INVALID_DATA)
     return media_object.Object(xml_objects)
            if self.STATUS_UNAUTHORISED_ == response.status:
                helper.log_error(
                    'HttpDataProvider.post_request_. Unauthorized error')
                raise DVBLinkError(DVBLinkStatus.STATUS_UNAUTHORISED)
            data = response.read()
            connection.close()
            return data
        except socket.error, error:
            helper.log_error('HttpDataProvider.post_request_. Socket error')
            raise DVBLinkError(DVBLinkStatus.STATUS_CONNECTION_ERROR)

    def get_xml_string_(self, response):
        status = DVBLinkStatus.STATUS_INVALID_DATA
        try:
            document = parseString(response)
            status = int(
                document.getElementsByTagName(STATUS_CODE_NODE)
                [0].firstChild.data)
        except Exception, error:
            helper.log_error('HttpDataProvider.get_xml_string_. %s' %
                             str(error))
            raise DVBLinkError(DVBLinkStatus.STATUS_INVALID_DATA)
        if status != DVBLinkStatus.STATUS_OK:
            raise DVBLinkError(status)
        try:
            return document.getElementsByTagName(
                XML_RESULT_NODE)[0].firstChild.data
        except:
            pass
        return None
Пример #12
0
 def __init__(self, xml_string):
     if self.from_xml_(xml_string) == False:
         raise DVBLinkError(DVBLinkStatus.STATUS_INVALID_DATA)