def getAuth(): "get Tweepy OAuthHandler authentication object" config = getConfig() auth = tweepy.OAuthHandler(config["twitter"]["consumer_key"], config["twitter"]["consumer_secret"]) auth.set_access_token(config["twitter"]["access_token"], config["twitter"]["access_token_secret"]) return auth
def getWhitelist(): "get user whitelist" whitelistfilename = getConfig()["whitelist"]["file"] rt_bot_path = os.path.dirname(os.path.abspath(__file__)) wlpath = os.path.join(rt_bot_path, whitelistfilename) with open(wlpath, 'r') as f: whitelist = [int(line.rstrip('\n')) for line in f] logger.debug(whitelist) return whitelist
def okrt(status): "should we RT this status?" if status['user']['id'] == getConfig()['settings']['bot_id']: logger.debug("self status, no RT") return False ok = not isrt(status) and \ not isquote(status) and \ not isreply(status) and \ iswhitelist(status) and \ not isreplacement(status) logger.debug("is this status ok for RT? %s", ok) return ok
from TwitterAPI import TwitterAPI, constants import json import cfg # Uses TwitterAPI to generate a twarc by monkeypatching in Twitter's private conversation API. constants.ENDPOINTS.update({'conversation/show/:PARAM': ('GET', 'api')}) api = TwitterAPI(cfg.getConfig()["twitter"]["consumer_key"], cfg.getConfig()["twitter"]["consumer_secret"], cfg.getConfig()["twitter"]["access_token"], cfg.getConfig()["twitter"]["access_token_secret"]) twid = 210290960695959553 r = api.request('conversation/show/:%d' % twid, {"count":100}) for item in r: print(json.dumps(item))
#Import the necessary methods from tweepy library from tweepy.streaming import StreamListener #from tweepy import OAuthHandler from tweepy import Stream import json from cfg import getConfig from twitter import getAuth from doctoctocbot import okrt, retweet from log import setup_logging import logging setup_logging() logger = logging.getLogger(__name__) config = getConfig() def printStatus(status): "output stuff" json_str = json.dumps(status._json) print(json_str) print("\n") return #This is a basic listener that just prints received tweets to stdout. class StdOutListener(StreamListener): def on_status(self, status): logger.debug(status._json) logger.debug("is this status ok for RT? %s", str(okrt(status._json))) if okrt(status._json):
json objects collected with TweetScraper contain a "nbr_reply" field. json objects collected with basic Twitter API lack "reply_count field. This script adds this field. """ import cfg import json import os from log import setup_logging import logging import myprettyprint setup_logging() logger = logging.getLogger(__name__) pathin = cfg.getConfig()["settings"]["doctoctoc"] pathout = cfg.getConfig()["settings"]["complete"] partial_tweets = [] complete_tweets = [] for filename in os.listdir(pathin): path = pathin + filename with open(path, mode='r') as f: partial_tweets.append(json.load(f)) for filename in os.listdir(pathout): path = pathout + filename with open(path, mode='r') as f: complete_tweets.append(json.load(f))
#!/usr/bin/env python # -*- coding: utf-8 -*- u"""" json objects collected with TweetScraper contain a "nbr_reply" field. json objects collected with basic Twitter API lack "reply_count field. This script adds this field. """ import cfg import json import os from log import setup_logging import logging import myprettyprint setup_logging() logger = logging.getLogger(__name__) path = cfg.getConfig()["settings"]["complete"] tweets = [] for filename in os.listdir(path): p = path + filename with open(p, mode='r') as f: tweets.append(json.load(f)) sum = 0 for tweet in tweets: sum += tweet["reply_count"] print(sum)