def check_user_info(self): """ Searches for twitter user profile and saves the data :return: """ #connect to the api api = Tapi.TwitterAPI(cons_key, cons_secret, token, token_secret) #get request out = api.request('users/lookup', {'Name': '{}'.format(self.username), 'screen_name': '{}'.format(self.username)}) if out is not None: for i in out: info = i following = info['friends_count'] followers = info['followers_count'] join_date = info['created_at'] location = info['location'] description = info['description'] user_id = info['id_str'] #create user self.user = TwitterUser(username=info['screen_name'], location=location, description=description, date_joined=join_date, following=following, followers=followers,id=user_id) # save this user TwitterUserDA().save_user(self.user) else: self.user = TwitterUser(username=self.username, location="", description="*manually created*", date_joined="Joined June 2009", following='', followers='')
def lambda_handler(event, context): with open("credentials.json", "r") as creds_file: creds = json.load(creds_file) oauth_consumer_key = creds["oauth_consumer_key"] oauth_consumer_secret = creds["oauth_consumer_secret"] oauth_token = creds["oauth_token"] oauth_token_secret = creds["oauth_token_secret"] api = TwitterAPI(consumer_key=oauth_consumer_key, consumer_secret=oauth_consumer_secret, access_token_key=oauth_token, access_token_secret=oauth_token_secret) username = event.get('payload') msg = "Remember to LOCK the door!" event = { "event": { "type": "message_create", "message_create": { "target": { "recipient_id": username }, "message_data": { "text": msg } } } } send_msg = api.request('direct_messages/events/new', json.dumps(event)) print('SUCCESS' if send_msg.status_code == 200 else 'PROBLEM: ' + send_msg.text)
def send_tweet(message_to_tweet: str, url_to_media: str) -> int: """ Send a tweet with an image. :param message_to_tweet: Message to send :param url_to_media: Image to upload :return: Tweet ID (-1 if it failed) :raises Exception: Tweet failed to send for some reason """ twitter_api = TwitterAPI.TwitterAPI( config.get('twitter', 'CONSUMER_KEY'), config.get('twitter', 'CONSUMER_SECRET'), config.get('twitter', 'ACCESS_TOKEN_KEY'), config.get('twitter', 'ACCESS_TOKEN_SECRET'), ) logging.info('Tweet to send: {}'.format(message_to_tweet)) try: if url_to_media is not None: resize_image(url_to_media) photo = open(url_to_media, 'rb') status = twitter_api.request('statuses/update_with_media', {'status': message_to_tweet}, {'media[]': photo}) logging.info('Twitter Status Code: {}'.format(status.status_code)) response = TwitterAPI.TwitterResponse(status, False).json() logging.info('Twitter Response Parsed: {}'.format(response)) return int(response['id_str']) raise Exception("No image attached to tweet") except UnicodeDecodeError: logging.exception( 'Your message could not be encoded. Perhaps it contains non-ASCII characters?' ) raise Exception("Tweet failed to send")
def main(): app_settings = settings.get_app_settings() api = TwitterAPI.TwitterAPI(**app_settings) store_data = store.get_data() search_term = 'vnnlp' query = {'screen_name': search_term} filename = 'user_timeline/{}.yaml'.format(search_term) utils.ensure_dir(filename) if 'user_timeline' in store_data and 'max_id' in store_data[ 'user_timeline']: query['max_id'] = store_data['user_timeline']['max_id'] - 1 max_id = None try: with open(filename, 'a') as output_file: r = TwitterAPI.TwitterPager(api, 'statuses/user_timeline', query) for tweet in r.get_iterator(): yaml.dump([tweet], output_file, default_flow_style=False) if 'id' in tweet: max_id = tweet['id'] except KeyboardInterrupt: pass if not 'user_timeline' in store_data: store_data['user_timeline'] = {} store_data['user_timeline']['max_id'] = max_id store.store_data(store_data)
def main(): app_settings = settings.get_app_settings() api = TwitterAPI.TwitterAPI(**app_settings) search_term = 'dongng' query = {'screen_name': search_term} twitter.fetch_one(api, 'followers/list', search_term, query)
def setUp(self): """Read credentials from TwitterAPI/credentials.txt. You must copy your credentials into this text file. """ oa = TwitterAPI.TwitterOAuth.read_file() self.api = TwitterAPI.TwitterAPI(oa.consumer_key, oa.consumer_secret, oa.access_token_key, oa.access_token_secret)
def main(): app_settings = settings.get_app_settings() api = TwitterAPI.TwitterAPI(**app_settings) search_term = 'dongng' query = {'q': search_term} twitter.fetch(api, 'search/tweets', search_term, query)
def __init__(self, tweet_count=200, twitter_account='stackdevjobs'): self.tweet_count = tweet_count self.twitter_account = twitter_account self.api = TwitterAPI.TwitterAPI(self.consumer_key, self.consumer_secret, auth_type='oAuth2')
def tweetImage(message, image_file): api = ta.TwitterAPI(consumer_key, consumer_secret, access_token_key, access_token_secret) file = open(image_file, 'rb') data = file.read() r = api.request('statuses/update_with_media', {'status': message}, {'media[]': data}) return 'SUCCESS' if r.status_code == 200 else 'FAILURE'
def main(): api = TwitterAPI.TwitterAPI(**settings.get_app_settings()) # twitter.fetch(api, 'search/tweets', 'input', {'q': 'coccoc', 'lang': 'vi'}) # twitter.fetch(api, 'search/tweets', 'input', {'q': '#coccoc', 'lang': 'vi'}) # twitter.fetch(api, 'search/tweets', 'input', {'q': 'cntt', 'lang': 'vi'}) # twitter.fetch(api, 'search/tweets', 'input', {'q': 'ifan', 'lang': 'vi'}) # twitter.fetch(api, 'search/tweets', 'input', {'q': 'yêu', 'lang': 'vi'}) twitter.fetch(api, 'search/tweets', 'input', {'q': 'ghét', 'lang': 'vi'})
def get_results() -> Tuple[List[Dict[str, str]], List[Dict[str, str]]]: """ Get the results from the competition and print it out :return: Winner's name and their query """ twitter_api = TwitterAPI.TwitterAPI( config.get('twitter', 'CONSUMER_KEY'), config.get('twitter', 'CONSUMER_SECRET'), config.get('twitter', 'ACCESS_TOKEN_KEY'), config.get('twitter', 'ACCESS_TOKEN_SECRET'), ) valid_normal_entries: List[Dict[str, Any]] = [] valid_regex_entries: List[Dict[str, Any]] = [] logging.info('GET RESULTS') json_db: Dict[str, Any] = load_json_db( config.get('scryfallCardGolf', 'TWEET_DATABASE')) max_key: str = max(json_db.keys()) r = TwitterAPI.TwitterPager(twitter_api, 'statuses/mentions_timeline', { 'count': 200, 'since_id': json_db[max_key]['tweet_id'] }) for item in r.get_iterator(): if 'text' not in item: logging.warning('SUSPEND, RATE LIMIT EXCEEDED: ' + item['message']) break logging.info('[TWEET] ' + item['user']['screen_name'] + ': ' + item['text']) for url in item['entities']['urls']: test_url = url['expanded_url'] if 'scryfall.com' not in test_url: continue logging.info('{} submitted solution: {}'.format( item['user']['screen_name'], test_url)) test_query_results = test_query(item['user']['screen_name'], test_url) if test_query_results: user_json_entry: Dict[str, Any] = { 'name': item['user']['screen_name'], 'length': len(test_query_results), 'query': test_query_results } if re.search(r'/.+/', test_query_results): valid_regex_entries.append(user_json_entry) else: valid_normal_entries.append(user_json_entry) return valid_normal_entries, valid_regex_entries
def connect_to_streaming_API(): ''' Connect to twitter streaming API Returns an iterator with all the stream data. ''' api = TwitterAPI.TwitterAPI( 'f8olfZWtAPvgANdP9qecg', 'bSEnCXJuWazjT8S8hZ6BLWMo1C7egIKNgjObHM6Ck', '1726636778-jEn4qUAj2wV60ckbskNSbLJgTRr0c7hiemVOU7x', 'UgwEfM3cukoWIxCWjCiIZiJ0gnQVGH9U42WLfJjnEFODw') r = api.request('statuses/filter', {'track': '#rip'}) return r.get_iterator()
def getTweetCount(q, p=False, debug=False): auth = tweepy.OAuthHandler(consumer_key, consumer_secret) auth.set_access_token(access_token, access_token_secret) api = TwitterAPI.TwitterAPI(consumer_key, consumer_secret, access_token, access_token_secret) #today's info a = time.time() timeStamp = datetime.datetime.utcnow().date() tDay = timeStamp.day tMonth = timeStamp.strftime("%b") api = TwitterAPI.TwitterAPI(consumer_key, consumer_secret, access_token, access_token_secret) count = 0 r = TwitterPager(api, 'search/tweets', {'q': q, 'count': 100}) for item in r.get_iterator(wait=6): time_stamp = item['created_at'] day = int(time_stamp[8:10]) if (tDay != day and tDay - 1 != day): break if 'message' in item and item['code'] == 88: print('SUSPEND, RATE LIMIT EXCEEDED: %s' % item['message']) break if (tDay - 1 == day): count += 1 if (p): print("Term: " + q + " on " + item["created_at"]) if (debug): b = time.time() c = int(b - a) print("\nThis took " + str(round(c / 60, 2)) + " minutes") return count #res = tweet.getTweetCount("qwertyuiop", False,True) #print(res)
def __init__(self): self.name = 'TwitterApi' self.keys_dict = json.loads(open(twitter_key_path, 'r').read()) self.twitter_api = twitter.TwitterAPI( consumer_key=self.keys_dict['consumer_key'], consumer_secret=self.keys_dict['consumer_secret'], access_token_key=self.keys_dict['access_token_key'], access_token_secret=self.keys_dict['access_token_secret']) self.querying_types = [ # TwitterApi.ResultType.popular, # TwitterApi.ResultType.recent, TwitterApi.ResultType.mixed ]
def __init__(self, twitter_creds, host, port): self.api = TwitterAPI(twitter_creds["consumer_key"], twitter_creds["consumer_secret"], twitter_creds["access_token_key"], twitter_creds["access_token_secret"]) self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.s.bind((host, port)) print host, port self.s.listen(5) while True: try: c, addr = self.s.accept() self.c = c self.stream() except Exception as e: print e
def __init__( self, logger, consumer_key, consumer_secret, oauth_token, oauth_secret ): # initialize superclass Stream.__init__(self, logger) self.basetime = datetime.now() self.limit = 140 self.api = TwitterAPI.TwitterAPI( consumer_key, consumer_secret, oauth_token, oauth_secret )
def sendtotwitter(list2): filename = "/Users/kylebauder/Documents/Anki/User 1/collection.media/" + str( list2[1]) # ALL YOUR SECRET STUFF! # Consumer Key (API Key) cons_key = 'Y7dzXbP9Jx7HPnLyscaAcuzHD' # Consumer Secret (API Secret) cons_secret = 'OE9tDUnQLzJGZ6LcwliVt5OJ3M3u8Xdx21ETSJMQtaRT0qe9qA' # Access Token access_token = '953403732384886784-A4eYTNV8nfb8meOEoN0zbQvvchi0Agr' # Access Token Secret access_token_secret = 'jfm2s1wkORCc1HgGyCV28c7pRnMuGAzU4TvLgz0QOpTDR' api = TwitterAPI.TwitterAPI(cons_key, cons_secret, access_token, access_token_secret) file = open(filename, 'rb') data = file.read() hashtag = randomizer() stringtoadd = list2[0] + " >>> " + list2[2] + hashtag r = api.request('statuses/update_with_media', {'status': stringtoadd}, {'media[]': data})
def analyze(username): # collect data api = TwitterAPI.TwitterAPI() print 'Getting user information...' user = api.get_user(username) print 'Getting frineds information...' friends = api.get_friends(username) print 'Getting tweets...' tweets = api.get_statuses(username) print 'All data collected!' if not (user and friends and tweets): print 'Incomplete Data. No analysis performed.' return None res = {'analysis': {}, 'data': {}, 'user': copy.deepcopy(user)} # datetime is converted to ISO string to be serializable res['user']['created_at'] = res['user']['created_at'].isoformat() total = [] # user analysis print '\nPerforming user analysis...' res['analysis']['user'] = {} print 'Analyzing profile...' profileAnalyzer = UserAnalysis.ProfileAnalysis() res['analysis']['user']['profile'] = profileAnalyzer.analyze(user) total.append(res['analysis']['user']['profile']) print 'Analyzing friends...' friendsAnalyzer = UserAnalysis.FriendsAnalysis() res['analysis']['user']['friends'] = friendsAnalyzer.analyze(friends) total.append(res['analysis']['user']['friends']) # status analysis print '\nPerforming status analysis...' res['analysis']['status'] = {} print 'Analyzing frequency...' timeSeriesAnalyer = StatusAnalysis.TimeSeriesAnalysis() res['analysis']['status']['frequency'] = timeSeriesAnalyer.analyze(tweets) total.append(res['analysis']['status']['frequency']) print 'Analyzing sentiment...' sentimentAnalyzer = StatusAnalysis.SentimentAnalysis() res['analysis']['status']['sentiment'] = sentimentAnalyzer.analyze(tweets) total.append(res['analysis']['status']['sentiment']) print 'Analyzing words...' wordAnalyzer = StatusAnalysis.WordAnalysis() res['analysis']['status']['word'] = wordAnalyzer.analyze(tweets) total.append(res['analysis']['status']['word']) print 'Analysis done!' # data for front end print '\nPreparing data...' res['data']['friends'] = [] for friend in friends: info = { k: friend[k] for k in friend if k in ['screen_name', 'followers_count', 'profile_image_url'] } res['data']['friends'].append(info) # top 10 words, only work after word analysis words_count = wordAnalyzer.stats['words_count'] res['data']['words'] = map( lambda k: (k, words_count[k]), heapq.nlargest(10, words_count, key=lambda k: words_count[k])) print 'Data prepared!' # total score #res['analysis']['total'] = sum(total) / len(total) res['analysis']['total'] = min(total) return res
'root': { 'level': 'INFO', 'handlers': ['wsgi'] } }) app = flask.Flask(__name__) kinesis = boto3.client( 'kinesis', aws_access_key_id=CONSTANTS.get('AWS').get('aws_access_key_id'), aws_secret_access_key=CONSTANTS.get('AWS').get('aws_secret_access_key'), region_name=CONSTANTS.get('AWS').get('region_name'), ) twitter_client = twitter.TwitterAPI( CONSTANTS.get('TWITTER').get('consumer_key'), CONSTANTS.get('TWITTER').get('consumer_secret'), CONSTANTS.get('TWITTER').get('access_token_key'), CONSTANTS.get('TWITTER').get('access_token_secret'), ) topics = ['trans', 'transgender'] hook = twitter_hook.TwitterDataStream( kinesis, twitter_client, logger=app.logger, topics=topics ) @app.route('/') def index(): if hook.is_alive(): latest_tweet = twitter_hook.TwitterDataStream.latest_tweet
def get_twitter(config_file): token1, token2, token3, token4 = '' # To be replaced with tokens config = configparser.ConfigParser() config.read(config_file) twitter = TwitterAPI.TwitterAPI(token1, token2, token3, token4) return twitter
import sys import json import os.path import time import requests #Credencias de acesso App Twitter consumer_key = "NBL0CtVrn2ajbpaGEWC1GBY2c" consumer_secret = "2F5Uz5VYg0ONu4xTYYZsWkAGfc3TYXCkXLCsXMJ1eCKOfhBTfS" access_token = "2345718031-we2K2PETQXkz7NCexjdGuvE2L2rnd5KfouzN3Up" access_token_secret = "aEQPKGifu1y29Wbh3u6Z0YIcjAsBC8VeD4Y75CDL2r12o" #acessa OAuth # Referencia para API: https://dev.twitter.com/rest/reference twitter = TwitterAPI(consumer_key, consumer_secret, auth_type='oAuth2') ##DataBase s client = MongoClient() db = client.baseTweetsTCC tweets = db.tweets.find() users_count = 0 #print('Buscando...\n') #print('Isso Pode Demorar Um Pouco..\n') it = 1 for document in tweets: try:
import json import TwitterAPI import database from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer with open('./config.json', encoding='utf-8') as f: data = json.load(f) api_key = data['api_key'] api_secret = data['api_secret'] access_key = data['access_key'] access_secret = data['access_secret'] url = data['url'] locations = data['locations'] api = TwitterAPI.TwitterAPI(api_key, api_secret, access_key, access_secret) db = database.Couchdb(url) analyzer = SentimentIntensityAnalyzer() def extract(tweet): if 'text' not in tweet or tweet['text'].startswith('RT'): return None if 'place' not in tweet or 'full_name' not in tweet['place']: return None location = tweet['place']['full_name'] if 'Queensland' in location: db_name = 'queens' elif 'New South Wales' in location: db_name = 'nsw' elif 'Victoria' in location:
def get_authentication(): api = TwitterAPI.TwitterAPI(twitter_credentials.consumer_key, twitter_credentials.consumer_secret, twitter_credentials.access_token, twitter_credentials.access_token_secret) return api
def auth_lib(self): auth = TwitterAPI(self.consumer_key, self.consumer_secret, self.access_token, self.access_secret) return auth
app = flask.Flask(__name__) # filename? fn = 'config.json.example' if os.path.isfile('config.json'): fn = 'config.json' print("opening fn: " + fn) with open(fn) as json_data_file: config = json.load(json_data_file) tweet_limit = config["tweet_limit"] #AUTH api = TwitterAPI.TwitterAPI(config["consumer_key"], config["consumer_secret"], config["access_token_key"], config["access_token_secret"]) # HELPERS def statuses(username): r = api.request("statuses/user_timeline", { "count": tweet_limit, "screen_name": username }) return r.get_iterator() #def statuses(username): # return [{'egg': 'yes', 'ham': 'yes', 'spam': 'no'}, {'egg': 'yes', 'ham': 'yes', 'spam': 'no'}, {'egg': 'yes', 'ham': 'yes', 'spam': 'no'}]
def auth(self): self.api = TwitterAPI(self.CONSUMER_KEY, self.CONSUMER_SECRET, self.ACCESS_TOKEN, self.ACCESS_TOKEN_SECRET)
consumer_secret = 'eig9biZmFBVK8iy1VobOwwIsvaD7jM6fF0ODmUulc8yTJVpaCZ' filename = 'twitter_output.txt' file = open(filename, 'a') filename_id = 'since_id.txt' file_id = open(filename_id, 'r') if os.stat(filename_id).st_size == 0: since_id = '' else: ids = file_id.readlines() since_id = ids[-1] file_id.close() api = TwitterAPI.TwitterAPI(consumer_key=consumer_key, consumer_secret=consumer_secret, auth_type='oAuth2') if since_id: pager = TwitterAPI.TwitterRestPager(api, 'statuses/user_timeline', { 'count': 200, 'screen_name': 'wx_copenhagen', 'since_id': since_id }) else: pager = TwitterAPI.TwitterRestPager(api, 'statuses/user_timeline', { 'count': 200, 'screen_name': 'wx_copenhagen' })
def __init__(self, endpoint, params, user=None, rcfile=None, times=1): self.endpoint = endpoint self.params = params self.api = T.TwitterAPI(*keys.keys(user, rcfile)) self.times = times self.stop, self.cursor, self.response = self.get_first()
def __init__(self): self.t_api = twitter_api.TwitterAPI("keys.json")
CHECKINS_QUEUE.put_nowait(None) break if __name__ == '__main__': # pylint: disable=C0103 if DB: DB.checkin.ensure_index([('loc', cm.pymongo.GEOSPHERE), ('lid', cm.pymongo.ASCENDING), ('city', cm.pymongo.ASCENDING), ('time', cm.pymongo.ASCENDING)]) SAVE = save_checkins_mongo if ARGS.mongodb else th.save_checkins_json config = ConfigParser.ConfigParser() config.read('api_keys.cfg') api = twitter.TwitterAPI(config.get('twitter', 'consumer_key'), config.get('twitter', 'consumer_secret'), config.get('twitter', 'access_token'), config.get('twitter', 'access_token_secret')) accu = Thread(target=accumulate_checkins, name='AccumulateCheckins') accu.daemon = True accu.start() start = clock() end = start + ARGS.duration * 60 * 60 failures = th.Failures(initial_waiting_time=2.0) while clock() < end: try: read_twitter_stream(api, end) except (KeyboardInterrupt, SystemExit): CHECKINS_QUEUE.put_nowait(None) raise except: msg = 'Fail to read or enqueue tweet\n'