def resolve_twitter_ids(): r = get_redis_connection() # TODO: we can't use our non-overlapping task decorator as it creates a loop in the celery resolver when registering if r.get("resolve_twitter_ids_task"): # pragma: no cover return with r.lock("resolve_twitter_ids_task", 1800): # look up all 'twitter' URNs, limiting to 30k since that's the most our API would allow anyways twitter_urns = ContactURN.objects.filter( scheme=URN.TWITTER_SCHEME, contact__status=Contact.STATUS_ACTIVE).exclude(contact=None) twitter_urns = twitter_urns[:30000].only("id", "org", "contact", "path") api_key = settings.TWITTER_API_KEY api_secret = settings.TWITTER_API_SECRET client = Twython(api_key, api_secret) updated = 0 print("found %d twitter urns to resolve" % len(twitter_urns)) # contacts we will stop stop_contacts = [] # we try to look these up 100 at a time for urn_batch in chunk_list(twitter_urns, 100): screen_names = [u.path for u in urn_batch] screen_map = {u.path: u for u in urn_batch} # try to fetch our users by screen name try: resp = client.lookup_user(screen_name=",".join(screen_names)) for twitter_user in resp: screen_name = twitter_user["screen_name"].lower() twitter_id = twitter_user["id"] if screen_name in screen_map and twitter_user["id"]: twitterid_urn = URN.normalize( URN.from_twitterid(twitter_id, screen_name)) old_urn = screen_map[screen_name] # create our new contact URN new_urn = ContactURN.get_or_create(old_urn.org, old_urn.contact, twitterid_urn, priority=50) # if our new URN already existed for another contact and it is newer # than our old contact, reassign it to the old contact if (new_urn.contact != old_urn.contact and new_urn.contact.created_on > old_urn.contact.created_on): new_urn.contact = old_urn.contact new_urn.save(update_fields=["contact"]) # get rid of our old URN ContactURN.objects.filter(id=old_urn.id).update( contact=None) del screen_map[screen_name] updated += 1 except Exception as e: # if this wasn't an exception caused by not finding any of the users, then break if str(e).find("No user matches") < 0: # exit, we'll try again later print("exiting resolve_twitter_ids due to exception: %s" % e) break # add all remaining contacts to the contacts we will stop for contact in screen_map.values(): stop_contacts.append(contact) # stop all the contacts we couldn't resolve that have only a twitter URN stopped = 0 for contact_urn in stop_contacts: contact = contact_urn.contact if len(contact.urns.all()) == 1: contact.stop(contact.created_by) stopped += 1 if len(twitter_urns) > 0: print("updated %d twitter urns, %d stopped" % (updated, len(stop_contacts)))
def user_handle(): return Twython(CONSUMER_KEY, CONSUMER_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
# Importh the modules from twython import Twython import sys # Import Twitter API keys from twitter_auth import * # Set authentication twitter = Twython( consumer_key, consumer_secret, access_token, access_secret ) # Get message tweet = raw_input("What do you want to tweet? ") # Send tweet if tweet == "": print("Unable to tweet nothing!") sys.exit() else: twitter.update_status(status=tweet) print("Tweeted: %s" % tweet)
tweetRepository = TweetRepository.TweetRepository(credentials["dbcoll"], credentials["dbdb"], credentials["mongostring"]) otherClient = MongoClient(credentials["mongostring"]) krypton_db = otherClient[credentials["dbdb"]] ids_coll = krypton_db['tweets_ids_in_reply_to_status'] #----------------------------------------------------------------------- # load our API credentials #----------------------------------------------------------------------- config = {} filename = os.path.join('/opt/libs', 'key_twitter_2.py') exec(open(filename).read(), config) #----------------------------------------------------------------------- # create twitter API object #----------------------------------------------------------------------- twitter = Twython(config["consumer_key"], config["consumer_secret"], config["access_key"], config["access_secret"]) #----------------------------------------------------------------------- # Params and Vars #----------------------------------------------------------------------- queue = Queue.Queue() delay = 60 #----------------------------------------------------------------------- # Infinite loop #----------------------------------------------------------------------- #Put minions to work for minion in ["Minion-1", "Minion-2"]: queue.put(minion) #Forever while True: minion = queue.get() #this will block until some minion is free print minion, 'goes to work!'
import config import random from twython import Twython import time # setup the twitter api client twitter_api = Twython( config.twitter_CONSUMER_KEY, config.twitter_CONSUMER_SECRET, config.twitter_ACCESS_KEY, config.twitter_ACCESS_SECRET, ) hashtags = "#Clarl2016" statuses = [ "Beep Boop! I was programmed to love!", "Weddings are fun! Beep Boop!", "Beep Boop! A memento of the day!", "Don't they look great!?" ] print 'hello, I am about to tweet. HERE WE GO' tstart = time.time() # get filename for this group of photos #now = jpg_group now = "test" fname = config.file_path + now + '.gif' # choose new status from list and at the hashtags status_choice = random.choice(statuses) status_total = status_choice + " " + hashtags
import sys import string import simplejson from twython import Twython #WE WILL USE THE VARIABLES DAY, MONTH, AND YEAR FOR OUR OUTPUT FILE NAME import datetime now = datetime.datetime.now() day=int(now.day) month=int(now.month) year=int(now.year) #FOR OAUTH AUTHENTICATION -- NEEDED TO ACCESS THE TWITTER API t = Twython(app_key='5dBlAKQreMfde02aWkTwvj3cv', #REPLACE 'APP_KEY' WITH YOUR APP KEY, ETC., IN THE NEXT 4 LINES app_secret='9s17OqhWmvdhJg50V9H9BljOMzkGycGPtYjiXqFooUbVlnrMLl', oauth_token='1003877817719767041-b57g3SFSPcuhFq0uXjtLng56ZZIezC', oauth_token_secret='nWv4msdIinp8tbMkQjLN1BxHXQSfeFFFuQgCA3fcUJV2C') #REPLACE WITH YOUR LIST OF TWITTER USER IDS ids = ("27203714") #ACCESS THE LOOKUP_USER METHOD OF THE TWITTER API -- GRAB INFO ON UP TO 100 IDS WITH EACH API CALL #THE VARIABLE USERS IS A JSON FILE WITH DATA ON THE 32 TWITTER USERS LISTED ABOVE users = t.lookup_user(user_id = ids) #NAME OUR OUTPUT FILE - %i WILL BE REPLACED BY CURRENT MONTH, DAY, AND YEAR outfn = "twitter_user_data_%i.%i.%i.txt" % (now.month, now.day, now.year) #NAMES FOR HEADER ROW IN OUTPUT FILE fields = ("id screen_name name created_at url followers_count friends_count statuses_count \favourites_count listed_count \contributors_enabled description protected location lang expanded_url".split())
def backtest_expert(conn, aktien_isin, zeitrahmen, diagramm, zwitschern): print("*** Starte Backtest: " + aktien_isin + " ***") global orderglobal global daysglobal # leeres depot und startkapital saldo = 0 anzahl = 0 order = 0 # doppelte anzahl kurse aus der DB holen command = """select kurs_date, kurs_close, aktien_name, aktien_isin, aktien_url from ( select distinct kurs_date, kurs_close, kurs_isin from kurse as k where kurs_isin='""" + aktien_isin + """' order by kurs_date desc limit """ + str( zeitrahmen * 2) + """ ) as foo inner join aktien as a on (foo.kurs_isin=a.aktien_isin) order by kurs_date asc;""" df2 = pd.read_sql(command, con=conn) tage = range(1, zeitrahmen) for day in tqdm(tage, dynamic_ncols=True, desc="Days"): # fenster verschieben aus der doppelten DB tabelle offset = day - 1 # und dataframe für pandas mit neuen index draus ziehen px = pd.DataFrame(df2[offset:zeitrahmen + offset], columns=[ 'kurs_date', 'kurs_close', 'aktien_name', 'aktien_isin', 'aktien_url' ]) px.index = range(len(px)) # letzter ist immer der tag zu dem in diesem loop gestetet wird letzter = len(px.index) - 1 #print("Tag: ", str(day), " - ", px['kurs_date'][letzter], "-> Kurs: ", str(px['kurs_close'][letzter]), "-> Saldo: ", str(saldo)) # neue lin reg für jeden tag im backtest aktie_deter, performance, trenddays, vorhersage = optimierte_lineare_regression( px) #print (aktie_deter,performance,trenddays) #print (stopkurs) buysell = expert_advisor(px, aktie_deter, performance, vorhersage) kurs_aktuell = px['kurs_close'][letzter] # day if (buysell == 1) and (anzahl == 0): #print("*** Kaufen: ", str(kurs_aktuell)) saldo = saldo - kurs_aktuell anzahl = 1 order = order + 1 if diagramm: pyplot.plot(day, kurs_aktuell, "go", markersize=12) if (buysell == -1) and (anzahl == 1): #print("*** Verkaufen: ", str(kurs_aktuell)) saldo = saldo + kurs_aktuell anzahl = 0 order = order + 1 if diagramm: pyplot.plot(day, kurs_aktuell, "ro", markersize=12) if (anzahl == 1): daysglobal = daysglobal + 1 # depot am ende ausleeren if (anzahl == 1): #print("Verkaufen: ", str(kurs_aktuell)) saldo = saldo + kurs_aktuell print("Saldo: " + str(saldo)) print("Order= " + str(order)) orderglobal = orderglobal + order prozent = saldo / px['kurs_close'].mean() * 100 print("Performance: " + str(prozent) + "%") if diagramm or zwitschern: kurse = px['kurs_close'] tage = range(0, len(kurse)) schnitt = kurse.median() plot_title = str(px['aktien_name'][letzter]) + " - " + str( px['aktien_isin'][letzter]) pyplot.plot((0, letzter), (schnitt, schnitt), color='blue', linewidth=1, linestyle="dotted") pyplot.title(plot_title) pyplot.plot(tage, kurse, color='black', linewidth=2) #sma90 = px['kurs_close'].rolling(window=90).mean() #pyplot.plot(tage,sma90, color='blue', linewidth=1) pyplot.plot(tage[int(letzter - trenddays):int(letzter)], vorhersage[int(letzter - trenddays):int(letzter)], color='purple', linewidth=2) pyplot.xlabel('Tage seit ' + str(px['kurs_date'][0])) pyplot.ylabel('Kurs in Euro') pyplot.grid() if diagramm: bolltage = 20 bollfaktor = 2.0 px['bb0'] = px['kurs_close'].rolling(window=bolltage).mean() px['std'] = px['kurs_close'].rolling(window=bolltage).std() bollinger0 = px['bb0'] pyplot.plot(tage, bollinger0, color='darkgray', linewidth=1, linestyle="dotted") bollinger1 = px['bb0'] + (px['std'] * bollfaktor) bollinger2 = px['bb0'] - (px['std'] * bollfaktor) pyplot.fill_between(tage, bollinger1, bollinger2, color="lightyellow") pyplot.plot(tage, bollinger1, color='darkgray', linewidth=1) pyplot.plot(tage, bollinger2, color='darkgray', linewidth=1) if zwitschern: aktien_chart = str( tempfile.gettempdir()) + "/" + str(px['aktien_isin'][0] + ".PNG") print(aktien_chart) pyplot.savefig(aktien_chart, format="png") twitter = Twython(config.get('twitter', 'APP_KEY'), config.get('twitter', 'APP_SECRET'), config.get('twitter', 'OAUTH_TOKEN'), config.get('twitter', 'OAUTH_TOKEN_SECRET')) photo = open(aktien_chart, 'rb') response = twitter.upload_media(media=photo) tweet_title = str(px['aktien_name'][0]) + " - #" + str( px['aktien_isin'][0]) + " #" + px['aktien_name'][0].split( ' ', 1)[0] + " #" + str(px['aktien_url'][0]) twitter.update_status(status=tweet_title, media_ids=[response['media_id']]) else: pyplot.show() pyplot.clf() pyplot.cla() pyplot.close() return prozent
from django.shortcuts import render, render_to_response from .models import Page from django.conf import settings from twython import Twython twitter = Twython('hCkbdXZwdoFoIJsQqANRQ', 'mccOHaIdUEH8hsLoX0VtYe3sjlgfaX2RiChHT02rg', '163400534-6PVteKAIQZVGmU2drQZJ0huwU2nBYS0UWbY7qAxW', 'pP7pa4UH0klP8JlOFmYBL5JhlJkLxmZ0qcreSohjulsig') # Create your views here. def about(request): aboutpage = Page.objects.filter(title='about') tweets = twitter.get_home_timeline() context = {'aboutpage': aboutpage} return render(request, 'pages/about.html', context) from django.shortcuts import render from .models import ContactForm from django.http import HttpResponseRedirect from django.core.mail import send_mail from django.core.mail.message import EmailMessage def contact(request): if request.method == 'POST': form = ContactForm(request.POST) if form.is_valid(): cd = form.cleaned_data name = 'Name:' + '\n' + str(cd['name']).strip('[]') + '\n\n'
import config from twython import Twython, TwythonError # create a Twython object by passing the necessary secret passwords twitter = Twython(config.api_key, config.api_secret, config.access_token, config.token_secret)
from twython import Twython import blinkt import time import csv import string import sys import colorsys blinkt.clear() blinkt.show() APP_KEY = '' APP_SECRET = '' twitter = Twython(APP_KEY, APP_SECRET, oauth_version=2) ACCESS_TOKEN = twitter.obtain_access_token() twitter = Twython(APP_KEY, access_token=ACCESS_TOKEN) with open('/home/pi/Desktop/colours.csv', 'Ur') as f: colours = list(csv.reader(f)) def hex_to_rgb(hex): if len(hex) == 3: hex = hex[0] + hex[0] + hex[1] + hex[1] + hex[2] + hex[2] r = int(hex[0:1], base=16) g = int(hex[2:3], base=16) b = int(hex[4:5], base=16) return r, g, b def colourCheck(inputstring): inputstring = inputstring.lower()
import json from twython import Twython # read authentication credentials with open('credentials.json') as f: credentials = json.load(f) accessToken = credentials['access_token'] accessSecret = credentials['access_token_secret'] consumerKey = credentials['consumer_key'] consumerSecret = credentials['consumer_key_secret'] twitter = Twython(accessToken, accessSecret, oauth_version=2) authentication_token = twitter.obtain_access_token() print(authentication_token)
y = canvas.winfo_rooty() + 50 img = auto.screenshot(region=(x, y, MAX - x, MAX - y)) img.save(image_file_name) sleep(5) root.destroy() cur_hour = datetime.now().strftime('%H') image_file_name = 'art.jpg' MAX = 600 HEX_MAX = 15 HEX_LEN = 6 root = Tk() root.geometry("%dx%d+%d+%d" % (MAX, MAX, 50, 50)) canvas = Canvas(root, width=MAX, height=MAX, bg='black') canvas.pack() print('Creating artwork') fill_canvas() scrn = ScreenshotThread() scrn.start() mainloop() print('Updating Twitter status with image') twtr = Twython(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET) img_bytes = open(image_file_name, 'rb') response = twtr.upload_media(media=img_bytes) twtr.update_status(media_ids=[response['media_id']])
# Import the Twython class from twython import Twython import json import pandas as pd # Load credentials from json file with open("twitter_credentials.json", "r") as file: creds = json.load(file) # Instantiate an object python_tweets = Twython(creds['CONSUMER_KEY'], creds['CONSUMER_SECRET']) # Create our query query = { 'q': 'learn python', 'result_type': 'popular', 'count': 10, 'lang': 'en', } # Search tweets dict_ = {'user': [], 'date': [], 'text': [], 'favorite_count': []} for status in python_tweets.search(**query)['statuses']: dict_['user'].append(status['user']['screen_name']) dict_['date'].append(status['created_at']) dict_['text'].append(status['text']) dict_['favorite_count'].append(status['favorite_count']) # Structure data in a pandas DataFrame for easier manipulation df = pd.DataFrame(dict_) df.sort_values(by='favorite_count', inplace=True, ascending=False)
# To add a new cell, type '#%%' # To add a new markdown cell, type '#%% [markdown]' #%% from IPython import get_ipython #%% import config import pandas as pd import numpy as np import matplotlib.pyplot as plt get_ipython().run_line_magic('matplotlib', 'inline') import seaborn as sns from twython import Twython # Obtain an OAUTH 2 Access Token twitter = Twython(config.API_KEY, config.API_SECRET_KEY, oauth_version=2) ACCESS_TOKEN = twitter.obtain_access_token() #%% # Prepare to use Access Token twitter = Twython(config.API_KEY, access_token=ACCESS_TOKEN) #%% # Get users Tweets, Rts, Replies and Favs from timeline Tweets = twitter.get_user_timeline(screen_name=config.USER, count=200) Favs = twitter.get_favorites(screen_name=config.USER) # for fav in Favs: # print(f"User: {fav['quoted_status']['user']['screen_name']} -> {fav['text']}") #%%
import sys from twython import Twython from picamera import PiCamera from time import sleep apiKey = "sQQEszZ8Gwy8SVnZeeuLMF40C" apiSecret = "GWdTgpS7p9t6P7ZCC3QdRFTMBl75NyTVQwodz20sOIV85n7ooo" accessToken = "1271178365219766272-R5eAnVIeIRDFdlYpp7namiweyFk3Zp" accessTokenSecret = "1XxHeiD9vL851hbbuuXoJHMuCTvPEqymbdtYCopQ1UxdG" api = Twython(apiKey, apiSecret, accessToken, accessTokenSecret) camera = PiCamera() for i in range(5): camera.start_preview() sleep(5) camera.capture('./image{}.jpg'.format(i)) camera.stop_preview() sleep(5) photo = open('./image{}.jpg'.format(i), 'rb') response = api.upload_media(media=photo) api.update_status(status="Image{} from Raspberry Pi Camera".format(i), media_ids=[response['media_id']])
def importData(TWITTER_APP_KEY,TWITTER_APP_KEY_SECRET, TWITTER_ACCESS_TOKEN, TWITTER_ACCESS_TOKEN_SECRET): inp = str(input("Please enter a stock quote starting with a $ sign: ")) twitter = Twython(app_key=TWITTER_APP_KEY, app_secret=TWITTER_APP_KEY_SECRET, oauth_token=TWITTER_ACCESS_TOKEN, oauth_token_secret=TWITTER_ACCESS_TOKEN_SECRET) search = twitter.search(q = inp, count=100) tweets=search['statuses'] tweet_list = [] for tweet in tweets: #print (tweet['text'].encode('utf-8'),'\n') tweet_list.append(tweet['text'].encode('utf-8')) CompanyName = inp if tweet_list != None: readLine = tweet_list posTweets = 0 negTweets = 0 """ Words used for Sentiment analysis sourced from https://github.com/jeffreybreen/twitter-sentiment-analysis-tutorial-201107/blob/master/data/opinion-lexicon-English/positive-words.txt Minqing Hu and Bing Liu. "Mining and Summarizing Customer Reviews." Proceedings of the ACM SIGKDD International Conference on Knowledge Discovery and Data Mining (KDD-2004), Aug 22-25, 2004, Seattle, Washington, USA, Bing Liu, Minqing Hu and Junsheng Cheng. "Opinion Observer: Analyzing and Comparing Opinions on the Web." Proceedings of the 14th International World Wide Web conference (WWW-2005), May 10-14, 2005, Chiba, Japan. """ wordlistOpened = False while wordlistOpened == False: try: openWordlist = open("positive-words.txt") wordlistOpened = True except: openWordlist = None print("Positive word list could not be opened. Please check that it exists.") sys.exit(0) if openWordlist != None: readLineWord = openWordlist.readline() posWords = [] while len(readLineWord) > 0: if readLineWord[0] != ";": readLineWord = readLineWord.rstrip() #readLineWord = readLineWord.upper() #print(readLineWord) posWords.append((readLineWord).encode('utf-8')) readLineWord = openWordlist.readline() openWordlist.close() wordlistOpened = False while wordlistOpened == False: try: openWordlist = open("negative-words.txt") wordlistOpened = True except: openWordlist = None print("Negative word list could not be opened. Please check that it exists.") sys.exit(0) if openWordlist != None: readLineWord = openWordlist.readline() negWords = [] while len(readLineWord) > 0: if readLineWord[0] != ";": readLineWord = readLineWord.rstrip() #readLineWord = readLineWord.upper() #print(readLineWord) negWords.append((readLineWord).encode('utf-8')) readLineWord = openWordlist.readline() openWordlist.close() posTweets = 1 negTweets = 1 for j in range(len(readLine)): for i in range(len(posWords)): if posWords[i] in readLine[j]: posTweets += 1 #print(":)", posTweets) for i in range(len(negWords)): if negWords[i] in readLine[j]: negTweets += 1 #print(":(", negTweets) noOfTweets = posTweets + negTweets stockinfo = stock_price(CompanyName) print("Data successfully imported!") return(True, posTweets, negTweets, noOfTweets, "", CompanyName, stockinfo)
## __author__ = "Ben McGinnes <*****@*****.**>" __copyright__ = "Copyright © Benjamin D. McGinnes, 2013-2015" __license__ = "BSD" __version__ = "0.0.2" __bitcoin__ = "1KvKMVnyYgLxU1HnLQmbWaMpDx3Dz15DVU" import datetime import os.path import pprint import sys from twython import Twython, TwythonError from config import * twitter = Twython(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET) cred = twitter.verify_credentials() dd = datetime.datetime pp = pprint.pprint l = len(sys.argv) if l == 1: limits = input("Enter the resources to check (separated by spaces): ") elif l == 2: limits = sys.argv[1] elif l > 2: limits = sys.argv[1:] else: limits = input("Enter the resources to check (separated by spaces): ") if limits.lower() == "all":
import string import simplejson import pandas as pd from twython import Twython #WE WILL USE THE VARIABLES DAY, MONTH, AND YEAR FOR OUR OUTPUT FILE NAME import datetime now = datetime.datetime.now() day = int(now.day) month = int(now.month) year = int(now.year) #FOR OAUTH AUTHENTICATION -- NEEDED TO ACCESS THE TWITTER API t = Twython( app_key= 'GotdByfuWw6wLT3OYEaNFbF9c', #REPLACE 'APP_KEY' WITH YOUR APP KEY, ETC., IN THE NEXT 4 LINES app_secret='91iEbNlzNYUUjbybW0I8DvECxbffQTEiDZ52FpdEzFzzJWt5C7', oauth_token='868966136-IA3MAwfujlzd2TEZbLEgJENlN826TOjAtWIT0sVK', oauth_token_secret='P6m4Hyd7dgAezY5J4VwEzmi8lC8uDIpi5T2FrNx8jxFyA') tmpdf = pd.read_csv( r"/Users/franciscojavierarceo/ProjectWork/Misc/TwitterUsers.txt", header=-1, sep='\t', names=['indx', 'TwitterName', 'ignore']) #ACCESS THE LOOKUP_USER METHOD OF THE TWITTER API -- GRAB INFO ON UP TO 100 IDS WITH EACH API CALL #THE VARIABLE USERS IS A JSON FILE WITH DATA ON THE 32 TWITTER USERS LISTED ABOVE userlist = tmpdf['TwitterName'] Rawdata = [] for i in userlist: try:
import tokens from datetime import datetime """ example response {"data": [ {"federation": "UEFA", "home_team": "Manchester City", "prediction": "1", "status": "pending", "season": "2017 - 2018", "start_date": "2018-04-22T16:30:00", "result": "", "odds": {"X": 8.458, "12": 1.053, "2": 20.46, "X2": 5.864, "1": 1.135, "1X": 1.013}, "id": 13249, "competition_cluster": "England", "last_update_at": "2018-04-22T14:06:35.857000", "competition_name": "Premiership", "is_expired": false, "away_team": "Swansea"}]} {'data': [{'odds': {'2': 6.37, 'X': 3.911, '1X': 1.113, '12': 1.213, '1': 1.553, 'X2': 2.466}, 'home_team': 'Atletico Madrid', 'prediction': '1', 'status': 'pending', 'id': 13287, 'start_date': '2018-04-22T19:45:00', 'competition_name': 'Primera Division', 'season': '2017 - 2018', 'federation': 'UEFA', 'competition_cluster': 'Spain', 'away_team': 'Real Betis', 'result': '', 'is_expired': False, 'last_update_at': '2018-04-22T14:06:35.857000'}, {'odds': {'2': 12.426, 'X': 5.661, '1X': 1.038, '12': 1.102, '1': 1.213, 'X2': 3.932}, 'home_team': 'Sporting CP', 'prediction': '1', 'status': 'pending', 'id': 13406, 'start_date': '2018-04-22T20:15:00', 'competition_name': 'Primeira Liga', 'season': '2017 - 2018', 'federation': 'UEFA', 'competition_cluster': 'Portugal', 'away_team': 'Boavista', 'result': '', 'is_expired': False, 'last_update_at': '2018-04-22T14:06:35.857000'}]} """ twitter_api = Twython(tokens.consumer_key, tokens.consumer_secret, tokens.access_token, tokens.access_token_secret) # Example JSON response json_response = { "data": [{ "federation": "UEFA", "home_team": "Manchester City", "prediction": "1", "status": "pending", "season": "2017 - 2018", "start_date": "2018-04-22T16:30:00", "result": "", "odds": { "X": 8.458, "12": 1.053, "2": 20.46,
import dash_daq as daq import dash_bootstrap_components as dbc from dash.dependencies import Output, Input import base64 import matplotlib.pyplot as plt import re from twython import Twython from wordcloud import WordCloud, STOPWORDS from app import app from navbar import Navbar app.config['suppress_callback_exceptions'] = True nav = Navbar() APP_KEY = "D4P9kxxnr4IQWXcXabrziMV18" APP_SECRET = "rqmQLjoHgohPG1Ts819vxlpE6wnSr2xQsJpgr9gAqLqkbn7qfZ" twitter = Twython(APP_KEY, APP_SECRET) cloud = dbc.Container([ html.Div( [html.Img(id='image', src='wordcloud.png', height=550, width=700)], className='offset-by-three column'), html.Div(daq.Slider(id='image-dropdown', min=100, max=2000, size=700, value=400, handleLabel={ "showCurrentValue": True, "label": "Tweets" }, marks={
import sqlite3 from random import shuffle from datetime import datetime import time import pymysql from twitter_credentials import * from twython import Twython ############################################################################################# ####################### INPUTS ############################################################## db_name = 'lille.db' max_queries_per_user = 16 twitter = Twython(twitter_auth['consumer_key'], twitter_auth['consumer_secret'], twitter_auth['access_token'], twitter_auth['access_token_secret']) # Working directory is where the sqlite database file is found! working_directory = "./" ############################################################################################# ######################## BELOW SHOULD NOT NEED TO BE ALTERED ################################ ############################################################################################# ######################## Tweet Data Class ################################################### class TweetData: def __init__(self, conn): self.cnx = conn
from twython import Twython ConsumerKey = "" ConsumerSecret = "" AccessToken = "" AccessTokenSecret = "" twitter = Twython(ConsumerKey, ConsumerSecret, AccessToken, AccessTokenSecret) followers = twitter.get_followers_list(screen_name="hmcuesta") #for follower in followers["users"]: # print(" {0} \n ".format(follower)) for follower in followers["users"]: print(" user: {0} \n name: {1} \n Number of tweets: {2} ".format( follower["screen_name"], follower["name"], follower["statuses_count"]))
def parse_ids(season_id, game_id): ### pull common variables from the parameters file charts_units = parameters.charts_units files_root = parameters.files_root ### generate date and team information schedule_csv = files_root + season_id + "_schedule.csv" schedule_df = pd.read_csv(schedule_csv) schedule_date = schedule_df[(schedule_df['GAME_ID'] == int(game_id))] home = schedule_date['HOME'].item() away = schedule_date['AWAY'].item() ### establish common filepaths livefeed_file = files_root + 'livefeed.json' ### post charts to Twitter APP_KEY = twitter_credentials.APP_KEY APP_SECRET = twitter_credentials.APP_SECRET OAUTH_TOKEN = twitter_credentials.OAUTH_TOKEN OAUTH_TOKEN_SECRET = twitter_credentials.OAUTH_TOKEN_SECRET twitter = Twython(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET) units_onice_shots_away = open(charts_units + 'onice_shots_away.png', 'rb') units_onice_shots_home = open(charts_units + 'onice_shots_home.png', 'rb') units_onice_xg_away = open(charts_units + 'onice_xg_away.png', 'rb') units_onice_xg_home = open(charts_units + 'onice_xg_home.png', 'rb') with open(livefeed_file) as livefeed_json: livefeed_data = json.load(livefeed_json) period = livefeed_data["liveData"]["linescore"]["currentPeriod"] status = livefeed_data["liveData"]["linescore"][ "currentPeriodTimeRemaining"] minutes_gone = int() seconds_gone = int() regulation_time_gone = str() ot_time_gone = str() try: time_left_split = status.split(':') regulation_minutes_gone = 20 - int(time_left_split[0]) ot_minutes_gone = 5 - int(time_left_split[0]) if int(time_left_split[1]) == 0 and int(time_left_split[1]) > 50: seconds_gone = 0 - int(time_left_split[0]) seconds_gone = '0' + str(seconds_gone) elif int(time_left_split[1]) != 0 and int(time_left_split[1]) > 50: seconds_gone = 60 - int(time_left_split[1]) seconds_gone = '0' + str(seconds_gone) regulation_time_gone = str(regulation_minutes_gone) + ':' + str( seconds_gone) ot_time_gone = str(ot_minutes_gone) + ':' + str(seconds_gone) except: pass images = [ units_onice_shots_away, units_onice_shots_home, units_onice_xg_away, units_onice_xg_home ] media_ids = [] for i in images: response = twitter.upload_media(media=i) media_ids.append(response['media_id_string']) if period == 1 and status != 'END': twitter.update_status( status=away + ' @ ' + home + ' (' + regulation_time_gone + ' into the 1st Period) on-ice shots, xG for units:', media_ids=media_ids) elif period == 1 and status == 'END': twitter.update_status(status=away + ' @ ' + home + ' (End of 1st Period) on-ice shots, xG by unit:', media_ids=media_ids) if period == 2 and status != 'END': twitter.update_status( status=away + ' @ ' + home + ' (' + regulation_time_gone + ' into the 2nd Period) on-ice shots, xG for units:', media_ids=media_ids) elif period == 2 and status == 'END': twitter.update_status(status=away + ' @ ' + home + ' (End of 2nd Period) unit on-ice shots, xG:', media_ids=media_ids) if period == 3 and status != 'END' and status != 'Final': twitter.update_status( status=away + ' @ ' + home + ' (' + regulation_time_gone + ' into the 3rd Period) on-ice shots, xG for units:', media_ids=media_ids) elif period == 3 and status == 'END': twitter.update_status( status=away + ' @ ' + home + ' (End of 3rd Period) on-ice shots, xG for units:', media_ids=media_ids) elif period == 3 and status == 'Final': twitter.update_status(status=away + ' @ ' + home + ' (Final) on-ice shots, xG for units:', media_ids=media_ids) if period == 4 and status != 'END' and status != 'Final': twitter.update_status(status=away + ' @ ' + home + ' (' + ot_time_gone + ' into Overtime) on-ice shots, xG for units:', media_ids=media_ids) elif period == 4 and status == 'END': twitter.update_status(status=away + ' @ ' + home + ' (End of Overtime) on-ice shots, xG for units:', media_ids=media_ids) elif period == 4 and status == 'Final': twitter.update_status(status=away + ' @ ' + home + ' (Final) on-ice shots, xG for units:', media_ids=media_ids) if period == 5 and status != 'Final': twitter.update_status(status=away + ' @ ' + home + ' (End of Overtime) on-ice shots, xG for units:', media_ids=media_ids) elif period == 5 and status == 'Final': twitter.update_status(status=away + ' @ ' + home + ' (Final) on-ice shots, xG for units:', media_ids=media_ids) print('Tweeted unit on-ice shots, xG.')
def get_timeline_user(user_name, tweets_dict): """Function to obain the user timeline from Twitter API Arguments: user_name {str} -- user name to extract tweets_dict {dict} -- dict to store tweets Returns: dict -- dict with tweets stored """ print("Obtaining info of "+user_name) env_vars = read_from_env_file() twitter = Twython(env_vars['TWYTHON_KEY'], env_vars['TWYTHON_PASS']) last_id = 0 try: timeline_results = twitter.get_user_timeline( screen_name=user_name, count='200') except TwythonError as e: print(e) for tweet in timeline_results: user = '******'+tweet['user']['screen_name'] userid = tweet['user']['id'] name = tweet['user']['name'] location = tweet['user']['location'] img = tweet['user']['profile_image_url'] favorited = tweet['favorited'] coordinates = tweet['coordinates'] in_reply_to_user_id_str = tweet['in_reply_to_user_id_str'] in_reply_to_screen_name = '@'+str(tweet['in_reply_to_screen_name']) if(in_reply_to_screen_name == '@None'): in_reply_to_screen_name = '' idstr = tweet['id_str'] created_at = tweet['created_at'] hashtags = "" mentions = "" mentions_id = "" urls = "" if tweet['entities']['hashtags']: for row in tweet['entities']['hashtags']: hashtags = hashtags+' #'+row['text'] if tweet['entities']['urls']: for row in tweet['entities']['urls']: urls = urls+' '+row['url'] if tweet['entities']['user_mentions']: for row in tweet['entities']['user_mentions']: mentions = mentions+' @'+row['screen_name'] replies_count = 0 if tweet['entities']['user_mentions']: replies_count = len(tweet['entities']['user_mentions']) for row in tweet['entities']['user_mentions']: mentions_id = mentions_id+' '+row['id_str'] text = tweet['text'] retweet_count = tweet['retweet_count'] favourites_count = tweet['user']['favourites_count'] followers_count = tweet['user']['followers_count'] listed_count = tweet['user']['listed_count'] friends_count = tweet['user']['friends_count'] link_tweet = f'twitter.com/{user}/status/{idstr}' # Output tweets tweets_dict["id_tweet"].append( idstr if idstr else "") tweets_dict["id_user"].append( userid if userid else "") tweets_dict["user"].append( user if user else "") tweets_dict["link_tweet"].append( link_tweet if link_tweet else "") tweets_dict["timestamp"].append( created_at if created_at else "") tweets_dict["text"].append( text if text else "") tweets_dict["replies_count"].append( replies_count if replies_count else "") tweets_dict["retweets_count"].append( retweet_count if retweet_count else "") tweets_dict["likes_count"].append( favourites_count if favourites_count else "") return tweets_dict
#coding:utf-8 from twython import Twython from twython import TwythonStreamer import sys reload(sys) sys.setdefaultencoding('utf8') twitter = Twython('Consumer Key', 'Consumer Secret', 'Access Token', 'Access Token Secret') class MyStreamer(TwythonStreamer): def on_success(self, data): if 'user' in data: userName = data['user']['screen_name'] favoriteList = open('favoriteList.txt').read().split(';') if userName in favoriteList: id = data['id'] try: twitter.create_favorite(id=id) except Exception: pass def on_error(self, status_code, data): print status_code, data stream = MyStreamer('Consumer Key', 'Consumer Secret', 'Access Token', 'Access Token Secret')
def inicializa(self): self.authorizator.load_twitter_token(self.tipo_id) api_key, access_token = self.authorizator.get_twython_token() self.twitter = Twython(api_key, access_token=access_token)
from twython import Twython import configparser import sys import json import os config = configparser.ConfigParser() # reading APP_KEY and APP_SECRET from the config.ini file # update with your credentials before running the app config.read(os.path.dirname(__file__) + '../config.ini') APP_KEY = config['twitter_connections_details']['APP_KEY'] APP_SECRET = config['twitter_connections_details']['APP_SECRET'] api = Twython(APP_KEY, APP_SECRET) if not api: print('Authentication failed!.') sys.exit(-1) tweets = [] # list where to store data tweets_per_request = 100 max_tweets_to_be_fetched = 10000 # number of tweets you want fetch (optional) search_query = "geocode:45.533471,-73.552805,12mi" max_id = None since_id = None count_tweets = 0
from twython import Twython import config import datetime now = datetime.datetime.now() day = int(now.day) month = int(now.month) year = int(now.year) from pymongo import MongoClient client = MongoClient('mongodb://localhost:27017') db = client.epidemic user_tweets = db.user_tweets topic_tweets = db.topic_tweets t = Twython(app_key=config.app_key, app_secret=config.app_secret, oauth_token=config.oauth_token, oauth_token_secret=config.oauth_token_secret) #REPLACE WITH YOUR LIST OF TWITTER USER IDS #ids = "207442453," #users = t.lookup_user(user_id = ids) def get_user_timeline(screen_name): for screen_name in doctors: try: print screen_name tweets = t.get_user_timeline(screen_name=screen_name) res = {} res['screen_name'] = screen_name res['tweets'] = [i['text'] for i in tweets]
# if table_exists is not True: # print('new table created!') # connection.create_table(tweets_table_name, families) # print(connection.tables()) # tweets_table = connection.table(tweets_table_name) #print(tweets_table.row('row1')) #tweets_table.put('test1',{'lat':'0', 'long':'1', 'time': '12:00 AM', 'type':'robbery'}) CONSUMER_KEY = 'XXXX' CONSUMER_SECRET = 'XXXX' ACCESS_TOKEN = 'XXXX' ACCESS_SECRET = 'XXXX' twitter = Twython(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_SECRET) emergency_key_word1 = '{{{' emergency_key_word2 = '' #This is a basic listener that just prints received tweets to stdout. class StdOutListener(StreamListener): hbase = Query() def on_data(self, data): tweet = json.loads(data) print(tweet) #
# Intel Galileo - Insper Workshop # Python script to send tweets to change cheerlights color import sys import random from twython import Twython # only uncomment these lines if it does not work # before uncommenting, you will need to install urllib3 # install it with the command "pip install urllib3" #import urllib3 #urllib3.disable_warnings() # API ACCESS TOKENS API_KEY = 'LyJSQNCHZgdjqONbs280TK1Or' API_SECRET = 'DdioxOLnhix5KUz81DLTzAKVa1BdiiaisvDVD3mmVX9heeScvE' ACCESS_TOKEN = '3153951443-H2Kf0iK3qghZx2vfYhfIaBbEVE8bzQ7y8JN98LR' ACCESS_TOKEN_SECRET = 'UyKFksP9RkBm3MNExAWO30Y2ctb23lXKxshkAbhJSewMM' api = Twython(API_KEY,API_SECRET,ACCESS_TOKEN,ACCESS_TOKEN_SECRET) extra_words = ['please', 'thank you', 'now please', 'instead', 'this time', 'for fun', 'thanks', 'very much', 'thank you so much', 'much obliged'] random_word = random.choice(extra_words) name = "Anne" api.update_status(status='@cheerlights %(name)s Galileo wants %(color)s, %(word)s' % {"color": sys.argv[1],"name": name, "word":random_word})