質問

I have a bot script that connects to the Twitter API (using tweepy library), gets atm trending hashtags data and composes a tweet that ends with a hashtag from that list. It reads text by lines that are no longer than 120 characters, but sometimes it happens that the top trending hashtag is longer than 20 characters which causes the tweet to go over the 140 character twitter limit and that breaks the script running.

So I want to write a nested loop that will say something like this:

get twitter trends data
if the top hashtag is longer than 120 characters
then check the second one
and if even the second one is longer than 120 characters
wait for 15 min and check again
tweet when you find a hashtag than is smaller or same as 20 characters 

I tried this but it didn't work:

for line in f:
    trends1 = api.trends_place(1)
    # print trends1
    hashtags = [x['name'] for x in trends1[0]['trends'] if x['name'].startswith('#')]
    # print hashtags
    print hashtags[0]
    if len(hashtags[0]) <= 20:
        trend_hashtag = hashtags[0]
        api.update_status(line + trend_hashtag)
    elif len(hashtags[0]) > 20:
        trend_hashtag = hastags[1]
        api.update_status(line + trend_hashtag)
    elif len(hashtags[1]) > 20:
        time.sleep(900)
    time.sleep(10800)

I am just starting to learn python, what am I doing wrong, can you help me out here?

Connecting to twitter api and getting trending hashtags in a list works fine, the problem is just in the described loop. Thanks!

役に立ちましたか?

解決

Your first elif does not appear to match up to the specification you describe at the top of your question.

The if statement checks if the first hashtag is ≦20 characters. If so, it tweets; if not, it doesn't. So far, so good. Then, your first elif statement is guaranteed to execute - because if len(hashtags[0]) is not <= 20, then it certainly is > 20, and so you'll never get to the second elif. If you want to check whether or ot the second hashtag is ≦20 characters, your first elif should read as follows:

elif len(hashtag[1]) <= 20:
    trend_hashtag = hashtags[1] # you have a typo here btw
    api.update_status(line + trend_hashtag)

Finally, your last elif should just be an else. If you want it to sleep for 3 hours between tweets, you should have the call to time.sleep(10800) after each time it tweets. Taken together, your code should end up looking more like this:

for line in f:
    trends1 = api.trends_place(1)
    hashtags = [x['name'] for x in trends1[0]['trends'] if x['name'].startswith('#')]
    trend_hashtag = None
    if len(hashtags[0]) <= 20:
        trend_hashtag = hashtags[0]
    elif len(hashtags[1]) <= 20:
        trend_hashtag = hashtags[1]
    if trend_hashtag:
        api.update_status(line + trend_hashtag)
        time.sleep(3*60*60)
    else:
        time.sleep(15*60)

This way, you only do a 3-hour sleep after you've tweeted - otherwise (if you haven't tweeted), you only sleep for 15 minutes and then try again.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top