Ejemplo n.º 1
0
 def __paged_results(self, url):
   r = requests.get(url)
   repos = r.json()
   results = repos['results']
   while repos['next']:
     r = requests.get(repos['next'])
     repos = r.json()
     results += repos['results']
   return results
def verifyFacebookToken(token):
    APP_SECRET = 'fdd185af07273c8269555b67295db4c7'
    APP_ID = '2749120435101395'

    appLink = 'https://graph.facebook.com/oauth/access_token?client_id={}&client_secret={}&grant_type=client_credentials'.format(
        APP_ID, APP_SECRET)
    appToken = requests.get(appLink).json()['access_token']
    link = 'https://graph.facebook.com/debug_token?input_token={}&access_token={}'.format(
        token, appToken)

    try:
        userId = requests.get(link).json()['data']['user_id']
    except (ValueError, KeyError, TypeError) as error:
        return error
    return userId
Ejemplo n.º 3
0
def verifyFacebookToken(token):
    APP_SECRET = os.environ.get('APP_SECRET')
    APP_ID = os.environ.get('APP_ID')

    appLink = 'https://graph.facebook.com/oauth/access_token?client_id={}&client_secret={}&grant_type=client_credentials'.format(
        APP_ID, APP_SECRET)
    appToken = requests.get(appLink).json()['access_token']
    link = 'https://graph.facebook.com/debug_token?input_token={}&access_token={}'.format(
        token, appToken)

    try:
        userId = requests.get(link).json()['data']['user_id']
    except (ValueError, KeyError, TypeError) as error:
        return Response({"error": error})
    return userId
Ejemplo n.º 4
0
def events():
    global client
    global active_scouts
    req = request.get_json()
    # Create scouts instance
    nightscouts = scouts()
    print(req)
    if "status" in req:
        if req["status"] == "completed":
            phone = req["to"]
            # The next line is not recomended its functional but use the global active_scouts variable
            # This variable is updated by the daemon process.. that run in another context
            # Not the flask context, for that reason we are going to use firebase to get
            # fresh data
            # uscout = [active_scout for active_scout in active_scouts if active_scout['phone'] == phone]
            uscout = nightscouts.getby_personal_phone(phone)
            if uscout != None:
                entries = requests.get(uscout["nightscout_api"]).json()
                glucose = entries[0]['sgv']
                sms_glucose_alert(
                    uscout["emerg_contact"], uscout["username"], glucose)
                # print('sms simulation to: {0} {1} {2}'.format(uscout["emerg_contact"], uscout["username"], glucose))
                for phone in uscout["extra_contacts"]:
                    # print('sms simulation to: {0} {1} {2}'.format(phone, uscout["username"], glucose))
                    sms_glucose_alert(phone, uscout["username"], glucose)
    return "Event Received"
Ejemplo n.º 5
0
def send_request(master_host, namespace, target, rpc, params):
  """Issue a request to the Kubernetes master.
  Args:
    master_host: The IP address of the master e.g. https://35.188.37.10
    namespace: The namespace
    target: The K8s service corresponding to the pod.
    rpc: Which rpc to call.
    params: What parameters to send in the request.
  """
  cluster_name = os.getenv("CLUSTER_NAME")
  res = subprocess.check_output(["aws", "eks", "get-token", "--cluster-name", cluster_name])
  res = json.loads(res)
  token = res["status"]["token"]
  headers = {
    "Authorization": "Bearer " + token.strip(),
  }
  url = ("{master}/api/v1/namespaces/{namespace}/services/{service}:2222"
         "/proxy/{rpc}").format(
           master=master_host, namespace=namespace, service=target, rpc=rpc)
  r = requests.get(url, headers=headers, params=params, verify=False)

  if r.status_code == requests.codes.NOT_FOUND:
    logging.info("Request to %s returned 404", url)
    return ""
  if r.status_code != requests.codes.OK:
    msg = "Request to {0} exited with status code: {1}".format(
      url, r.status_code)
    logging.error(msg)
    raise RuntimeError(msg)

  logging.info("URL %s returned; %s", url, r.content)
  return r.content
Ejemplo n.º 6
0
  def versions(self):
    if not self.__versions:
      repo_split = self.repository.split('/')
      # TODO: Use .match instead
      m = re.search('(?=^.{4,253}$)(^((?!-)[a-zA-Z0-9-]{1,63}(?<!-)\.)+[a-zA-Z]{2,63}$)',
                    repo_split[0])
      if m:
        if repo_split[0].endswith('gcr.io'):
          logging.debug('Adding gcr docker repository...')
          self.__versions += [
            DockerRepositoryGcr(name=repo_split[0],
                                namespace=repo_split[1],
                                gcr_key_file=self.gcr_key_file)]
        else:
          v1_response = requests.get('https://%s/v1' % self.repository)
          if v1_response.ok:
            logging.debug('Adding v1 docker repository...')
          self.__versions += [DockerRepositoryV1(name=repo_split[0], namespace=repo_split[1])]
        # v2_response = requests.get('https://%s/v2' % self.repository)
        # if v2_response.ok:
        # if v2_response.status_code == 401:

      else:
        logging.debug('Adding docker store repository...')
        self.__versions += [DockerStore(namespace=repo_split[0])]

    logging.debug('Docker factory returns: %s' % self.__versions)
    return self.__versions
Ejemplo n.º 7
0
def paginate(url, headers, min_count=30, start_page=1, params=None, limit=None):
    '''paginate will send posts to a url with post_url
       until the results count is not exceeded

       Parameters
       ========== 
       min_count: the results count to go to
       start_page: the starting page
    '''
    if params is None:
        params = dict()
    result = []
    result_count = 1000
    page = start_page
    while result_count >= 30:

        # If the user set a limit, honor it
        if limit is not None:
            if len(result) >= limit:
                return result

        params['page'] = page
        paginated_url = format_params(url, params)
        new_result = requests.get(paginated_url, headers=headers).json()
        result_count = len(new_result)

        # If the user triggers bad credentials, empty repository, stop
        if isinstance(new_result, dict):
            return result
        
        result = result + new_result
        page += 1
    return result
Ejemplo n.º 8
0
 def tags(self, image_name, token=None):
   if not token:
     token = self.token()
   result = requests.get(
       'https://%s/v2/%s/%s/tags/list' % (self.name, self.namespace, image_name),
       headers={'Authorization': 'Bearer %s' % token}).json()
   return natsorted(result['tags'], reverse=True)
def get_client_id(project_id, location, composer_environment):
    # [START composer_get_environment_client_id]
    import google.auth
    import google.auth.transport.requests
    import requests
    import six.moves.urllib.parse

    # Authenticate with Google Cloud.
    # See: https://cloud.google.com/docs/authentication/getting-started
    credentials, _ = google.auth.default(
        scopes=['https://www.googleapis.com/auth/cloud-platform'])
    authed_session = google.auth.transport.requests.AuthorizedSession(
        credentials)

    # project_id = 'YOUR_PROJECT_ID'
    # location = 'us-central1'
    # composer_environment = 'YOUR_COMPOSER_ENVIRONMENT_NAME'

    environment_url = (
        'https://composer.googleapis.com/v1beta1/projects/{}/locations/{}'
        '/environments/{}').format(project_id, location, composer_environment)
    composer_response = authed_session.request('GET', environment_url)
    environment_data = composer_response.json()
    airflow_uri = environment_data['config']['airflowUri']

    # The Composer environment response does not include the IAP client ID.
    # Make a second, unauthenticated HTTP request to the web server to get the
    # redirect URI.
    redirect_response = requests.get(airflow_uri, allow_redirects=False)
    redirect_location = redirect_response.headers['location']

    # Extract the client_id query parameter from the redirect.
    parsed = six.moves.urllib.parse.urlparse(redirect_location)
    query_string = six.moves.urllib.parse.parse_qs(parsed.query)
    print(query_string['client_id'][0])
Ejemplo n.º 10
0
def oauthroute():
    token = oauth.fetch_token('https://accounts.google.com/o/oauth2/token',
                              authorization_response=request.url,
                              client_secret=client_secret)
    req = google.auth.transport.requests.Request()

    id_info = id_token.verify_oauth2_token(token['id_token'], req, client_id)
    headers = {'Authorization': 'Bearer ' + token["access_token"]}
    r = requests.get(
        'https://content-people.googleapis.com/v1/people/me?personFields=names',
        headers=headers)
    person = r.json()
    query = client.query(kind=constants.volunteers)
    query.add_filter("email", "=", id_info['email'])
    results = list(query.fetch())
    if len(results) == 0:
        new_user = datastore.Entity(key=client.key(constants.volunteers))
        client.put(new_user)
        new_user.update({
            "first_name":
            person['names'][0]['givenName'].split(' ')[0],
            "last_name":
            person['names'][0]['familyName'],
            "email":
            id_info["email"],
            "board_member":
            False,
            "hours":
            0
        })
        client.put(new_user)

    return "Your JWT is: %s" % token['id_token'], 201
Ejemplo n.º 11
0
def get_client_id(project_id, location, composer_environment):
    # [START composer_get_environment_client_id]
    import google.auth
    import google.auth.transport.requests
    import requests
    import urllib.parse

    # Authenticate with Google Cloud.
    # See: https://cloud.google.com/docs/authentication/getting-started
    credentials, _ = google.auth.default(
        scopes=['https://www.googleapis.com/auth/cloud-platform'])
    authed_session = google.auth.transport.requests.AuthorizedSession(
        credentials)

    # project_id = 'YOUR_PROJECT_ID'
    # location = 'us-central1'
    # composer_environment = 'YOUR_COMPOSER_ENVIRONMENT_NAME'

    environment_url = (
        'https://composer.googleapis.com/v1beta1/projects/{}/locations/{}'
        '/environments/{}').format(project_id, location, composer_environment)
    composer_response = authed_session.request('GET', environment_url)
    environment_data = composer_response.json()
    airflow_uri = environment_data['config']['airflowUri']

    # The Composer environment response does not include the IAP client ID.
    # Make a second, unauthenticated HTTP request to the web server to get the
    # redirect URI.
    redirect_response = requests.get(airflow_uri, allow_redirects=False)
    redirect_location = redirect_response.headers['location']

    # Extract the client_id query parameter from the redirect.
    parsed = urllib.parse.urlparse(redirect_location)
    query_string = urllib.parse.parse_qs(parsed.query)
    print(query_string['client_id'][0])
Ejemplo n.º 12
0
def callback():
    # Get authorization code Google sent back to you
    code = request.args.get("code")

    # Find out what URL to hit to get tokens that allow you to ask for
    # things on behalf of a user
    google_provider_cfg = get_google_provider_cfg()
    token_endpoint = google_provider_cfg["token_endpoint"]

    # Prepare and send request to get tokens! Yay tokens!
    token_url, headers, body = client.prepare_token_request(
        token_endpoint,
        authorization_response=request.url,
        redirect_url=request.base_url,
        code=code,
    )
    token_response = requests.post(
        token_url,
        headers=headers,
        data=body,
        auth=(GOOGLE_CLIENT_ID, GOOGLE_CLIENT_SECRET),
    )

    # Parse the tokens!
    client.parse_request_body_response(json.dumps(token_response.json()))

    # Now that we have tokens (yay) let's find and hit URL
    # from Google that gives you user's profile information,
    # including their Google Profile Image and Email
    userinfo_endpoint = google_provider_cfg["userinfo_endpoint"]
    uri, headers, body = client.add_token(userinfo_endpoint)
    userinfo_response = requests.get(uri, headers=headers, data=body)

    # We want to make sure their email is verified.
    # The user authenticated with Google, authorized our
    # app, and now we've verified their email through Google!
    if userinfo_response.json().get("email_verified"):
        unique_id = userinfo_response.json()["sub"]
        users_email = userinfo_response.json()["email"]
    else:
        return "User email not available or not verified by Google.", 400

    # Create a user in our db with the information provided
    # by Google

    # Create a user in our db with the information provided
    # by Google
    user = User(id_=users_email)

    # Doesn't exist? Add to database of suspicious people
    if not User.get(users_email):
        User.suspicious(users_email)
        print("User doesn't exist")

    # Begin user session by logging the user in
    login_user(user)

    # Send user back to homepage
    return redirect(url_for("index"))
def get_YT_title(yt_ID):
    html = requests.get(f"http://www.youtube.com/watch?v={yt_ID}").text
    soup = BeautifulSoup(html, "lxml")  #enter your youtube url here
    try:
        video_title = soup.find('title').text
        return video_title.replace(' - YouTube', '')
    except:
        return "error"
Ejemplo n.º 14
0
def POST(url, headers, data=None, params=None):
    '''post_url will use the requests library to post to a url
    '''
    if data is not None:
        return requests.post(url,
                             headers=headers,
                             data=json.dumps(data))
    return requests.get(url, headers=headers)
Ejemplo n.º 15
0
  def list(self):
    r = requests.get('https://%s/v1/search?q=%s' % (self.name, self.namespace))
    search = r.json()
    search_results = search['results']
    while search['num_pages'] != search['page']:
      search = requests.get('https://%s/v1/search?q=%s&page=%d'
                            % (self.name, self.namespace, search['page'] + 1)).json()
      search_results += search['results']

    return [
      DockerRepositoryRecord(
          repository=None if self.name == DockerRepositoryV1.DEFAULT_VALUE else self.name,
          namespace=self.namespace,
          name=search_hit['name'].split('/')[1],
          tags=self.tags(search_hit['name'].split('/')[1]))
      for search_hit in search_results
      if search_hit['name'].startswith('%s/' % self.name.rstrip('/'))
    ]
Ejemplo n.º 16
0
def acquire_token(audience: str = AUDIENCE_URL) -> str:
    # Construct a URL with the audience and format.
    url = METADATA_VM_IDENTITY_URL.format(audience, FORMAT, LICENSES)

    # Request a token from the metadata server.
    r = requests.get(url, headers=METADATA_HEADERS)
    # Extract and return the token from the response.
    r.raise_for_status()
    return r.text
Ejemplo n.º 17
0
def job(id):
    # Calling nemo global client variable
    global client
    global active_scouts
    global nightscout_failed_pings
    global nightscout_failed_update_wait_mark
    global start_scouts

    print("Alerts Job " + id + "")
    if active_scouts != None:
        if start_scouts == False:
            refresh_scouts('Starting_Scouts')
        for active_scout in active_scouts:
            print(active_scout["nightscout_api"])
            try:
                entries = requests.get(active_scout["nightscout_api"]).json()
                glucose = entries[0]['sgv']
                # No failed connection, so initialise the failed pings to zero for the active user
                nightscout_failed_pings[active_scout["phone"]] = 0
                # Check if the last NIGHTSCOUT entry doesn't exceed the NIGHTSCOUT NOT_UPDATE SECONDS limit.
                # In other words, this code verifies if nightscout entries get updates
                current_isoformat_datetime = datetime.utcnow().isoformat()
                nightscout_datetime = entries[0]['dateString']
                # Little trick to make UTC isodate from nodejs moment library compatible with python isoformat
                nightscout_isoformat_datetime = nightscout_datetime.replace(
                    "Z", "+00:00")
                # Difference in seconds between current time and last nightscout entry
                time_difference = calendar.timegm(datetime.fromisoformat(current_isoformat_datetime).utctimetuple(
                )) - calendar.timegm(datetime.fromisoformat(nightscout_isoformat_datetime).utctimetuple())
                print(time_difference, " ", os.getenv(
                    "NIGHTSCOUT_NOT_UPDATE_SECONDS"))
                if time_difference > int(os.getenv("NIGHTSCOUT_NOT_UPDATE_SECONDS")):
                    raise Exception("entry_update")
                else:
                    nightscout_failed_update_wait_mark[active_scout["phone"]] = 0

                # We add a dynamic attribute called glucose to pass glucose info to events url
                if 70 <= glucose <= 240:
                    print("{0} is inside the range for {1}".format(
                        glucose, active_scout["username"]))
                else:
                    print("Executing emergency call and loading sms NCCO if needed")
                    call_glucose_alert(active_scout["phone"], glucose)
            except KeyError:
                print("Key error")
            except Exception as instance:
                if instance.args[0] == 'entry_update':
                    handle_nightscout_failed_update(
                        active_scout["phone"], active_scout["nightscout_api"], active_scout["username"])
                    print(
                        "No updated entries for user, executing the failed update handler for user " + active_scout["username"])
                else:
                    handle_nightscout_failed_pings(
                        active_scout["phone"], active_scout["nightscout_api"], active_scout["username"])
                    print("Server could not establish connection with " +
                          active_scout["nightscout_api"])
Ejemplo n.º 18
0
 def list(self):
   token = self.token()
   response = requests.get('https://%s/v2/%s/tags/list' % (self.name, self.namespace),
                           headers={'Authorization': 'Bearer %s' % token})
   return [
     DockerRepositoryRecord(repository=self.name,
                            namespace=self.namespace,
                            name=child,
                            tags=self.tags(child, token))
     for child in response.json()['child']
   ]
Ejemplo n.º 19
0
 def complete_login(self, request, app, token, **kwargs):
     resp = requests.get(self.profile_url,
                         params={
                             "access_token": token.token,
                             "alt": "json"
                         },
                         timeout=5)
     resp.raise_for_status()
     extra_data = resp.json()
     login = self.get_provider().sociallogin_from_response(
         request, extra_data)
     return login
Ejemplo n.º 20
0
def send_signed_request():
    """Send a request to an App Engine service, with signed identity token.

    This uses the metadata service to get a signed identity token for the
    default service account, with the target audience ("aud"). The token is
    added to a new request, and we show the response from the target service.
    """
    target_url = get_target_url()
    token = get_identity_token(audience=target_url)
    response = requests.get(target_url,
                            headers={'Authorization': 'Bearer ' + token})
    response.raise_for_status()

    return response.json()
Ejemplo n.º 21
0
def get_client_id(project_id, location, composer_environment):
    # [START composer_get_environment_client_id]
    # This script is intended to be used with Composer 1 environments
    # In Composer 2, the Airflow Webserver is not in the tenant project
    # so there is no tenant client ID
    # See https://cloud.google.com/composer/docs/composer-2/environment-architecture
    # for more details
    import google.auth
    import google.auth.transport.requests
    import requests
    import six.moves.urllib.parse

    # Authenticate with Google Cloud.
    # See: https://cloud.google.com/docs/authentication/getting-started
    credentials, _ = google.auth.default(
        scopes=["https://www.googleapis.com/auth/cloud-platform"])
    authed_session = google.auth.transport.requests.AuthorizedSession(
        credentials)

    # project_id = 'YOUR_PROJECT_ID'
    # location = 'us-central1'
    # composer_environment = 'YOUR_COMPOSER_ENVIRONMENT_NAME'

    environment_url = (
        "https://composer.googleapis.com/v1beta1/projects/{}/locations/{}"
        "/environments/{}").format(project_id, location, composer_environment)
    composer_response = authed_session.request("GET", environment_url)
    environment_data = composer_response.json()
    composer_version = environment_data["config"]["softwareConfig"][
        "imageVersion"]
    if "composer-1" not in composer_version:
        version_error = (
            "This script is intended to be used with Composer 1 environments. "
            "In Composer 2, the Airflow Webserver is not in the tenant project, "
            "so there is no tenant client ID. "
            "See https://cloud.google.com/composer/docs/composer-2/environment-architecture for more details."
        )
        raise (RuntimeError(version_error))
    airflow_uri = environment_data["config"]["airflowUri"]

    # The Composer environment response does not include the IAP client ID.
    # Make a second, unauthenticated HTTP request to the web server to get the
    # redirect URI.
    redirect_response = requests.get(airflow_uri, allow_redirects=False)
    redirect_location = redirect_response.headers["location"]

    # Extract the client_id query parameter from the redirect.
    parsed = six.moves.urllib.parse.urlparse(redirect_location)
    query_string = six.moves.urllib.parse.parse_qs(parsed.query)
    print(query_string["client_id"][0])
Ejemplo n.º 22
0
def fetch_manifest():
    mani_ref = db.collection('environment').document('manifest')
    mani_doc = mani_ref.get()
    if mani_doc.exists and dt.datetime.now(
            dt.timezone.utc) - mani_doc.update_time < dt.timedelta(hours=24):
        manifest = mani_doc.to_dict()
    else:
        print('from source')
        url = 'https://store.opencravat.org/manifest.yml'
        data = requests.get(url)
        out = data.content
        manifest = yaml.load(out, Loader=yaml.FullLoader)
        mani_ref.set(manifest)
    return manifest
def get_client_id(airflow_uri) -> str:
    # The Composer environment response does not include the IAP client ID.
    # Make a second, unauthenticated HTTP request to the web server to get the
    # redirect URI.
    redirect_response = requests.get(airflow_uri, allow_redirects=False)
    redirect_location = redirect_response.headers['location']

    # Extract the client_id query parameter from the redirect.
    parsed = six.moves.urllib.parse.urlparse(redirect_location)
    query_string = six.moves.urllib.parse.parse_qs(parsed.query)
    client_id = query_string['client_id'][0]
    print("client_id is: ")
    print(client_id)
    return client_id
Ejemplo n.º 24
0
def callback():
    # Get authorization code Google sent back to you
    code = request.args.get("code")

    google_provider_cfg = get_google_provider_cfg()
    token_endpoint = google_provider_cfg["token_endpoint"]

    token_url, headers, body = client.prepare_token_request(
        token_endpoint,
        authorization_response=request.url,
        redirect_url=request.base_url,
        code=code)

    token_response = requests.post(
        token_url,
        headers=headers,
        data=body,
        auth=(GOOGLE_CLIENT_ID, GOOGLE_CLIENT_SECRET),
    )

    client.parse_request_body_response(json.dumps(token_response.json()))

    userinfo_endpoint = google_provider_cfg["userinfo_endpoint"]
    uri, headers, body = client.add_token(userinfo_endpoint)
    userinfo_response = requests.get(uri, headers=headers, data=body)

    if userinfo_response.json().get("email_verified"):
        google_uid = userinfo_response.json()["sub"]
        users_email = userinfo_response.json()["email"]
        users_name = userinfo_response.json()["given_name"]
    else:
        return "User email not available or not verified by Google.", 400

    user = User(google_id=google_uid, first_name=users_name, email=users_email)

    # If user does not exist in db, create and add them to it
    if not db.session.query(
            exists().where(User.google_id == google_uid)).scalar():
        db.session.add(user)
        db.session.commit()
    else:
        user = db.session.query(User).filter_by(google_id=google_uid).first()

    # Begin user session by logging the user in

    login_user(user)

    # Send user back to homepage
    return redirect(url_for("home"))
Ejemplo n.º 25
0
def get(url):
    """
    Makes a REST GET call to the given url and returns response
    """
    params = {'key': 'AIzaSyCIkzCe8fd_8roPjePi_t_kKVBX-aOCwYM'}
    bearer = get_auth_token()
    headers = {
        'content-type': 'application/json',
        'Accept-Charset': 'UTF-8',
        'Authorization': 'Bearer ' + bearer
    }
    # print(url, headers)
    response = requests.get(url, headers=headers, params=params)
    response.raise_for_status()
    return json.loads(response.content)
Ejemplo n.º 26
0
def markdown():
    mname = request.args.get('module')
    version = request.args.get('version')
    mdoc_ref = db.collection('environment').document('manifest').collection(
        'module-doc').document(mname)
    mdoc = mdoc_ref.get()
    md_text = None
    if mdoc.exists and dt.datetime.now(
            dt.timezone.utc) - mdoc.update_time < dt.timedelta(hours=24):
        md_text = mdoc.to_dict().get('data')
    if md_text is None:
        url = f'https://store.opencravat.org/modules/{mname}/{version}/{mname}.md'
        r = requests.get(url)
        r.raise_for_status()
        md_text = r.text
        mdoc_ref.set({'data': md_text})
    return md_text
Ejemplo n.º 27
0
def make_get_request_to_ai_platform(
        uri: str,
        credentials: Optional[google.auth.credentials.Credentials] = None,
        timeout_ms: int = constants.DEFAULT_TIMEOUT):
    """Makes a get request to AI Platform.

  Args:
    uri: URI of the model resource.
    credentials: The OAuth2.0 credentials to use for GCP services.
    timeout_ms: Timeout for each service call to the api (in milliseconds).

  Returns:
    Request results in json format.
  """
    headers = _get_request_header(credentials)

    r = requests.get(uri, headers=headers, timeout=timeout_ms)
    return _handle_ai_platform_response(uri, r)
Ejemplo n.º 28
0
def get_client_id() -> str:
    """

    Alternatively:
        `curl -v <airflow-url>`, e.g. https://bc5c0e43e23571a62-tp.appspot.com/`
        And the client_id will be printed in the URL
        after "https://accounts.google.com/o/oauth2/v2/auth?client_id="

    """
    airflow_uri = get_airflow_uri()

    # The Composer environment response does not include the IAP client ID.
    # Make a second, unauthenticated HTTP request to the web server to get the redirect URI.
    redirect_response = requests.get(airflow_uri, allow_redirects=False)
    redirect_location = redirect_response.headers['location']

    # Extract the client_id query parameter from the redirect.
    parsed = six.moves.urllib.parse.urlparse(redirect_location)
    query_string = six.moves.urllib.parse.parse_qs(parsed.query)

    return query_string['client_id'][0]
Ejemplo n.º 29
0
    def get(self, attachment_url):
        """
        Get an attachment

        :param attachment_url: Attachment URL
        :type attachment_url: str

        :return: File content-type, File name, File content
        :rtype: (str, str, str)
        """

        # Check if attachment URL is valid URL
        if not is_valid_url(attachment_url):
            logging.info(
                f"Attachment '{attachment_url}' cannot be downloaded, not a valid url"
            )
            return None, None, None

        # Parse url into file name and type
        file_name = unquote_plus(urlparse(attachment_url).path).split("/")[-1]
        file_type = mimetypes.guess_type(file_name)[0]

        # Refresh authentication token
        self.credentials.refresh(self.auth_req)

        request_headers = {"Authorization": f"Bearer {self.credentials.token}"}

        # Get bucket
        try:
            response = requests.get(attachment_url, headers=request_headers)
            response.raise_for_status()
        except requests.exceptions.RequestException as e:
            logging.error(
                f"Attachment '{attachment_url}' cannot be downloaded, skipping upload: {str(e)}"
            )
            return None, None, None
        else:
            logging.debug(f"Successfully downloaded attachment '{attachment_url}'")
            return file_type, file_name, response.content
    def authenticate(self, username=None, password=None):

        print('Facebook ! ')

        url = 'https://graph.facebook.com/v2.11/me?fields=id,name,email,birthday'
        access_token = 'Bearer ' + username
        response = requests.get(url, headers={
            'Authorization': access_token
        }).json()
        if 'id' in response:
            try:
                user = User.objects.get(email=response['email'])
            except User.DoesNotExist:
                user = User.objects.create(
                    username=response['email'],
                    email=response['email'],
                    name=response['name'],
                    birth_date=datetime.datetime.strptime(
                        response['birthday'], "%m/%d/%Y").strftime("%Y-%m-%d"),
                    password=get_random_string(length=32))
            return user
        else:
            return None
Ejemplo n.º 31
0
    def search_cars(filter, param):
        print('SEARCH CARS\n' + 'FILTER: ' + filter + ', PARAM: ' + param)

        try:
            response = requests.get(
                'http://localhost:5000/api/cars?{}={}'.format(filter, param))
            data = json.loads(response.text)
            cars = data['cars']
        except:
            print("Problem communicating with server")
            cars = []

        print('%-2s | %-10s | %-10s | %-8s | %s | %s | %s' %
              ("ID", "Make", "Body Type", "Colour", "No. Seats", "Cost/Hour",
               "Location"))
        print(
            '---+------------+------------+----------+-----------+-----------+----------------------'
        )

        for car in cars:
            print('%-2d | %-10s | %-10s | %-8s | %-9d | $%-8d | %s' %
                  (car['id'], car['make'], car['body_type'], car['colour'],
                   car['no_seats'], car['cost_per_hour'], car['location']))