Exemplo 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
Exemplo n.º 2
0
    def requestUrl(self,
                   url,
                   destination=None,
                   params=None,
                   data=None,
                   usePost=False):
        result = RequestResult()

        if self._OAUTH is not None:
            # Use OAuth path
            try:
                result = self._OAUTH.request(url,
                                             destination,
                                             params,
                                             data=data,
                                             usePost=usePost)
            except (RequestExpiredToken, RequestInvalidToken):
                logging.exception('Cannot fetch due to token issues')
                result = RequestResult().setResult(RequestResult.OAUTH_INVALID)
                self.invalidateOAuth()
            except requests.exceptions.RequestException:
                logging.exception('request to download image failed')
                result = RequestResult().setResult(RequestResult.NO_NETWORK)
        else:
            tries = 0
            while tries < 5:
                try:
                    if usePost:
                        r = requests.post(url,
                                          params=params,
                                          json=data,
                                          timeout=180)
                    else:
                        r = requests.get(url, params=params, timeout=180)
                    break
                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 r:
                result.setHTTPCode(r.status_code).setHeaders(
                    r.headers).setResult(RequestResult.SUCCESS)

                if destination is None:
                    result.setContent(r.content)
                else:
                    with open(destination, 'wb') as f:
                        for chunk in r.iter_content(chunk_size=1024):
                            f.write(chunk)
                    result.setFilename(destination)
        return result
Exemplo n.º 3
0
    def requestUrl(self,
                   url,
                   destination=None,
                   params=None,
                   data=None,
                   usePost=False):
        result = RequestResult()

        if self._OAUTH is not None:
            # Use OAuth path
            result = self._OAUTH.request(url,
                                         destination,
                                         params,
                                         data=data,
                                         usePost=usePost)
        else:
            tries = 0
            while tries < 5:
                try:
                    if usePost:
                        r = requests.post(url, params=params, json=data)
                    else:
                        r = requests.get(url, params=params)
                    break
                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 r:
                result.setHTTPCode(r.status_code).setHeaders(
                    r.headers).setResult(RequestResult.SUCCESS)

                if destination is None:
                    result.setContent(r.content)
                else:
                    with open(destination, 'wb') as f:
                        for chunk in r.iter_content(chunk_size=1024):
                            f.write(chunk)
                    result.setFilename(destination)
        return result