twython icon indicating copy to clipboard operation
twython copied to clipboard

retweet_count field is always zero

Open ghost opened this issue 13 years ago • 3 comments

please fix to update this field

ghost avatar May 27 '13 14:05 ghost

I'm not sure what you mean by this. Do you mean you are making a call to Twitter and grabbing statuses and the retweet_count that Twitter is returning is always 0?

michaelhelmick avatar May 28 '13 00:05 michaelhelmick

That is exactly the problem I'm now encountering. I call to the 'retweet_count' object and it always returns a zero. Every other field I've pulled seems to work fine, except 'retweeted' always pulls a 'false' as well. Do you think this is on Twython's side of things? Any ideas?

Code below:

# Script connects to Streaming API and writes climate-related tweets to a file.
import json
import datetime
import time
import sys
import traceback

from twython import TwythonStreamer


class MyStreamer(TwythonStreamer):
    # Save the files to a different directory.
    twitterDirectory = ''
    #twitterDirectory = ''

    def on_success(self, data):
        fileName = self.twitterDirectory + datetime.datetime.now().strftime("%Y_%m_%d") + "_climate.txt"
        todaysFile = open(fileName, 'a')
        #note: first work with all 'non nullable' fields, then add in possible nullible fields.
        try:
            if 'text' in data and 'id_str' in data and 'created_at' in data and 'user' in data: 
                retweetCount = str(data['retweet_count']).encode('utf-8', 'ignore').replace("\n", "").replace("\r", "")
                retweeted = str(data['retweeted']).encode('utf-8', 'ignore').replace("\n", "").replace("\r", "")
                tweetParts = data['text'].encode('utf-8', 'ignore').replace("\n", "").replace("\r", "") + "\t" + data['id_str'].encode('utf-8', 'ignore').replace("\n", "").replace("\r", "") + "\t" +  data['user']['id_str'].encode('utf-8', 'ignore').replace("\n", "").replace("\r", "") + "\t" + retweetCount + "\t" + retweeted

                todaysFile.write(tweetParts + "\n")
                heartbeatFile = open(self.twitterDirectory + "climateHeartbeat.txt", 'w')
                heartbeatFile.write("streamclimate.py last got a tweet at " + datetime.datetime.now().strftime("%b %d %Y %H:%M:%S") +"\n")
        except:
            errorFile = open(self.twitterDirectory + "climateError.txt", 'w')
            errorFile.write("Unexpected error: " + str(traceback.format_exc()) + "\n at " + datetime.datetime.now().strftime("%b %d %Y %H:%M:%S") + "\n")

    def on_error(self, status_code, data):
        errorFile = open(self.twitterDirectory + "climateError.txt", 'w')
        errorFile.write("Twitter stream error: " + status_code + "\n at " + datetime.datetime.now().strftime("%b %d %Y %H:%M:%S") + "\n" + data)

    def on_disconnect(self, data):
        time.sleep(10)
        connectAndStream(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET)

APP_KEY = ""
APP_SECRET = ""
OAUTH_TOKEN = ""
OAUTH_TOKEN_SECRET = ""

def connectAndStream(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET):
    stream = MyStreamer(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
    stream.statuses.filter(track="keywords")

connectAndStream(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET)

nickobradovich avatar Mar 04 '14 17:03 nickobradovich

OK, disregard the above problem. For anyone interested in the future, need to call ['retweeted_status']['retweet_count'] to get the running count of times original tweet has been repeated. ['retweet_count'] will only give the number of retweets for that specific tweet, which is usually zero.

nickobradovich avatar Mar 04 '14 18:03 nickobradovich