예제 #1
0
def some_action(post):
    """ Here you might want to do something with each post. E.g. grab the
    post's message (post['message']) or the post's picture (post['picture']).
    In this implementation we just print the post's created time.
    """
    print(post['source'])
    database.insert_db(table_name='thichngammong', values=post['source'])
예제 #2
0
def webauthn_begin_assertion():
    jsonData = request.get_json()
    username = jsonData['username']

    if not util.validate_username(username):
        return make_response(jsonify({'fail': 'Invalid username.'}), 401)

    user = database.query_db("select * from Users where username=?",[username])[0]
    if len(user) == 0:
        return make_response(jsonify({'fail': 'User does not exist.'}), 401)
    if not user[4]:
        return make_response(jsonify({'fail': 'Unknown credential ID.'}), 401)


    challenge = util.generate_challenge(32)

    # We strip the padding from the challenge stored in the session
    # for the reasons outlined in the comment in webauthn_begin_activate.
    toStoreChallenge = challenge.rstrip('=')
    try:
        insert = database.insert_db("insert into PublicKeyCredentialCreationOptions VALUES (?,?,?,?,?,?)",[None, None,user[0],None,username,toStoreChallenge])
    except:
        update = database.insert_db("update PublicKeyCredentialCreationOptions SET challenge=? where user_username=?",[toStoreChallenge,username])
    webauthn_user = webauthn.WebAuthnUser(
        user[0], user[1], user[2], user[6],
        user[4], user[3], user[5], user[7])

    webauthn_assertion_options = webauthn.WebAuthnAssertionOptions(
        webauthn_user, challenge)

    return jsonify(webauthn_assertion_options.assertion_dict)
예제 #3
0
def talk(request):
    body = json.loads(request.body)
    data = database.get_db()
    user_id = body["object"]["message"]["from_id"]
    if body["object"]["message"]["text"].find('/') != -1:
        mes = body["object"]["message"]["text"].split('/')
        database.insert_db(mes[0], mes[1])
        vk_api.messages.send(user_id=user_id,
                             message="Я записал новую фразу",
                             random_id=random.randint(1,
                                                      50000000000000000000000),
                             v=5.103)
    else:
        for i in data:
            if i[1] == body["object"]["message"]["text"]:
                messages = i[2]
                vk_api.messages.send(user_id=user_id,
                                     message=messages,
                                     random_id=random.randint(
                                         1, 50000000000000000000000),
                                     v=5.103)
            elif i == data[-1] and i[1] != body["object"]["message"]["text"]:
                message1 = "Я не понимаю это сообщение"
                message2 = "Напиши мне пару Сообщение/Ответ, если ты хочешь добавить новую фразу. Не забудь разделить их знаком /"
                vk_api.messages.send(user_id=user_id,
                                     message=message1,
                                     random_id=random.randint(
                                         1, 50000000000000000000000),
                                     v=5.103)
                vk_api.messages.send(user_id=user_id,
                                     message=message2,
                                     random_id=random.randint(
                                         1, 50000000000000000000000),
                                     v=5.103)
예제 #4
0
def email():
    if request.method == 'POST':
        data = request.get_json()
        if check_email(data['email']):
            insert_db(data['email'])
            return jsonify(result=0)  # Everything is good!
        return jsonify(result=1)  # The email format is no valid
    return jsonify(result=-1)  # the type request is not valid
def insertmovie():
    # inserting the movies details in the cassandra database
    movie_id = request.form['mid']
    movie_name = request.form['mname']
    movie_ratings = request.form['mratings']
    movie_revenue = request.form['mrevenue']
    data = [movie_id, movie_name, movie_ratings, movie_revenue]
    insert_db(data)
    return render_template("home.html")
예제 #6
0
def webauthn_end_activate():
    jsonData = request.get_json()
    AuthenticatorAttestationResponse = jsonData['AuthenticatorAttestationResponse']
    clientDataJSON = AuthenticatorAttestationResponse['clientDataJSON']
    clientDataJSON_padding = clientDataJSON.ljust((int)(math.ceil(len(clientDataJSON) / 4)) * 4, '=')
    clientDataJSON = base64.b64decode(clientDataJSON_padding).decode('utf8')
    clientDataJSONparsed = json.loads(clientDataJSON)
    retrievedChallenge = clientDataJSONparsed['challenge']
    try:
        data = database.query_db("select * from PublicKeyCredentialCreationOptions where challenge=?",[retrievedChallenge])[0]
    except:
        return jsonify({"Error:","Could not find challenge"}),500
    trusted_attestation_cert_required = True
    self_attestation_permitted = True
    none_attestation_permitted = True

    registration_response = {'clientData':AuthenticatorAttestationResponse['clientDataJSON'],'attObj':AuthenticatorAttestationResponse['attestationObject']}
    trust_anchor_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), TRUST_ANCHOR_DIR)
    webauthn_registration_response = webauthn.WebAuthnRegistrationResponse(
        RP_ID,
        ORIGIN,
        registration_response,
        data[5],
        trust_anchor_dir,
        trusted_attestation_cert_required,
        self_attestation_permitted,
        none_attestation_permitted,
        uv_required=False)  # User Verification

    try:
        webauthn_credential = webauthn_registration_response.verify()
    except (RuntimeError, TypeError, NameError):
        print(RuntimeError)
        return jsonify({'fail': 'Registration failed. Error: {}'.format(RuntimeError)})
    credential_id=webauthn_credential.credential_id.decode("utf-8")
    duplicatedId = database.query_db("select credential_id from Users where credential_id=?",[credential_id])
    if len(duplicatedId)!=0:
        return jsonify({"Error":"Error with register, try again"}),500

    existing_user = database.query_db("select user_id from Users where username=?",[data[4]])
    if len(existing_user)!=0:
        return jsonify({"Error":"Error with register, try again"}),500
    #Add user to database
    database.insert_db("insert into Users VALUES (?,?,?,?,?,?,?,?)",[data[2],data[4],data[3],webauthn_credential.public_key,credential_id,webauthn_credential.sign_count,'http://localhost',data[1]])
    #Remove from PublicKeyCredentialCreationOptions
    database.delete_db("delete from PublicKeyCredentialCreationOptions where challenge=?",[retrievedChallenge])
    return jsonify({'success': 'User successfully registered.'})
예제 #7
0
def add_user(db, _uuid, _reg_id):
    if not _uuid:
        raise ValueError("uuid must be not null!")
    if not _reg_id:
        raise ValueError("reg_id must be not null!")

    dic = create_dictionary(_uuid=_uuid, _reg_id=_reg_id)
    return database.insert_db(db, sql_commands.SQL_USER_INSERT_ENTRY, dic, True)
예제 #8
0
파일: views.py 프로젝트: M-P-A-Z-b/vk_bot
def talk(request):
    body = json.loads(request.body)
    data = database.get_db()
    user_id = body["object"]["message"]["from_id"]
    stopne = 0
    # print(body)
    # if body == { "type": "confirmation", "group_id": 175960072 }:
    #     return HttpResponse("34c03ca2")
    # print(body)
    # if body["type"] == 'message_new':
    #     data = database.get_db()
    #     user_id = body["object"]["message"]["from_id"]
    #     stocne = 0
    if body["object"]["message"]["text"].find('/') != -1:
        nes = body["object"]["message"]["text"].split('/')
        database.insert_db(nes[0], nes[1])
        vk_api.messages.send(user_id=user_id,
                             message="Я записал новое сообщение",
                             random_id=random.randint(
                                 1, 50000000000000000000000000),
                             v=5.103)
    else:
        for i in data:
            if i[1] == body["object"]["message"]["text"]:
                messages = i[2]
                vk_api.messages.send(user_id=user_id,
                                     message=messages,
                                     random_id=random.randint(
                                         1, 50000000000000000000000000),
                                     v=5.103)
                stopne = 1
            elif i == data[-1] and i[1] != body["object"]["message"][
                    "text"] and stopne != 1:
                message1 = "Простите, но я не могу ответить так как этого нет в моей базе данных."
                message2 = "Чтобы добавить что-то новое базу данных сообщений напиши Сообщение-Ответ и поставь /."
                vk_api.messages.send(user_id=user_id,
                                     message=message1,
                                     random_id=random.randint(
                                         1, 50000000000000000000000000),
                                     v=5.103)
                vk_api.messages.send(user_id=user_id,
                                     message=message2,
                                     random_id=random.randint(
                                         1, 50000000000000000000000000),
                                     v=5.103)
예제 #9
0
def add_subscription(db, _user_id, _school_id, _class):
    if not _user_id:
        raise ValueError("user id must be not null for a valid subscription!")
    if not _school_id:
        raise ValueError("school id must be not null for a valid subscription!")
    if not _class:
        raise ValueError("class must be not null for a valid subscription!")

    dic = create_dictionary(_user_id=_user_id, _school_id=_school_id, _class=_class)
    return database.insert_db(db, sql_commands.SQL_SUB_INSERT_ENTRY, dic, True)
예제 #10
0
def verify_assertion():
    jsonData = request.get_json()
    AuthenticatorAttestationResponse = jsonData['AuthenticatorAttestationResponse']
    clientDataJSON = AuthenticatorAttestationResponse['clientDataJSON']
    clientDataJSON_padding = clientDataJSON.ljust((int)(math.ceil(len(clientDataJSON) / 4)) * 4, '=')
    clientDataJSON = base64.b64decode(clientDataJSON_padding).decode('utf8')
    clientDataJSONparsed = json.loads(clientDataJSON)
    retrievedChallenge = clientDataJSONparsed['challenge']
    try:
        data = database.query_db("select * from PublicKeyCredentialCreationOptions where challenge=?",[retrievedChallenge])[0]
    except:
        return jsonify({"Error:","Could not find challenge"}),500
    #DELETE from table
    database.delete_db("delete from PublicKeyCredentialCreationOptions where challenge=?",[retrievedChallenge])
    signature = AuthenticatorAttestationResponse['signature']
    assertion_response =  {'clientData':AuthenticatorAttestationResponse['clientDataJSON'],'authData':AuthenticatorAttestationResponse['authenticatorData'],'signature':signature,'userHandle':AuthenticatorAttestationResponse['userHandle']}

    credential_id = AuthenticatorAttestationResponse.get('id')
    user = database.query_db("select * from Users where credential_id=?",[credential_id])[0]
    if len(user)==0:
        return make_response(jsonify({'fail': 'User does not exist.'}), 401)
    webauthn_user = webauthn.WebAuthnUser(
        user[0], user[0], user[2], user[6],
        user[4], user[3], user[5], user[7])

    webauthn_assertion_response = webauthn.WebAuthnAssertionResponse(
        webauthn_user,
        assertion_response,
        retrievedChallenge,
        ORIGIN,
        uv_required=False)  # User Verification
    sign_count = webauthn_assertion_response.verify()
    try:
        sign_count = webauthn_assertion_response.verify()
    except Exception as e:
        print(e)
        return make_response(jsonify({'fail': 'Assertion failed'}),500)

    # Update counter.

    #Update database
    update = database.insert_db("update Users SET sign_count=? where username=?",[sign_count,user[1]])
    identityObj={'username':user[1],'id':user[0],'displayname':user[2]}
    expires = datetime.timedelta(hours=2)
    print(identityObj)
    jwt = create_access_token(identity=identityObj,expires_delta=expires)
    return jsonify({
        'success':
        'Successfully authenticated as {}'.format(user[1]),
        'jwt':jwt,
        'username': user[1]
    })
예제 #11
0
def webauthn_begin_activate():
    jsonData = request.get_json()
    name = jsonData['name']
    surname = jsonData['surname']
    email = jsonData['email']
    username = email
    display_name = name + " " + surname
    challenge = util.generate_challenge(32)
    id = util.generate_ukey()
    '''
     PublicKeyCredentialCreationOptions
     rp-> Relying Party: It's the server where you want to authenticate.
            RP_NAME: name
            RP_ID: The RP ID must be equal to the origin's effective domain, or a registrable domain suffix of
                   the origin's effective domain.
                   The origin's scheme must be https.
                   The origin's port is unrestricted.
     user information->
            Information about the user registering
            Helps to choose from multiple credentials.
            username: it is a human-palatable identifier for a user account.
                    It is intended only for display, i.e., aiding the user in determining
                    the difference between user accounts with similar displayNames.
                    For example, "alexm", "*****@*****.**" or "+14255551234".
            display_name: A human-palatable name for the user account, intended only for display. For example, "Alex P. Müller" or "田中 倫".
                         The Relying Party SHOULD let the user choose this, and SHOULD NOT restrict the choice more than necessary.
            id: The user handle of the user account entity.
                A user handle is an opaque byte sequence with a maximum size of 64 bytes,
                and is not meant to be displayed to the user.
    '''
    make_credential_options = webauthn.WebAuthnMakeCredentialOptions(
        challenge, RP_NAME, RP_ID, id, username, display_name,
        'http://localhost')
    challenge = challenge.rstrip("=")
    insert = database.insert_db("insert into PublicKeyCredentialCreationOptions VALUES (?,?,?,?,?,?)",[RP_NAME, RP_ID,id,display_name,username,challenge])
    return jsonify(make_credential_options.registration_dict)
예제 #12
0
        engine = create_engine(
            'postgresql://*****:*****@35.224.187.73:5432/postgres')
        print("Database found.")
    except:
        print("No database available.")
        quit()

    # Ebert's website for listings
    pages = int(
        input(
            "How many pages would you like to scrape (24 movies per page)? "))
    print("Scraping movie listings from Ebert's website.")
    ebert_listing = scrape_eberts_listing()
    archive_data(ebert_listing, "ebert_listing")
    ebert_listing = clean_ebert_listings(ebert_listing)
    insert_db(ebert_listing, 'ebert_listing', engine)

    # Ebert's website for reviews
    print("Scraping movie reviews from Ebert's website.")
    ebert_reviews, _ = scrape_movie_reviews(ebert_listing)
    archive_data(ebert_reviews, "ebert_reviews")
    ebert_reviews = clean_ebert_reviews(ebert_reviews)
    insert_db(ebert_reviews, 'ebert_reviews', engine)

    # IMDB website for other movie info
    print("Scraping movie information from IMDB.")
    imdb_info, _ = scrape_imdb_listing(ebert_listing)
    archive_data(imdb_info, "imdb_info")
    imdb_info = clean_imdb(imdb_info)
    insert_db(imdb_info, 'imdb_info', engine)
예제 #13
0
파일: tweet.py 프로젝트: keoleiji/bot_ocr
def bot():
    CONSUMER_KEY = environ['CONSUMER_KEY']
    CONSUMER_SECRET = environ['CONSUMER_SECRET']
    ACCESS_TOKEN = environ['ACCESS_TOKEN']
    ACCESS_TOKEN_SECRET = environ['ACCESS_TOKEN_SECRET']
    ANSWER_FILE_NAME = 'answer.csv'
    OCR_KEY = environ['OCR_KEY']
    OCR_KEY_2 = environ['OCR_KEY_2']

    auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
    auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)

    api = tweepy.API(auth,
                     wait_on_rate_limit=True,
                     wait_on_rate_limit_notify=True)

    if not os.path.isfile(ANSWER_FILE_NAME):
        write_file = open(ANSWER_FILE_NAME, 'w', newline="")
        write_answer = csv.writer(write_file)
        write_answer.writerow(['Screen_Name', 'Tweet_Id', 'Answer_Date'])
        write_file.close()

    # Instanciando atividades concluídas
    read_file = open(ANSWER_FILE_NAME, 'rt')
    read_answer = csv.reader(read_file)
    answer_data = list(read_answer)
    read_file.close()

    search = '@ConvertToText'
    numero_de_tweets = 250

    for tweet in tweepy.Cursor(api.search, search,
                               include_entities=True).items(numero_de_tweets):
        for row in read_db(tweet.id):
            found = False
            if str(tweet.id) in row:
                found = True
                print('found', tweet.id)
                break
        if found == False:
            try:
                print('not found', tweet.id)
                if 'media' in tweet.entities:
                    for image in tweet.entities['media']:
                        image_url = image['media_url']
                        req = requests.get(
                            'https://api.ocr.space/parse/imageurl?apikey=' +
                            OCR_KEY + '&url=' + image_url)
                        res = json.loads(req.text)
                        reply_text = res['ParsedResults'][0]['ParsedText']
                        print('texto:', tweet.text, '\nid:', tweet.id)
                        try:
                            api.update_status(
                                status='@' + tweet.user.screen_name + '\n' +
                                reply_text,
                                in_reply_to_status_id=tweet.id,
                                auto_populate_reply_metadata=True)
                            insert_db(tweet.user.screen_name, tweet.id,
                                      datetime.now())
                            #write_file = open(ANSWER_FILE_NAME, 'a', newline="")
                            #write_answer = csv.writer(write_file)
                            #write_answer.writerow([tweet.user.screen_name, tweet.id, datetime.now()])
                            #write_file.close()
                            time.sleep(20)
                        except tweepy.TweepError as e:
                            print(e.reason)
                else:
                    print('tweet sem imagem', tweet.id,
                          tweet.in_reply_to_status_id_str)
                    tweet_reply = api.get_status(
                        tweet.in_reply_to_status_id_str)
                    if 'media' in tweet_reply.entities:
                        for image in tweet_reply.entities['media']:
                            image_url = image['media_url']
                            req = requests.get(
                                'https://api.ocr.space/parse/imageurl?apikey='
                                + OCR_KEY + '&url=' + image_url)
                            res = json.loads(req.text)
                            reply_text = res['ParsedResults'][0]['ParsedText']
                            print('texto:', tweet.text, '\nid:', tweet.id)
                            try:
                                api.update_status(
                                    status='@' + tweet.user.screen_name +
                                    '\n' + reply_text,
                                    in_reply_to_status_id=tweet.id,
                                    auto_populate_reply_metadata=True)
                                insert_db(tweet.user.screen_name, tweet.id,
                                          datetime.now())
                                time.sleep(20)
                            except tweepy.TweepError as e:
                                print(e.reason)
            except tweepy.TweepError as e:
                print(e.reason)
            except StopIteration:
                break