aboutsummaryrefslogtreecommitdiffstats
path: root/unloved_tweets.py
blob: 6a19c0082244647627e1729b3bd51febcfb385ad (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import tweepy
import datetime
import math
from settings import *

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)

tweet_ids_with_replies = []


# grab mentions to get latest tweets to see if things were replied to
# edge case: fails to work if there are 100 mentions on other tweets within 5 minutes
# if this happens to you why are you using this script, also, sorry
mentions = api.search(q=username)
for mention in mentions:
    parent_id = mention.in_reply_to_status_id
    if parent_id:
        tweet_ids_with_replies.append(parent_id)
        if DEBUG:
            print "adding %s to tweets with one or more replies" % mention.text


public_tweets = api.user_timeline()
for tweet in public_tweets:
    lifetime = datetime.datetime.utcnow()-tweet.created_at
    minutes = math.floor(lifetime.total_seconds() / 60)
    
    engagements = tweet.favorite_count + tweet.retweet_count

    # tweet was a reply to another tweet
    if tweet.in_reply_to_status_id:
        engagements = engagements + 1

    # tweet had one or more replies
    if tweet.id in tweet_ids_with_replies:
        engagements = engagements + 1


    if DEBUG:
        print "--------"
        print tweet.id
        print tweet.text
        print "%d engagements after %d minutes" % (engagements, minutes)
        if tweet.id in tweet_ids_with_replies:
            print "has one or more replies!"

    if minutes<60 and minutes>=MINUTES_TO_LIVE and engagements<REQUIRED_ENGAGEMENTS:
        if DEBUG:
            print "DELETING " + str(tweet.id)
            print tweet.text            

        with open(DELETED_TWEETS_FILE, "a") as f:
            f.write( tweet.text.encode('utf8') )
            f.write("\n\n")

        if not DEBUG:
            api.destroy_status(tweet.id)