kurozael / twitter-contest-bot

Will poll for Retweet Contests and retweet them. Inspired by http://www.hscott.net/twitter-contest-winning-as-a-service/
236 stars 107 forks source link

Issue #7

Closed hawkchomp closed 6 years ago

hawkchomp commented 9 years ago

=== SCANNING FOR NEW CONTESTS === Getting new results for: RT to win Could not connect to TwitterAPI - are your credentials correct? Traceback (most recent call last): File "main.py, line 179, in ScanForContests() FILE "main.pyu", line 176, in ScanForContests Print("Exception: " + e) TypeError: cannot concatenate 'str' and 'exceptions.NameError' objects

hawkchomp commented 9 years ago

Credential are correct, I can run the original file and retweet.

silverstar194 commented 9 years ago

Try hardcoding your credentials into the main.py doc. (Set each variable at top directly instead of pulling them in form the .json file)

hawkchomp commented 9 years ago

Thanks silverstar194 - sadly that is not something I am capable of LOL. I can barely function in Python

kurozael commented 9 years ago

Did you rename config.json.example to config.json?

hawkchomp commented 9 years ago

I did rename config.json - not sure why this isn't working :-/

hawkchomp commented 9 years ago

I'm wondering if it isn't finding the TwitterAPI during the import - my credentials have been hardcoded to the file for testing and it still doesn't function.

hawkchomp commented 9 years ago

Reinstalled the TwitterAPI - still an error somewhere in the ScanForContests()

Merubokkusu commented 9 years ago

Getting the same thing. Its reading the credentials right, I was able to print them into the console.

Wawatasing commented 9 years ago

Same error - I fixed the error message that is throwing the TypeError(last few lines of the error message) in the main.py and now I'm getting this:

=== SCANNING FOR NEW CONTESTS === Getting new results for: RT to win Could not connect to TwitterAPI - are your credentials correct? Exception: local variable 'original_screen_name' referenced before assignment Getting new results for: Retweet and win Could not connect to TwitterAPI - are your credentials correct? Exception: local variable 'original_screen_name' referenced before assignment === CHECKING RETWEET QUEUE === Queue length: 0

I would be really happy if someone could help! :)

hawkchomp commented 9 years ago

Me too. Still no luck! :-(

Wawatasing commented 9 years ago

I tried it on two different systems and it does not work. Is it possible that there's a problem with the Twitter Developer Account? Do you have to change some settings or do a setup in any form?

flyingsriracha commented 9 years ago

anyone figured out?? im stucked too

AuxProc commented 9 years ago

Same issue. I deleted the concatenation on line 176 to see if a different error shows up, and I get: "Could not connect to TwitterAPI - are your credentials correct? local variable 'original_screen_name' referenced before assignment"

rotersand commented 8 years ago

apparently line 112 have a kind of bad character. just delete the that line and write again.

flyingsriracha commented 8 years ago

i don't see anything weird on line 112. i retyped it. still got the could not connect to twitterAPI error. hmm...i wonder what is going on with twitterapi. i even reinstall the twitterapi

hawkchomp commented 8 years ago

Sames - I deleted the line and nothing happens

rotersand commented 8 years ago

ok, this is what i did: uncomment the following line: 26, 101, 126, 127, 172. then i write again the line 112. screen shot 2015-09-10 at 2 45 54 pm

hawkchomp commented 8 years ago

Loading ignore list [] ===SCANNING FOR NEW CONTESTS === Getting new results for: RT to win Could not connect to TwitterAPI - are your credentials correct? Traceback (most recent call last): File "main.py", line 179 in module ScanForContests() File "main.py", line 176, in ScanForContests print("exception: " + e) TypeError: cannot concatenate 'str' and 'exceptions.UnboundLocalError' objects

flyingsriracha commented 8 years ago

Rotersand, I did what you suggested uncomment those lines and rewrite line 112. Issue still there.

=== SCANNING FOR NEW CONTESTS === Getting new results for: RT to win Could not connect to TwitterAPI - are your credentials correct? Exception: local variable 'original_screen_name' referenced before assignment Getting new results for: Retweet and win Could not connect to TwitterAPI - are your credentials correct? Exception: local variable 'original_screen_name' referenced before assignment === CHECKING RETWEET QUEUE === Queue length: 0

Hawkchom. for that 176 line exception. this is what I did. add str(e) print("Exception: " + str(e))

rotersand commented 8 years ago

Well so i have no idea what i did but is working right now, haha. how i can help you guys? i never used github before.

hawkchomp commented 8 years ago

Rotersand - want to just past the code you are using here so I can test it?

hawkchomp commented 8 years ago

FlyingSriracha - now I am getting the same error you are.

rotersand commented 8 years ago

sure, here it is:

from TwitterAPI import TwitterAPI
import threading
import time
import json
import os.path

# Load our configuration from the JSON file.
with open('config.json') as data_file:    
    data = json.load(data_file)

# These vars are loaded in from config.
consumer_key = data["consumer-key"]
consumer_secret = data["consumer-secret"]
access_token_key = data["access-token-key"]
access_token_secret = data["access-token-secret"]
retweet_update_time = data["retweet-update-time"]
scan_update_time = data["scan-update-time"]
search_queries = data["search-queries"]
follow_keywords = data["follow-keywords"]
fav_keywords = data["fav-keywords"]

# Don't edit these unless you know what you're doing.
api = TwitterAPI(consumer_key, consumer_secret, access_token_key, access_token_secret)
post_list = list()
ignore_list = list()
last_twitter_id = 0

if os.path.isfile('ignorelist'):
    print("Loading ignore list")
    with open('ignorelist') as f:
        ignore_list = f.read().splitlines()
    f.close()
    print ignore_list
    time.sleep(1)

# Print and log the text
def LogAndPrint( text ):
    tmp = text.replace("\n","")
    print(tmp)
    f_log = open('log', 'a')
    f_log.write(tmp + "\n")
    f_log.close()

# Update the Retweet queue (this prevents too many retweets happening at once.)
def UpdateQueue():
    u = threading.Timer(retweet_update_time, UpdateQueue)
    u.daemon = True;
    u.start()

    print("=== CHECKING RETWEET QUEUE ===")

    print("Queue length: " + str(len(post_list)))

    if len(post_list) > 0:
        post = post_list[0]
        LogAndPrint("Retweeting: " + str(post['id']) + " " + str(post['text'].encode('utf8')))

        CheckForFollowRequest(post)
        CheckForFavoriteRequest(post)

        api.request('statuses/retweet/:' + str(post['id']))
        post_list.pop(0)

# Check if a post requires you to follow the user.
# Be careful with this function! Twitter may write ban your application for following too aggressively
def CheckForFollowRequest(item):
    text = item['text']
    if any(x in text.lower() for x in follow_keywords):
        try:
            api.request('friendships/create', {'screen_name': item['retweeted_status']['user']['screen_name']})
            LogAndPrint("Follow: " + item['retweeted_status']['user']['screen_name'])
        except:
            user = item['user']
            screen_name = user['screen_name']
            api.request('friendships/create', {'screen_name': screen_name})
            LogAndPrint("Follow: " + screen_name)

# Check if a post requires you to favorite the tweet.
# Be careful with this function! Twitter may write ban your application for favoriting too aggressively
def CheckForFavoriteRequest(item):
    text = item['text']
    if any(x in text.lower() for x in fav_keywords):
        try:
            api.request('favorites/create', {'id': item['retweeted_status']['user']['id']})
            LogAndPrint("Favorite: " + item['retweeted_status']['user']['id'])
        except:
            api.request('favorites/create', {'id': item['id']})
            LogAndPrint("Favorite: " + item['id'])

# Scan for new contests, but not too often because of the rate limit.
def ScanForContests():
    t = threading.Timer(scan_update_time, ScanForContests)
    t.daemon = True;
    t.start()

    global last_twitter_id

    print("=== SCANNING FOR NEW CONTESTS ===")

    for search_query in search_queries:

        print("Getting new results for: " + search_query)

        try:
            r = api.request('search/tweets', {'q':search_query, 'result_type':"mixed", 'count':100})
#           r = api.request('search/tweets', {'q':search_query, 'since_id':last_twitter_id, 'result_type':"mixed", 'count':100})
            c=0

            for item in r:

                c=c+1
                user_item = item['user']
                screen_name = user_item['screen_name']
                text = item['text']
                text = text.replace("\n","")
                id = str(item['id'])
                original_id=id
                is_retweet = 0

                if (item['id'] > last_twitter_id):
                    last_twitter_id = item['id']

                if 'retweeted_status' in item:

                    is_retweet = 1
                    original_item = item['retweeted_status']
                    original_id = str(original_item['id'])
                    original_user_item = original_item['user']
                    original_screen_name = original_user_item['screen_name']

                if not original_id in ignore_list:

                    if not original_screen_name in ignore_list:

                        if item['retweet_count'] > 0:

                            post_list.append(item)
                            f_ign = open('ignorelist', 'a')

                            if is_retweet:
                                print(id + " - " + screen_name + " retweeting " + original_id + " - " + original_screen_name + ": " + text)
                                ignore_list.append(original_id)
                                f_ign.write(original_id + "\n")
                            else:
                                print(id + " - " + screen_name + ": " + text)
                                ignore_list.append(id)
                                f_ign.write(id + "\n")

                            f_ign.close()

                    else:

                        if is_retweet:
                            print(id + " ignored: " + original_screen_name + " on ignore list")
                        else:
                            print(original_screen_name + " in ignore list")

                else:

                    if is_retweet:
                        print(id + " ignored: " + original_id + " on ignore list")
                    else:
                        print(id + " in ignore list")

            print("Got " + str(c) + " results")
            print("Last ID: " + str(last_twitter_id))

        except Exception as e:
            print("Could not connect to TwitterAPI - are your credentials correct?")
            print("Exception: " + e)

ScanForContests()
UpdateQueue()

while (True):
    time.sleep(1)
hawkchomp commented 8 years ago

Doesn't work - this is crazy stuff

hawkchomp commented 8 years ago

rotersand - did you do anything with the travis file? I already have a working twitterAPI with the old file and didn't use that.

Where did you save the individual files at? I am thinking maybe it isn't locating the config.json file, or the twitterAPI somehow.

rotersand commented 8 years ago

i dont have that file in my folder. screen shot 2015-09-10 at 6 32 12 pm

hawkchomp commented 8 years ago

Oh - but you have an ignorelist I don't have

What is in the log and ignorlist file? Could you shoot me the text for your config file?

rotersand commented 8 years ago

that file is automatically generated when the code works

hawkchomp commented 8 years ago

Oh - got it.

AuxProc commented 8 years ago

Check out my fork: https://github.com/ModusVivendi/twitter-contest

I had the same exact issue in this thread, and I think it has something to do with the scope of some of the variables created in ScanForContests and incompatibility with the version of Python. I haven't got @PhilippBehmer rate limit changes working yet, but everything else seems to be working on my fork with Python 2.7.6. It's sort of crude as I don't have much Python experience, but its working okay for me for 1 day so far.

rotersand commented 8 years ago

hawkchomp, what "Access Level" have you twitter API? may be this is the problem...

hawkchomp commented 8 years ago

rotersand - I just changed to read/write/accessDM

hawkchomp commented 8 years ago

ModusVivendi - works, still says the error that I can't connect to TwitterAPI - but it is retweeting

Wawatasing commented 8 years ago

ModusVivendi - you are my hero <3

AuxProc commented 8 years ago

I have added CheckRateLimit back into my fork, and updated the config to reflect the ability to post more frequently based on the rate limit. Everything that @PhilippBehmer added and the changes to fix this issue seem to be working on my fork.

flyingsriracha commented 8 years ago

it works! however. i don't see i'm following them. does that work? Edit. nevermind it does work. it's just the way the tweet have to word it correctly. hm I will have to modify the config file to have more terms.

airjer commented 8 years ago

@ModusVivendi 's version seems to load and see listings, but it sure isn't doing anything on my account.

AuxProc commented 8 years ago

@airjer you can open an issue on my app's page if you would like. In reference to your issue, my app schedules specific times of the day to make posts so that posting seems more manual. On that note, it could just be that you had not waited until the first post was scheduled.

airjer commented 8 years ago

You're right. Seems to be working now. Thanks.

On Oct 11, 2015, 5:25 PM -0400, ModusVivendinotifications@github.com, wrote:

@airjer(https://github.com/airjer)you can open an issue on my app's page if you would like. In reference to your issue, my app schedules specific times of the day to make posts so that posting seems more manual. On that note, it could just be that you had not waited until the first post was scheduled.

— Reply to this email directly orview it on GitHub(https://github.com/kurozael/twitter-contest-bot/issues/7#issuecomment-147246903).

charlesfcoombsiv commented 8 years ago

using python 3.5.2 Figured out the original_screen_name issue. Around line 190 after original_id=id put original_screen_name=screen_name Line 204 is looking for the variable and wont run if the variable isnt initialized