def connect(self):
        """ intended for google callback, either logs in a existing user or creates a new user and logs them in """

        code = self.request.args.get("code")
        logging.info("received code of %s " % code)

        try:
            # Upgrade the authorization code into a credentials object
            oauth_flow = flow_from_clientsecrets("{}client_secrets.json".format(app_files), scope="profile email")
            oauth_flow.redirect_uri = secrets.GOOGLE_REDIRECT_URI
            credentials = oauth_flow.step2_exchange(code)

        except FlowExchangeError as e:
            logging.info(e)
            return self.flash_out("Failed to upgrade the authorization code.", 401)

        stored_credentials = session.get("credentials")
        stored_gplus_id = session.get("pid")

        # Store the access token in the session for later use, maybe in another
        # version
        session["provider"] = "google"
        session["credentials"] = credentials.to_json()
        # pid stands for provider id
        session["pid"] = credentials.id_token["sub"]

        if stored_credentials and (credentials.id_token["sub"] == stored_gplus_id):
            return self.flash_out("Current user is already logged in.")

        logging.info("#Geting google user info")
        # Get user info
        u_info = self.google.get_user_info(credentials.access_token, credentials.id_token["sub"])

        if u_info.get("error"):
            self.auth.logout()
            return self.flash_out("Something went wrong with the user_info retrieval", 401)

        my_image = self.upload_image_link(
            u_info["image"]["url"][:-2] + "200",
            utils.remove_special_characters(u_info["displayName"] + u_info["id"] + "google"),
        )
        # retrieve an existing user or store a new user in the db
        my_user = self.auth.store_user(u_info["displayName"], u_info["emails"][0]["value"], "/img/" + my_image.id)[1]

        if not my_user:
            self.auth.logout()
            return self.flash_out("Could not store the user to the db")

        # store the user id and the user name in session
        session["uid"] = my_user.id
        session["name"] = my_user.name

        return redirect("/")
Esempio n. 2
0
    def post(self):
        # check for csrf
        if not self.request.form.get("state") == session['state']:
            return self.flash_out(
                "The state did not match your session, please try again", 401, "/")

        # Define the available variables to populate the object
        my_vars = ['name', 'category', 'description', 'link']
        # Define which ones should be Title case
        titles = ['name', 'category']
        new_item = {}

        for mp in my_vars:
            if mp in titles:
                new_item[mp] = self.request.form.get(mp).title()
            else:
                new_item[mp] = self.request.form.get(mp)

        new_item['user_id'] = self.user_info['uid']

        # this test excludes the optional image file test
        new_item_valid, new_item_test_error = utils.test_new_item(new_item)
        if not new_item_valid:
            # Here we should really re-populate the fields with the content submitted
            # for now one has to resubmit, there should also be a test on the
            # front end
            return self.flash_out(
                new_item_test_error, 401, url_for("additem_view"))

        # Check to see if this potential item is already in the db
        if Items.item_exists(dbs, new_item['name'], new_item['category']):
            return self.flash_out(
                "This Item already exists in this category", 401, url_for("additem_view"))

        # Check to see if there is a picture submission and upload it
        upload_file = self.request.files["picture"]
        if upload_file:
            image_name = utils.remove_special_characters(
                new_item["name"] + new_item["category"]) + "_img"
            img = self.upload_image_file(upload_file, image_name)
            # if the upload and storage succeeds add the img id to the item.
            if img:
                new_item['picture'] = img.id

        # add the new item to the database
        Items.addItem(dbs, new_item)

        return self.flash_out("New Item added", 200, "/")
Esempio n. 3
0
def download_photos_from_archive_page(archive_page_index):
    archive_page = get_archives_page(archive_url, archive_page_index)
    photo_pages_urls = get_links_to_photo_pages(archive_page)

    for url in photo_pages_urls:
        photo_url = get_link_to_photo(photography_root_url + url)
        photo_page_content = requests.get(photography_root_url + url).text
        photo_name = get_photo_name(photo_page_content)
        photo_name = utils.remove_special_characters(photo_name)
        photo_timestamp = get_photo_timestamp(photo_page_content)
        photo_caption = photo_timestamp + '_' + photo_name

        if len(photo_url) > 0 and len(photo_caption) > 0:
            print('Downloading ' + photo_caption + ' from ' + photo_url)
            download_url_with_caption('downloads', photo_url, photo_caption)

    return
Esempio n. 4
0
def load_data(args, mode='train'):
    text = []
    usecols = list(
        map(lambda x: int(x),
            args.get('Data', 'usecols').split(',')))
    path = args.get('Data', 'dataset') + '/' + mode + '.csv'
    print('\n path: ' + path)
    data = pd.read_csv(path,
                       usecols=usecols,
                       encoding=args['Data'].get('encoding'),
                       sep=args['Data'].get('csv_sep'),
                       doublequote=True)
    labels = data.iloc[:, 0].tolist()
    if args.get('Data', 'dataset') == 'yelp':
        text = data.iloc[:, 1].tolist()
    elif args.get('Data', 'dataset') == 'ag_news':
        text = (data.iloc[:, 1] + ' ' + data.iloc[:, 2]).tolist()

    text = [remove_stop_words(t) for t in text]
    text = [remove_special_characters(t) for t in text]
    text = [remove_numbers(t) for t in text]
    text = [remove_single_letters(t) for t in text]
    text = [remove_single_characters(t) for t in text]
    return labels, text
    def connect(self):
        """ intended for facebook callback, either logs in a existing user or creates a new user and logs them in """
        if request.args.get("state") != session["state"]:
            return self.json_out("invalid state", 401)

        # get the authorization code
        code = request.args.get("code")

        # Get the access token for this user
        url = "https://graph.facebook.com/v2.3/oauth/access_token"
        params = {
            "client_id": secrets.FB_APP_ID,
            "redirect_uri": secrets.FB_REDIRECT_URI,
            "client_secret": secrets.FB_APP_SECRET,
            "code": code,
        }
        answer = requests.get(url, params=params)
        token_answer = answer.json()

        # check if the exchange succeeded
        if token_answer.get("error"):
            return self.flash_out("could not make the exchange", 401)

        # Get user info
        u_inf = self.oauth.facebook.get_user_info(token_answer["access_token"])

        # check if the user info request succeeded
        if u_inf.get("error"):
            return self.json_out("authentication failed", 401)

        # Try to find the user in the db
        user = self.auth.get_user_by_email(u_inf["email"])
        if not user:
            # get user picture before we store user
            u_pic = requests.get(
                "https://graph.facebook.com/v2.2/me/picture",
                params={"access_token": token_answer["access_token"], "redirect": 0, "width": 200, "height": 200},
            ).json()
            # if request for picture fails put a default avatar as a picture
            if u_pic.get("error"):
                user = self.auth.store_user(
                    name=u_inf["name"], email=u_inf["email"], picture="/static/img/default_avatar.jpg"
                )
            else:
                picture = self.upload_image_link(
                    u_pic["data"]["url"], utils.remove_special_characters(u_inf["id"] + u_inf["name"] + "facebook")
                )
                # Store the new user in the db
                user = self.auth.store_user(name=u_inf["name"], email=u_inf["email"], picture="/img/" + picture.id)[1]

            # If storing it in the db doesn't work, logout and throw an error
            if not user:
                self.auth.logout()
                return self.flash_out("could not store user in the db", 401)

        # Set credentials to the received token
        credentials = token_answer
        # Calculate the token expiration time based on the server time
        # and store it for easy token expiration check
        credentials["expiration"] = int(time.time() + token_answer["expires_in"])
        self.auth.set_login(user, credentials, "facebook", u_inf["id"])

        return redirect("/")
Esempio n. 6
0
 def test_no_emoticons_without_letters_or_numbers(self):
     s = ("Here are some emoticons without letters or numbers in them"
          " >:( :) :-)")
     self.assertEqual(
         to_string(ut.remove_special_characters(s)),
         "here are some emoticons without letters or numbers in them")
Esempio n. 7
0
 def test_numbers(self):
     s = "1,2 1.2 1,000"
     self.assertEqual(to_string(ut.remove_special_characters(s)),
                      "12 12 1000")
Esempio n. 8
0
 def test_no_twitter_signs(self):
     s = "#scandal @elonmusk #innovation @here"
     self.assertEqual(to_string(ut.remove_special_characters(s)),
                      "scandal elonmusk innovation here")
Esempio n. 9
0
 def test_no_emojis(self):
     s = "💪🔥"
     self.assertEqual(to_string(ut.remove_special_characters(s)), "")
Esempio n. 10
0
 def test_no_punctuation(self):
     s = "He said: 'Hey, my name is... Tim!' - Tim."
     self.assertEqual(to_string(ut.remove_special_characters(s)),
                      "he said hey my name is tim tim")
Esempio n. 11
0
    def post(self, category, item_id):
        # Check CSRF state
        state = self.request.form.get("csrf")
        if state != session['state']:
            return self.flash_out(
                "The CSRF state is not valid, try again", 401, "/")

        # Check if item is in the db
        item = Items.get_by_id(dbs, item_id)
        if not item or item.user_id != self.user_info['uid']:
            return self.flash_out(
                "The item you are trying to update does not belong to you.", 401, "/")

        # List of fileds allowed to be updated
        update_fields = ["name", "description", "category", "link"]
        new_vals = {}
        for field in update_fields:
            new_val = self.request.form.get(field)
            # if the user is choosing to update this field and it's not the
            # same value as before
            if new_val and not getattr(item, field) == new_val:
                new_vals[field] = new_val
                setattr(item, field, new_val)

        # if there are updates and they are valid properties
        if new_vals:
            new_vals_valid, new_vals_test_error = utils.test_item_prop(
                new_vals)
            if not new_vals_valid:
                return self.flash_out(new_vals_test_error, 401, "/")

        prev_img_id = None
        upload_file = self.request.files["picture"]
        if upload_file:
            if item.picture:
                # Changing the image name in order to prevent atomicity
                # problems (deleting and immediately writing to the same id)
                image_name = item.picture.split(".")[0]
                image_number = (
                    (int(image_name[-1]) + 1) if image_name[-1].isdigit() else 1)
                image_name = image_name + str(image_number)
            else:
                image_name = utils.remove_special_characters(
                    item.name + item.category) + "_img"
            img = self.upload_image_file(upload_file, image_name)
            if img:
                prev_img_id = item.picture
                item.picture = img.id

        # if there are no new values and no new image
        elif not new_vals:
            return self.flash_out(" No new updates submitted", 200, url_for(
                "item_view", category=item.category, item_id=item.id))

        # persist the changes
        Items.update_item(dbs, item)

        # Erase the previous picture from the db
        if prev_img_id:
            Images.delete_by_id(dbs, prev_img_id)

        return self.flash_out("Item has been updated", 200, url_for(
            "item_view", category=item.category, item_id=item.id))
    def connect(self):
        ''' intended for google callback, either logs in a existing user or creates a new user and logs them in '''

        code = self.request.args.get('code')
        logging.info("received code of %s " % code)

        try:
            # Upgrade the authorization code into a credentials object
            oauth_flow = flow_from_clientsecrets(
                '{}client_secrets.json'.format(app_files),
                scope='profile email')
            oauth_flow.redirect_uri = secrets.GOOGLE_REDIRECT_URI
            credentials = oauth_flow.step2_exchange(code)

        except FlowExchangeError as e:
            logging.info(e)
            return self.flash_out("Failed to upgrade the authorization code.",
                                  401)

        stored_credentials = session.get('credentials')
        stored_gplus_id = session.get('pid')

        # Store the access token in the session for later use, maybe in another
        # version
        session['provider'] = 'google'
        session['credentials'] = credentials.to_json()
        # pid stands for provider id
        session['pid'] = credentials.id_token['sub']

        if stored_credentials and (credentials.id_token['sub']
                                   == stored_gplus_id):
            return self.flash_out('Current user is already logged in.')

        logging.info("#Geting google user info")
        # Get user info
        u_info = self.google.get_user_info(credentials.access_token,
                                           credentials.id_token['sub'])

        if u_info.get("error"):
            self.auth.logout()
            return self.flash_out(
                "Something went wrong with the user_info retrieval", 401)

        my_image = self.upload_image_link(
            u_info['image']['url'][:-2] + '200',
            utils.remove_special_characters(u_info['displayName'] +
                                            u_info['id'] + 'google'))
        # retrieve an existing user or store a new user in the db
        my_user = self.auth.store_user(u_info['displayName'],
                                       u_info["emails"][0]['value'],
                                       "/img/" + my_image.id)[1]

        if not my_user:
            self.auth.logout()
            return self.flash_out("Could not store the user to the db")

        # store the user id and the user name in session
        session['uid'] = my_user.id
        session['name'] = my_user.name

        return redirect("/")
    def connect(self):
        ''' intended for facebook callback, either logs in a existing user or creates a new user and logs them in '''
        if request.args.get('state') != session['state']:
            return self.json_out("invalid state", 401)

        # get the authorization code
        code = request.args.get('code')

        # Get the access token for this user
        url = "https://graph.facebook.com/v2.3/oauth/access_token"
        params = {
            "client_id": secrets.FB_APP_ID,
            "redirect_uri": secrets.FB_REDIRECT_URI,
            "client_secret": secrets.FB_APP_SECRET,
            "code": code
        }
        answer = requests.get(url, params=params)
        token_answer = answer.json()

        # check if the exchange succeeded
        if token_answer.get("error"):
            return self.flash_out("could not make the exchange", 401)

        # Get user info
        u_inf = self.oauth.facebook.get_user_info(token_answer['access_token'])

        # check if the user info request succeeded
        if u_inf.get("error"):
            return self.json_out("authentication failed", 401)

        # Try to find the user in the db
        user = self.auth.get_user_by_email(u_inf['email'])
        if not user:
            # get user picture before we store user
            u_pic = requests.get("https://graph.facebook.com/v2.2/me/picture",
                                 params={
                                     "access_token":
                                     token_answer['access_token'],
                                     "redirect": 0,
                                     "width": 200,
                                     "height": 200
                                 }).json()
            # if request for picture fails put a default avatar as a picture
            if u_pic.get("error"):
                user = self.auth.store_user(
                    name=u_inf["name"],
                    email=u_inf['email'],
                    picture='/static/img/default_avatar.jpg')
            else:
                picture = self.upload_image_link(
                    u_pic['data']['url'],
                    utils.remove_special_characters(u_inf["id"] +
                                                    u_inf["name"] +
                                                    "facebook"))
                # Store the new user in the db
                user = self.auth.store_user(name=u_inf["name"],
                                            email=u_inf['email'],
                                            picture="/img/" + picture.id)[1]

            # If storing it in the db doesn't work, logout and throw an error
            if not user:
                self.auth.logout()
                return self.flash_out("could not store user in the db", 401)

        # Set credentials to the received token
        credentials = token_answer
        # Calculate the token expiration time based on the server time
        # and store it for easy token expiration check
        credentials['expiration'] = int(time.time() +
                                        token_answer['expires_in'])
        self.auth.set_login(user, credentials, "facebook", u_inf['id'])

        return redirect("/")