Ejemplo n.º 1
0
    def requestUrl(self,
                   url,
                   destination=None,
                   params=None,
                   data=None,
                   usePost=False):
        # pretend to download the file (for compatability with 'selectImageFromAlbum' of baseService)
        # instead just cache a scaled version of the file and return {status: 200}
        result = RequestResult()

        if url.count("||") == 2:
            filename, width, height = url.split("||", 2)
            recSize = {"width": width, "height": height}
        else:
            filename = url
            recSize = None

        if destination is None or not os.path.isfile(filename):
            result.setResult(RequestResult.SUCCESS).setHTTPCode(400)
        elif recSize is not None and helper.scaleImage(filename, destination,
                                                       recSize):
            result.setFilename(destination)
            result.setResult(RequestResult.SUCCESS).setHTTPCode(200)
        elif helper.copyFile(filename, destination):
            result.setFilename(destination)
            result.setResult(RequestResult.SUCCESS).setHTTPCode(200)
        else:
            result.setResult(RequestResult.SUCCESS).setHTTPCode(418)
        return result
Ejemplo n.º 2
0
    def request(self,
                uri,
                destination=None,
                params=None,
                data=None,
                usePost=False):
        ret = RequestResult()
        result = None
        stream = destination != None
        tries = 0

        while tries < 5:
            try:
                try:
                    auth = self.getSession()
                    if auth is None:
                        logging.error(
                            'Unable to get OAuth session, probably expired')
                        raise RequestExpiredToken
                    if usePost:
                        result = auth.post(uri,
                                           stream=stream,
                                           params=params,
                                           json=data,
                                           timeout=180)
                    else:
                        result = auth.get(uri,
                                          stream=stream,
                                          params=params,
                                          timeout=180)
                    if result is not None:
                        break
                except TokenExpiredError:
                    auth = self.getSession(True)
                    if auth is None:
                        logging.error(
                            'Unable to get OAuth session, probably expired')
                        raise RequestExpiredToken

                    if usePost:
                        result = auth.post(uri,
                                           stream=stream,
                                           params=params,
                                           json=data,
                                           timeout=180)
                    else:
                        result = auth.get(uri,
                                          stream=stream,
                                          params=params,
                                          timeout=180)
                    if result is not None:
                        break
            except InvalidGrantError:
                logging.error(
                    'Token is no longer valid, need to re-authenticate')
                raise RequestInvalidToken
            except:
                logging.exception('Issues downloading')
            time.sleep(tries * 10)  # Back off 10, 20, ... depending on tries
            tries += 1
            logging.warning('Retrying again, attempt #%d', tries)

        if tries == 5:
            logging.error('Failed to download due to network issues')
            raise RequestNoNetwork

        if destination is not None:
            try:
                with open(destination, 'wb') as handle:
                    for chunk in result.iter_content(chunk_size=512):
                        if chunk:  # filter out keep-alive new chunks
                            handle.write(chunk)
                ret.setResult(RequestResult.SUCCESS).setHTTPCode(
                    result.status_code)
                ret.setHeaders(result.headers)
            except:
                logging.exception('Failed to download %s' % uri)
                ret.setResult(RequestResult.FAILED_SAVING)
        else:
            ret.setResult(RequestResult.SUCCESS).setHTTPCode(
                result.status_code)
            ret.setHeaders(result.headers)
            ret.setContent(result.content)
        return ret