def respond_to_requests(count=10, testing=0):
    complete_new_requests = Database.RequestDB(
        testing=testing).get_unresponded_list()
    new_requests = complete_new_requests[:count]
    # Count limits the number of requests. This way I don't get fatigued.
    assert len(new_requests) <= count
    if len(new_requests) == 0:
        print("No new requests")
        return
    print(f"{len(complete_new_requests)} new requests!")
    for request in new_requests:
        my_resp_dict = {
            'row_date': request.row_date,
            'link': request.link,
            'testing': request.testing
        }
        pprint.pprint(request.text)
        time.sleep(1)
        my_response = input(
            "How would you like to respond to this request? Press x to void it.\n\n"
        )
        if my_response == '':
            print("Request Skipped.")
            continue
        elif my_response.lower() == 'x':
            print("Request voided.")
            Database.RequestDB(testing=testing).update_row_to_responded(
                unique_id=request.unique_id)
            continue
        elif len(my_response) == 1:
            my_response = input(
                "Are you sure? That's only one letter. Try again!")
        my_like_dict = my_resp_dict  # Copy the dictionary into two rows.
        # This way there will be a like action and comment action

        my_like_dict['directive'] = 'like'
        my_resp_dict['directive'] = 'comment'
        my_resp_dict['text'] = my_response
        my_resp_row = Database.ActionRow(action_dict=my_resp_dict)
        my_like_row = Database.ActionRow(action_dict=my_like_dict)
        Database.ActionDB(testing=testing).add_to_db(my_resp_row.get_dict())
        Database.ActionDB(testing=testing).add_to_db(my_like_row.get_dict())
        Database.RequestDB(testing=testing).update_row_to_responded(
            unique_id=request.unique_id)
        time.sleep(1)
        print("Response Added to Database.")
Ejemplo n.º 2
0
def add_csv_to_database(csv_path, testing=0):
    with open(csv_path, 'r+', encoding='utf-8') as f:
        reader = csv.reader(f)
        master_list = list(reader)
    test_csv(master_list)
    list_of_rows = []
    problem_list = []
    for row in master_list:
        try:
            my_dict = {
                'link': row[0],
                'directive': row[1],
                'row_date': datetime.datetime.now()
            }
            if len(row) == 3:
                my_dict['text'] = row[2]
            list_of_rows.append(Database.ActionRow(my_dict))
        except Exception as e:
            print(f"Problem encountered: {e}")
            problem_list.append(row)
    for i in list_of_rows:
        try:
            Database.ActionDB(testing=testing).add_to_db(row_dict=i.get_dict())
        except Exception as e:
            print(
                f"Problem encountered: {e}, adding this row to the problem list"
            )
            problem_list.append(i.get_dict())

    print(f"Finished adding all rows to database")
    with open(csv_path, 'w+') as f:
        writer = csv.writer(f)
        writer.writerows([])

    if len(problem_list) > 0:
        with open('data_discover_followers/problem_rows.csv', 'w+') as f:
            writer = csv.writer(f)
            writer.writerows(problem_list)
    url = f"https://twitter.com/{i.user.screen_name}/status/{i.id}"
    print(url)
    comment = input("To reply push R, to like push L\n\n").lower()
    if comment == '':
        continue
    my_dict = {'link': url,
               'row_date': datetime.datetime.now()}
    if comment == 'r':
        print("Reply Process\n\n")
        my_dict['text'] = input("What would you like to reply with?\n\n")
        my_dict['directive'] = 'comment'
    elif comment == 'l':
        print("Like Process")
        my_dict['directive'] = 'like'
        my_dict['text'] = ''
    my_action = Database.ActionRow(my_dict)  # Create a Row Object
    print(f"Adding to database:\n"
          f"Link: {my_dict['link']}\n"
          f"Directive: {my_dict['directive']}\n"
          f"Text: {my_dict['text']}\n"
          f"Unique_ID: {my_action.unique_id}")
    confirmation = input("Does this look okay? Hit x to cancel")
    if confirmation.lower() == 'x':
        continue
    Database.ActionDB().add_to_db(row_dict=my_action.get_dict())  # Add to Database
    print(f"Rate Limit: {my_helper.get_remaining(api)}")
    count = count + 1
    my_helper.update_progress(float(format(count / len(mentions), '.2f')))

with open('data_discover_followers/last_time.txt', 'w') as f:
    f.write(current_time)