def get_api(consumer_key, consumer_secret, access_token, access_token_secret): auth = OAuthHandler(consumer_key, consumer_secret) auth.set_access_token(access_token, access_token_secret) return API(auth)
def __init__(self): self.auth = OAuthHandler(self.consumer_key, self.consumer_secret) self.auth.set_access_token(self.access_token, self.access_secret) self.api = tweepy.API(self.auth)
csecret = "6CabwjLidazguNm6bbhn9TpA1a6zLfbVyzMLkuL518o88Ilh7Y" atoken = "3064499643-6FoYt11LlsuIaDahGJuZDz4w25Ery3lE9B0BPjG" asecret = "uOgt5jHcV1CoQuoHK5uClgkp8IA480h2n3YMDGEfVSLX4" ##def sentimentAnalysis(text): ## encoded_text = urllib.quote(text) class listener(StreamListener): def on_data(self, data): print(data) #return(True) ## tweet = data.split(',"text:"')[1].split('","source')[0] ## ## saveMe = tweet+'::'+sentimentRating+'\n' ## output = open('output.txt','a') ## outpute.write(saveMe) ## output.close() ## return True def on_error(self, status): print status auth = OAuthHandler(ckey, csecret) auth.set_access_token(atoken, asecret) twitterStream = Stream(auth, listener()) twitterStream.filter(track=["car"])
def parentmodule(): lo.write("\n") lo.write("[" + tyme + "]: " + "MAIN MODULE.\n") lo.write("[" + tyme + "]: " + "Setting all variables.\n") app = QtWidgets.QApplication(sys.argv) # Create our elements # w = QtWidgets.QWidget() # The window # Labels hold text and can also show us images. How cool! tweet = QtWidgets.QLabel(w) # Label - "Tweet" headline = QtWidgets.QLabel(w) # Label - "Reddit" la2 = QtWidgets.QLabel(w) # Label - "Reddit" time_time = QtWidgets.QLabel(w) # Label - "Time" time_time_s = QtWidgets.QLabel(w) # Label - "Time, Seconds" time_day = QtWidgets.QLabel(w) # Label - "Day" time_date = QtWidgets.QLabel(w) # Label - "" wth_dsc_img = QtWidgets.QLabel(w) # Image - Weather Icon snoo_img = QtWidgets.QLabel(w) # Image - Reddit Icon weather_city = QtWidgets.QLabel(w) # Label - "Weather/City Name" weather_temp = QtWidgets.QLabel(w) # Label - "Weather/Temperature" weather_humid = QtWidgets.QLabel(w) # Label - "Weather/Humidity" wth_tmp_img = QtWidgets.QLabel( w) # Label - "Weather/Thermometer Image" font = QtGui.QFont() # Make font element fontp = QtGui.QPalette() # Make new pallete for the first font font.setFamily("Helvetica") # Set Font font.setPointSize(26) # Set font size font.setBold(False) # Bold? No way Jose font2 = QtGui.QFont() # Make font element font2.setFamily("Helvetica") # Set Font font2.setPointSize(64) # Set font size font2.setBold(False) # Nope. font3 = QtGui.QFont() # Make font element font3.setFamily("Helvetica") # Set Font font3.setPointSize(32) # Set font size font3.setBold(False) # Nope. font4 = QtGui.QFont() # Make font element font4.setFamily("Helvetica") # Set Font font4.setPointSize(12) # Set font size font4.setBold(False) # Nope. lt_font = QtGui.QFont() # Make font element lt_font.setFamily("Helvetica") # Set Font lt_font.setPointSize(16) # Set font size lt_font.setBold(False) # Nah. # Set labels to fonts. la1 = QtWidgets.QLabel(w) # Label - "Welcome" la2.setFont(font) tweet.setFont(lt_font) headline.setFont(font4) time_time.setFont(font2) time_time_s.setFont(lt_font) time_day.setFont(font3) time_date.setFont(lt_font) weather_city.setFont(lt_font) weather_temp.setFont(lt_font) weather_humid.setFont(lt_font) # Set labels to pallete's fontp.setColor(QtGui.QPalette.Foreground, QtCore.Qt.white) # Label Color la2.setPalette(fontp) # Set label to palette tweet.setPalette(fontp) # Set label to palette headline.setPalette(fontp) # Set label to palette time_time.setPalette(fontp) time_time_s.setPalette(fontp) time_day.setPalette(fontp) time_date.setPalette(fontp) weather_city.setPalette(fontp) weather_temp.setPalette(fontp) weather_humid.setPalette(fontp) # Geometry! la2.setGeometry(10, 50, 1920, 100) tweet.move(860, 1000) tweet.setGeometry(0, 750, 1920, 500) headline.setGeometry(600, 50, 1920, 500) time_time.setGeometry(600, 10, 1080, 120) time_time_s.setGeometry(950, 0, 1080, 120) time_day.setGeometry(605, 125, 1080, 80) time_date.setGeometry(605, 190, 1920, 50) weather_city.setGeometry(600, 500, 1920, 50) weather_temp.setGeometry(600, 540, 1920, 50) weather_humid.setGeometry(850, 500, 1920, 50) lo.write("[" + tyme + "]: " + "All variables set.\n") """ # Begin setting labels def goodmorning(): currentTime = datetime.datetime.now() if currentTime.hour < 12: la1.setText("Good Morning,") elif 12 <= currentTime.hour < 18: la1.setText("Good Afternoon,") else: la1.setText("Good Night,") la1.setFont(font) la1.setPalette(fontp) # Set label to palette la1.setGeometry(10,0,1920,75) la2.setText(u_name) timer_timer = QtCore.QTimer() timer_timer.timeout.connect(goodmorning) timer_timer.start(500) # Check for new tweet/headline every second """ # Set the title of the window. w.setWindowTitle("Main") # Set window title text def time(): now = datetime.datetime.now() time_time.setText(now.strftime("%H:%M")) time_time_s.setText(now.strftime("%S")) time_day.setText(now.strftime("%A")) time_date.setText(now.strftime("%d %B")) time_timer = QtCore.QTimer() time_timer.timeout.connect(time) time_timer.start(500) # Check for new tweet/headline every second # Color # w.setAutoFillBackground(True) # Fill p = w.palette() # Make P palette for window and color options p.setColor(w.backgroundRole(), Qt.black) # Set window color w.setPalette(p) # w.show() w.showFullScreen() # Make window fullscreen headline.setText("Loading..") # Weather Data # lo.write("[" + tyme + "]: " + "Gathering Weather Data... (First time)\n") url = "http://api.openweathermap.org/data/2.5/weather?q=" + city + "&appid=" + wth_api data = requests.get(url) read = data.json() weather_city.setText(read['name']) if unit == 'C' or unit == 'Celsius': weather_temp.setText(str(read['main']['temp'] - 273.15) + '°C') lo.write("[" + tyme + "]: " + str(read['main']['temp'] - 273.15) + '°C' + "\n") elif unit == 'F' or unit == 'Fareinheit': weather_temp.setText( str(read['main']['temp'] * 9 / 5 - 459.67)[:4] + '°F') lo.write("[" + tyme + "]: " + str(read['main']['temp'] * 9 / 5 - 459.67)[:4] + '°F' + "\n") weather_humid.setText(str(read['main']['humidity']) + '%') lo.write("[" + tyme + "]: " + str(read['main']['humidity']) + '%' + "\n") lo.write("[" + tyme + "]: " + read['weather'][0]['description'] + "\n") # Set thermometer icon to corresponding temperature if unit == 'C' or 'Celsius': if read['main']['temp'] - 273.15 < 5: wth_tmp_img.setPixmap( QtGui.QPixmap('rsc/climacons/Thermometer-Zero.svg')) elif read['main']['temp'] - 273.15 <= 10: wth_tmp_img.setPixmap( QtGui.QPixmap('rsc/climacons/Thermometer-25.svg')) elif read['main']['temp'] - 273.15 >= 10: wth_tmp_img.setPixmap( QtGui.QPixmap('rsc/climacons/Thermometer-50.svg')) elif read['main']['temp'] - 273.15 >= 20: wth_tmp_img.setPixmap( QtGui.QPixmap('rsc/climacons/Thermometer-75.svg')) elif read['main']['temp'] - 273.15 >= 35: wth_tmp_img.setPixmap( QtGui.QPixmap('rsc/climacons/Thermometer-100.svg')) elif unit == 'F' or 'Fareinheit': # if read['main']['temp'] * 9 / 5 - 459.67 < 10: wth_tmp_img.setPixmap( QtGui.QPixmap('rsc/climacons/Thermometer-Zero.svg')) elif read['main']['temp'] * 9 / 5 - 459.67 >= 32: wth_tmp_img.setPixmap( QtGui.QPixmap('rsc/climacons/Thermometer-25.svg')) elif read['main']['temp'] * 9 / 5 - 459.67 != 50: wth_tmp_img.setPixmap( QtGui.QPixmap('rsc/climacons/Thermometer-50.svg')) elif read['main']['temp'] - 273.15 >= 75: wth_tmp_img.setPixmap( QtGui.QPixmap('rsc/climacons/Thermometer-75.svg')) elif read['main']['temp'] - 273.15 >= 100: wth_tmp_img.setPixmap( QtGui.QPixmap('rsc/climacons/Thermometer-100.svg')) wth_tmp_img.setGeometry(675, 540, 1920, 50) # Set icon to corresponding weather # wth_desc = read['weather'][0]['description'] if 'cloud' in wth_desc: wth_dsc_img.setPixmap(QtGui.QPixmap('rsc/climacons/Cloud.svg')) elif 'clear sky' in wth_desc: wth_dsc_img.setPixmap(QtGui.QPixmap('rsc/climacons/Sun.svg')) elif 'rain' or 'shower' in wth_desc: wth_dsc_img.setPixmap( QtGui.QPixmap('rsc/climacons/Cloud-Rain.svg')) elif 'thunder' in wth_desc: wth_dsc_img.setPixmap( QtGui.QPixmap('rsc/climacons/Cloud-Lightning.svg')) elif 'snow' in wth_desc: wth_dsc_img.setPixmap( QtGui.QPixmap('rsc/climacons/Cloud-Snow.svg')) elif 'mist' in wth_desc: wth_dsc_img.setPixmap( QtGui.QPixmap('rsc/climacons/Cloud-Drizzle.svg')) wth_dsc_img.setGeometry(750, 570, 100, 50) def wthtime(): lo.write("[" + tyme + "]: " + "Gathering Weather Data... (Automatic)\n") requests.get(url) # Re-Get Updated data data.json() # Get-it weather_city.setText(read['name']) if unit == 'C' or unit == 'Celsius': weather_temp.setText(str(read['main']['temp'] - 273.15) + '°C') lo.write("[" + tyme + "]: " + str(read['main']['temp'] - 273.15) + '°C' + "\n") elif unit == 'F' or unit == 'Fareinheit': weather_temp.setText( str(read['main']['temp'] * 9 / 5 - 459.67)[:4] + '°F') lo.write("[" + tyme + "]: " + str(read['main']['temp'] * 9 / 5 - 459.67)[:4] + '°F' + "\n") weather_humid.setText(str(read['main']['humidity']) + '%') lo.write("[" + tyme + "]: " + str(read['main']['humidity']) + '%' + "\n") lo.write("[" + tyme + "]: " + read['weather'][0]['description'] + "\n") # Set thermometer icon to corresponding temperature if unit == 'C' or 'Celsius': if read['main']['temp'] - 273.15 < 5: wth_tmp_img.setPixmap( QtGui.QPixmap('rsc/climacons/Thermometer-Zero.svg')) elif read['main']['temp'] - 273.15 <= 10: wth_tmp_img.setPixmap( QtGui.QPixmap('rsc/climacons/Thermometer-25.svg')) elif read['main']['temp'] - 273.15 >= 10: wth_tmp_img.setPixmap( QtGui.QPixmap('rsc/climacons/Thermometer-50.svg')) elif read['main']['temp'] - 273.15 >= 20: wth_tmp_img.setPixmap( QtGui.QPixmap('rsc/climacons/Thermometer-75.svg')) elif read['main']['temp'] - 273.15 >= 35: wth_tmp_img.setPixmap( QtGui.QPixmap('rsc/climacons/Thermometer-100.svg')) elif unit == 'F' or 'Fareinheit': # if read['main']['temp'] * 9 / 5 - 459.67 < 10: wth_tmp_img.setPixmap( QtGui.QPixmap('rsc/climacons/Thermometer-Zero.svg')) elif read['main']['temp'] * 9 / 5 - 459.67 >= 32: wth_tmp_img.setPixmap( QtGui.QPixmap('rsc/climacons/Thermometer-25.svg')) elif read['main']['temp'] * 9 / 5 - 459.67 != 50: wth_tmp_img.setPixmap( QtGui.QPixmap('rsc/climacons/Thermometer-50.svg')) elif read['main']['temp'] - 273.15 >= 75: wth_tmp_img.setPixmap( QtGui.QPixmap('rsc/climacons/Thermometer-75.svg')) elif read['main']['temp'] - 273.15 >= 100: wth_tmp_img.setPixmap( QtGui.QPixmap('rsc/climacons/Thermometer-100.svg')) wth_tmp_img.setGeometry(675, 90, 1920, 50) # Set icon to corresponding weather # wth_desc = read['weather'][0]['description'] if 'cloud' in wth_desc: wth_dsc_img.setPixmap(QtGui.QPixmap('rsc/climacons/Cloud.svg')) elif 'clear sky' in wth_desc: wth_dsc_img.setPixmap(QtGui.QPixmap('rsc/climacons/Sun.svg')) elif 'rain' or 'shower' in wth_desc: wth_dsc_img.setPixmap( QtGui.QPixmap('rsc/climacons/Cloud-Rain.svg')) elif 'thunder' in wth_desc: wth_dsc_img.setPixmap( QtGui.QPixmap('rsc/climacons/Cloud-Lightning.svg')) elif 'snow' in wth_desc: wth_dsc_img.setPixmap( QtGui.QPixmap('rsc/climacons/Cloud-Snow.svg')) elif 'mist' in wth_desc: wth_dsc_img.setPixmap( QtGui.QPixmap('rsc/climacons/Cloud-Drizzle.svg')) wth_dsc_img.setGeometry(750, 70, 100, 50) wth_timer = QtCore.QTimer() wth_timer.timeout.connect(wthtime) wth_timer.start( 1800 * 1000) # Time in seconds * 1000 for milliseconds, 30 minutes # # # # # # # # # # Update Reddit and Twitter feeds # def rheadlinez(): lo.write("[" + tyme + "]: " + "Gathering Reddit Data (Automatic)\n") fo = open('bin/headlines.txt', 'wb') reddit = praw.Reddit( user_agent='Portal Reddit Module (by /u/Swagmanhanna)', client_id='9EASNimdp6ItMw', client_secret="cuuiAqw8QCq3f8jDMbU0uV4uLWA") for submission in reddit.subreddit(subreddit).hot(limit=10): fo.write(submission.title.encode("utf-8") + b'\n') print("Done with Reddit!") lo.write("[" + tyme + "]: " + "Gathered Successfully.\n") fo.close() hlz_timer = QtCore.QTimer() hlz_timer.timeout.connect(rheadlinez) hlz_timer.start( 3600 * 1000 ) # Time in seconds * 1000 for milliseconds, 60 minutes, 1 Hour snoo_img.setPixmap(QtGui.QPixmap("rsc/reddit_ico.svg")) snoo_img.setGeometry(10, 200, 100, 50) def update_label(): with open('bin/tweet.txt', 'r', encoding="utf8") as tweet_file: tweet_content = tweet_file.read() #tweet.setText(tweet_content) # Split this! Add @ and says. reddit_content = random.choice( [f for f in open('bin/headlines.txt')]) headline.setText("/r/" + subreddit + ": " + textwrap.fill(reddit_content, 30)) lo.write("[" + tyme + "]: " + "Setting title to: " + reddit_content + "\n") timer = QtCore.QTimer() # Make a timer timer.timeout.connect(update_label) # Once time is up run the function timer.start(5000) # Check for new tweet/headline every 5 seconds sys.exit(app.exec_()) auth = OAuthHandler(ckey, csecret) # Auth Twitter auth.set_access_token(atoken, asecret) # Auth Twitter keyword = cfg.get('Twitter Module', 'keyword') # Get keyword twitterStream = Stream(auth, listener()) # Initiate Twitter Stream twitterStream.filter(track=[keyword]) # Filter tweets lo.write("[" + tyme + "]: " + "Launching.\n") print("Main Launched") # Huston, we don't have a problem.
def rellenar(request, otro): cadenas = [{ "cadena": "KFC", "cuentas": [ "kfc", "KFCSA", "KFC_UKI", "KFCcl", "kfcarabia", "KFCFrance", "kfcnederland", "KFC_jp", "KFC_SG", "KFC_fob" ] }, { "cadena": "McDonald", "cuentas": [ "McDonalds", "McDonaldsCorp", "McDonaldsUK", "McDonalds_Ar", "McDonaldsArabia", "McDonaldsMexico", "McDonalds_DMV", "McDonaldsCol", "McDonalds_Ecu", "McDonalds_VE", "McDonalds_Cl", "McDonaldsEgypt", "McDonalds_PER", "McDonalds_Uy", "NebMcDonalds", "McDonaldsEurope", "McDonaldsETN" ] }, { "cadena": "Burger King", "cuentas": [ "BurgerKing", "BurgerKing_ID", "BurgerKingMX", "BurgerKingFR", "BurgerKingBR", "burgerkingph", "BurgerKingNL" ] }, { "cadena": "Telepizza", "cuentas": [ "TelepizzaChile", "telepizza", "telepizzalucena", "TelepizzaCuenca", "TelepizzaPt", "TelepizzaUAE", "pizzavalde", "TelepizzaTeruel", "tpantequera", "TelepizzaMania", "TELEPIZZA_LEBRI", "TelepizzaVilaga", "TelepizzaPeru", "TELEPIZZALALINE", "Yosoy_Chile", "TelepizzaMntq", "telepizza_pa", "MartosTelepizza", "telepizzajaen" ] }, { "cadena": "Tarragona", "cuentas": ["tarragonaChile"] }, { "cadena": "Pedro juan y diego", "cuentas": ["PedroJuanDiego"] }, { "cadena": "Doggis", "cuentas": ["_doggito_"] }, { "cadena": "Juan maestro", "cuentas": ["JuanMaestro"] }, { "cadena": "Pizza Hut", "cuentas": [ "pizzahut", "pizzahutuk", "PizzaHutIN", "pizzahutmsia", "pizzahutdeliver", "PizzaHutME", "PizzaHutPak", "PizzaHut_SG", "PizzaHutCares", "pizzahutphils", "PizzaHutAus", "PizzaHutkuwait" ] }, { "cadena": "Tacobell", "cuentas": [ "tacobell", "tacobell_cr", "TacoBell4Teens", "TacoBellCanada", "TacoBellTruck", "TacoBellTeam", "TacoBellSpain", "TacoBellGuate", "tacobelluk", "TacoBellPA" ] }, { "cadena": "Domino", "cuentas": [ "dominomag", "dominos", "Dominos_UK", "Dominorecordco", "TheDomino", "Dominos_AU", "dominosmx", "DominoRecordsDE", "dominos_NZ" ] }] auth = OAuthHandler(ckey, csecret) auth.set_access_token(atoken, asecret) api = tweepy.API(auth) for ca in cadenas: print "->", ca["cadena"] chain = Cadena() chain.nombre = ca["cadena"] chain.save() for cu in ca["cuentas"]: usu = api.get_user(screen_name=cu) u = Usuario.crearUsuario(cu, "Internet", "Internet") u.save() r = Representa r.id_cadena = chain r.id_usuario = u r.save() return HttpResponse(json.dumps(cadenas))
def authenticate_twitter_app(self): auth = OAuthHandler(credentials.api_key, credentials.api_key_secret) auth.set_access_token(credentials.access_token, credentials.access_token_secret) return auth
def auth(): acct_name, consumer_key, consumer_secret, access_token, access_token_secret = get_account_sequential() auth = OAuthHandler(consumer_key, consumer_secret) auth.set_access_token(access_token, access_token_secret) auth_api = API(auth) return auth_api
from tweepy import Stream from tweepy import OAuthHandler from tweepy.streaming import StreamListener from listener import Listener if __name__ == "__main__": apiKey = "QY8f8scdZYb0sPnFMq6G48FMh" apiSecret = "Sxin83rprEg5YUMiThQQ47LZKJRmvLiRLJXJKskB8uno5z62EI" accessToken = "364088274-UvVg9icnCYo0LT8q9mXdKdtGAhOh5WQMgM0Jd7MY" accessSecret = "woi5HaLwnzfMfWQsAp6O15hECxBzIRzknEyiF2cFx83f8" # Set Twitter authorization. auth = OAuthHandler(apiKey, apiSecret) auth.set_access_token(accessToken, accessSecret) streamListener = Listener("#tpp") streamListener.connect(auth) print ": )"
write_to_pubsub(self.tweets) self.tweets = [] self.count += 1 if (self.count % 50) == 0: logging.info("count is: %s at %s" % (self.count, datetime.datetime.now())) return True def on_error(self, status): logging.error(status) def write_to_pubsub(tw): """ Publish to the given pubsub topic. """ messages = [] [messages.append({"data": json.loads(line, encoding="utf8")}) for line in tw] body = json.dumps({"messages": messages}, ensure_ascii=False, encoding="utf8") publisher = pubsub_v1.PublisherClient() topic_path = publisher.topic_path("ccc-2020-289323", "tweets") future = publisher.publish(topic_path, data=body.encode("utf8")) future.result() if __name__ == "__main__": listener = StdOutListener() auth = OAuthHandler(os.getenv("CONSUMER_KEY"), os.getenv("CONSUMER_SECRET")) auth.set_access_token(os.getenv("ACCESS_TOKEN"), os.getenv("ACCESS_SECRET")) stream = Stream(auth, listener) stream.filter(languages=["en"], track=[os.getenv("TRACK")])
else:#if the current chunk is full print "writing to " + fileName, str(chunkCount)+"/"+str(maxChunks) outputFile = open(fileName, 'a') outputFile.write(buffer) outputFile.close()#write it to the current file chunkCount += 1#increment the chunk counter buffer = "\n"#add a new line buffer+=tweetText#add the new tweet. if chunkCount>maxChunks:#if the last chunk was the 1024th chunk fileCount+=1#increment the file counter fileName = "negTweets/negative"+str(fileCount)+".txt"#reset the filename chunkCount = 0#reset the number of chunks to zero return(True) except Exception as e:#if there was an error print >> sys.stderr, "ERR:", str(e)#print err to stderr def on_error(self, status):#i dont really know what this does but it was in the examlpe to i put it in. print status auth = OAuthHandler(ckey, csecret)#authorize the app with twitter auth.set_access_token(atoken, asecret) twitterStream = Stream(auth, listener())#start the stream with a listener object. startStream()
def get_tw_trends_term(term): result = [] # print "starting" # TWITTER_KEY "Qe0uB9vqRPJC4t3XUGnNt7uW0" # TWITTER_SECRET "O018Ae3LCbw2jNe4Dqy4LgxZKnIdoxWJ9DtmgJ2RBzYBgUbvwi" # TWITTER_ACCESS_TOKEN "2340983450 - LunqxRWQeA7gtDQNtdk4BY7WpMLztOQiXiZQXo3" # TWITTER_ACCESS_TOKEN_SECRET "3UefP7CaWOy6PXOJIG3VjBdbOCdgR1zm96zldUVNFcdP4" twitter_key = 'Qe0uB9vqRPJC4t3XUGnNt7uW0' twitter_secret = 'O018Ae3LCbw2jNe4Dqy4LgxZKnIdoxWJ9DtmgJ2RBzYBgUbvwi' twitter_access_token = '2340983450 - LunqxRWQeA7gtDQNtdk4BY7WpMLztOQiXiZQXo3' twitter_access_token_secret = '3UefP7CaWOy6PXOJIG3VjBdbOCdgR1zm96zldUVNFcdP4' try: auth = OAuthHandler(twitter_key, twitter_secret) auth.set_access_token(twitter_access_token, twitter_access_token_secret) except AttributeError: print "No twitter keys are set, please set TWITTER_KEY parameters attribute in settings!!" return False print "auth" print auth api = API(auth) trends1 = api.trends_place(1) data = trends1[0] # grab the trends trends = data['trends'] # grab the name from each trend names = [trend['name'] for trend in trends] # put all the names together with a ' ' separating them trendsName = '\n '.join(names) print(trendsName) print trends1 # Also note that the search results at twitter.com may return historical results # while the Search API usually only serves tweets from the past week.- Twitter documentation. cricTweet = tweepy.Cursor(api.search, q="#" + term).items(350) print "cricTweet" print str(tweepy.Cursor(api.search, q="#" + term)) print "loop" for tweet in cricTweet: # print tweet.created_at, tweet.text, tweet.lang # print tweet.user.name name = tweet.user.name print name if "RT @" not in tweet.text: if (tweet.retweet_count > 5) or (tweet.favorite_count > 5): tweet = { "user": name, "text": tweet.text, "favs": tweet.favorite_count, "retweets": tweet.retweet_count, "created": tweet.created_at } result.append(tweet) # print "finished" # print result # print len(result) return result # n_docs is the total n. of tweets p_t = {} p_t_com = defaultdict(lambda: defaultdict(int)) for term, n in count_stop_single.items(): p_t[term] = n / n_docs for t2 in com[term]: p_t_com[term][t2] = com[term][t2] / n_docs
import time from tweepy import Stream from tweepy import OAuthHandler from tweepy.streaming import StreamListener from token import * class listener(StreamListener): def on_data(self, data): try: saveFile = open('twitDB2full.csv', 'a') saveFile.write(data) saveFile.write('\n') saveFile.close() return True except BaseException, e: print('failed ondata', str(e)) time.sleep(5) def on_error(self, status): print(status) auth = OAuthHandler(consumerkey, consumersecret) auth.set_access_token(accesstoken, accesstokensecret) twitterStream = Stream(auth, listener()) twitterStream.filter(track=["bromo"])
6: "Sunday" } ##Create a twitter account if you do not already have one. ##Go to https://apps.twitter.com/ and log in with your twitter credentials. ##Click "Create New App" ##Fill out the form, agree to the terms, and click "Create your Twitter application" ##In the next page, click on "API keys" tab, and copy your "API key" and "API secret". ##Scroll down and click "Create my access token", and copy your "Access token" and "Access token secret" api_key = 'YOUR_API_KEY' #consume/api key value api_secret = 'YOUR_API_SECRET' #consumer secret/api secret value access_token = 'YOUR_ACCESS_TOKEN' access_token_secret = 'YOUR_ACCESS_TOKEN_SECRET' auth = OAuthHandler(api_key, api_secret) auth.set_access_token(access_token, access_token_secret) api = API( auth, wait_on_rate_limit=True ) #with wait_on_rate_limit set it will go on fetching all followers of user otherwise it will terminate after processing around 18k followers def follower_generator(user_name): ids = [] for block in tweepy.Cursor(api.followers_ids, user_name).items(): if (len(ids) < 100): ids.append(block) else: yield ids ids = []
consumer_key = "QpkTPmn14usqZPZk7cIsVzKQd" consumer_secret = "kWj8r8gzit64CuohZ05fowuMs4dYhZFvT1tNUYmz7o1grfYMql" # Access token and token secret ID to connect from Tweepy to the Twitter Application access_token = "488884935-2Q1enFFqXbja4df9qdeclqiPVIFR3y6hoQ6sGaEO" access_token_secret = "AlTMqycAf8I6L84AQwgmpQqFIFaRNuemcFUuHcHznwhmS" # Authorizes this file to interact with the Twitter API auth = tweepy.OAuthHandler(consumer_key, consumer_secret) auth.set_access_token(access_token, access_token_secret) api = tweepy.API(auth) # class inherited from the Steam Listener superclass that faciliates continous Twitter streaming class StdOutListener(StreamListener): def on_data(self, data): time.sleep(1) # Tweet metadata are streamed on the terminal with one second intervals socket.send_string("stream %s" % data) # sends type string to the ZMQ subscriber print(data) # Prints the tweet metadata return True # This function is continued forever, as there is no condition to make this boolean false def on_error(self, status): print(status) # Prints a status if there is an error if __name__ == '__main__': # Python "main" method listener = StdOutListener() # calls the StdOutListener class and assigns the class to variable listener auth = OAuthHandler(consumer_key, consumer_secret) # Calls the OAuthHandler class to initiate Twitter streaming auth.set_access_token(access_token, access_token_secret) # authenticates the OAuthHandler access by furishing API access tokens stream = Stream(auth, listener) # Calls the Stream class and inputs the authentication and StdOutListener as the class parameters stream.filter(track = ['giants']) # john_sokol1 Twitter ID: 488884935; # Filters for tweets regarding 'giants'
import yaml from tweepy import OAuthHandler from tweepy import Stream from TweetListener import tweet_listener # Get application config keys from yml file def setup_keys(): cfg = {} with open("config.yml", "r") as yml_file: cfg = yaml.load(yml_file) return cfg app_config = setup_keys() auth = OAuthHandler(app_config["consumer_key"], app_config["consumer_secret"]) auth.set_access_token(app_config["access_token"], app_config["access_token_secret"]) myListener = tweet_listener.ThisListener() myStream = Stream(auth, listener=myListener) myStream.filter(track=app_config["key_words"]) myStream.disconnect()
from os import environ from dotenv import load_dotenv from tweepy import OAuthHandler from tweepy import Stream from common.database_access.db_connection import DbConnection from config.config import DOTENV_FILE from repo_path import REPO_PATH from twitter_data.tweeter_streemer.listener import Listener assert load_dotenv(DOTENV_FILE) print(REPO_PATH) print(DOTENV_FILE) auth = OAuthHandler(environ.get("CUSTOMER_KEY"), environ.get("CUSTOMER_SECRET")) auth.set_access_token(environ.get("ACCESS_TOKEN"), environ.get("ACCESS_TOKEN_SECRET")) listener = Listener(DbConnection('twitter').connection) twitterStream = Stream(auth, listener) twitterStream.filter(track=[ "Bitcoin", "BTC", "比特币", "crypto", "bitcoin", 'btc', ])
consumer_s = config['consumer_s'] token = config['token'] token_s = config['token_s'] bool_only_author = config['only_push_tweets_from_author'] users = config['usernames_to_watch'] webhooks = config['webhook_urls'] bool_retweet = config['retweet'] except Exception as f: print(f) _ = input( "[ERROR IN CONFIGURATION FILE (config.json)] Press enter to exit...") exit(0) # 0AUTH try: auth = OAuthHandler(consumer, consumer_s) auth.set_access_token(token, token_s) auth.secure = True api = API(auth) except Exception as f: print(f) _ = input( "[INVALID AUTH OR COULDN'T REACH TWITTER.COM] Press enter to exit...") exit(0) # VALIDATE USERS TO FOLLOW AND CREATE USERS OBJECT feed = [] for username in users: try: user = api.get_user(screen_name=username) feed.append(str(user.id))
def setup_authenticator(): auth = OAuthHandler(Configure_TweepyParameters.consumer_key, Configure_TweepyParameters.consumer_secret) auth.set_access_token(Configure_TweepyParameters.access_token, Configure_TweepyParameters.access_token_secret) return auth
def authenticate_twitter_app(self): auth = OAuthHandler(consumer_key, consumer_secret) auth.set_access_token(access_token, access_token_secret) return auth
def main(): #################################################### # Part I - Create a Twitter API Key and Access Token #################################################### # Twitter Consumer API keys # Twitter Access tokens ######################################################### # Part II - Do a single data pull from Twitter’s REST API ######################################################### # Set keys API_KEY = '' API_SECRET = '' TOKEN_KEY = '' TOKEN_SECRET = '' # Pull data from Twitter REST API url = 'https://api.twitter.com/1.1/search/tweets.json?q=%23popeindc' data = oauth_req(url, TOKEN_KEY, TOKEN_SECRET, API_KEY, API_SECRET) # Output json results to file with open("data/hashchildlabor.json", "wb") as data_file: data_file.write(data) ####################################################################### # Part III - Execute multiple queries at a time from Twitter’s REST API ####################################################################### import tweepy auth = tweepy.OAuthHandler(API_KEY, API_SECRET) auth.set_access_token(TOKEN_KEY, TOKEN_SECRET) api = tweepy.API(auth) query = '#childlabor' cursor = tweepy.Cursor(api.search, q=query, lang="en") # Store APT results in db table for page in cursor.pages(): for item in page: store_tweet('tweets', item) ####################################################### # Part IV - Do a data pull from Twitter’s Streaming API ####################################################### from tweepy.streaming import StreamListener from tweepy import OAuthHandler, Stream class Listener(StreamListener): def on_data(self, data): print(data) return True auth = OAuthHandler(API_KEY, API_SECRET) auth.set_access_token(TOKEN_KEY, TOKEN_SECRET) # Stream Twitter data and exports to terminal stream = Stream(auth, Listener()) stream.filter(track=['child labor'])
#parsed_tweet.update({'created_at': status.created_at}) parsed_tweet.update({'tweet_id': status.id}) parsed_tweet.update({'retweet_count': status.retweet_count}) parsed_tweet.update({'favorite_count': status.favorite_count}) #order_parsed_tweet = collections.OrderedDict(sorted(parsed_tweet.items())) print(parsed_tweet) fw.write(json.dumps(parsed_tweet)) fw.write("\n\n") return True def on_error(self, status): print status if __name__ == '__main__': #This handles Twitter authetification and the connection to Twitter Streaming API l = StdOutListener() auth = OAuthHandler(API_KEY, SECRET_KEY) auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET) stream = Stream(auth, l, tweet_mode = 'extended') stream.filter(languages=["en"], track=['the','a','an'])
# for tag in demotaglist: # if i["text"].lower() == tag: # d += 1 # print(i["text"].lower()) # for tag in repubtaglist: # if i["text"].lower() == tag: # r += 1 # print(i["text"].lower()) # print("d: " + str(d)) # print("r: " + str(r)) # print("\n") def on_error(self, status): print(status) if __name__ == '__main__': try: l = StdOutListener() auth = OAuthHandler(twitter_consumer_key, twitter_consumer_secret) auth.set_access_token(twitter_access_token, twitter_access_token_secret) stream = Stream(auth, l) stream.filter(track=[ '#imwithher', '#makeamericagreatagain', '#clintonfoundation', '#crookedhillary', '#amerikkka' ]) finally: GPIO.cleanup()
return False def on_error(self, status): if status == 420: return False def getData(self, data): return json.loads(data) if __name__ == "__main__": #server="localhost:9092" #topicName="corona" #producer = KafkaProducer(bootstrap_servers=[server]) listener = StdOutListener() auth = OAuthHandler(myCred.API_KEY, myCred.API_SECRET_KEY) auth.set_access_token(myCred.ACCESS_TOKEN, myCred.ACCESS_TOKEN_SECRET) stream = Stream(auth, listener) stream.filter(track=[topicName]) #producer = KafkaProducer(bootstrap_servers=[server], # value_serializer=lambda v: json.dumps(v).encode('utf-8')) #producer.send(topicName,data) #producer.flush() #for e in range(1000): # data = {'number' : e} # producer.send(topicName, value=stream.filter(track=[topicName]).encode()) # time.sleep(5)
consumerSecret = 'PUT YOUR DATA HERE' accessToken = 'PUT YOUR DATA HERE' accessSecret = 'PUT YOUR DATA HERE' #anger, disgust, fear, guilt, interest, joy, sadness, shame and surprise class listener(StreamListener): def on_data(self, data): try: #print data tweet = data.split(',"text":"')[1].split('","source')[0]#[1] indicate the right side of the split print tweet saveThis = str(time.time())+'::'+tweet saveFile = open('twitDBAnger.csv', 'a') #saveFile.write(data) saveFile.write(saveThis) saveFile.write('\n') saveFile.close() return True except BaseException, e: print 'Failed on data',str(e) time.sleep(5) def on_error(self, status): print status auth = OAuthHandler(consumerKey, consumerSecret) auth.set_access_token(accessToken, accessSecret) twitterStream = Stream(auth, listener()) twitterStream.filter(track=["angry"]) #twitterStream.filter(locations=[-180,-90,180,90]) #twitterStream.sample()
def authenticate_twitter_app(self): auth = OAuthHandler(twitter_credentials.CONSUMER_KEY, twitter_credentials.CONSUMER_SECRET) auth.set_access_token(twitter_credentials.ACCESS_TOKEN, twitter_credentials.ACCESS_TOKEN_SECRET) return auth
def authenticate_twitter_app(self): auth = OAuthHandler(twitter_credentials.CONSUMER_KEY, twitter_credentials.CONSUMER_SECRET)# this is an object from auth class that obtain the data from credentials file auth.set_access_token(twitter_credentials.ACCESS_TOKEN, twitter_credentials.ACCESS_TOKEN_SECRET)# this is the method that we can access the token, obtain from the credentials file. return auth
@author: Alex """ from __future__ import print_function import tweepy from tweepy import OAuthHandler from tweepy import Stream from tweepy.streaming import StreamListener ACCESS_TOKEN = '1201278023472840705-vXvkf0jOOturE1Kt6WxbT5bdMW7afP' ACCESS_SECRET = '1R70xgn5OOBHYHxVcmCdr0vvX5DRdTzAuhaHdvIbT9hYJ' CONSUMER_KEY = 'mUeXO93OhVbf8EqCOg2PmCKD4' CONSUMER_SECRET = 'fJTzWcyPMSYvtiiFQZSK6x1Uf6FSV4HZEbg0sq9psU9H6im9Iz' tweetDoc = open('twitterDoc.txt', 'w') class listener(StreamListener): def on_data(self, data): print(data, file = tweetDoc) return True def on_error(self, status): print(status) auth = OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET) auth.set_access_token(ACCESS_TOKEN, ACCESS_SECRET) twitterStream = Stream(auth, listener(),tweet_mode="extended") twitterStream.filter(track="Christmas") tweetDoc.close()
def authorise_twitter_api(config): auth = OAuthHandler(config['DEFAULT']['consumer_key'], config['DEFAULT']['consumer_secret']) auth.set_access_token(config['DEFAULT']['access_token'], config['DEFAULT']['access_secret']) return auth
# Scikit leanr from sklearn.metrics.pairwise import cosine_similarity app = Flask(__name__, static_folder='./client/build/') # Twitter Api Credentials consumer_key = "29t0d6bCnEPbWynevgwubCWAZ" consumer_secret = "mMCuy5v8AkkeqIuePQrHShd8GNrHF1BauHgiqq1devTkTIPeVo" access_token = "1147464618618437632-x4oiaSK6ORIySsJML05KYIKMTpyn4H" access_token_secret = "iASo6JMo7lyE4ZGAQWQhUD2ztdpmcQJCSPjijuAIiV5Cg" # CORS cors = CORS(app, resources={r"/api/*": {"origins": "*"}}) # Authentication auth = OAuthHandler(consumer_key, consumer_secret) auth.set_access_token(access_token, access_token_secret) auth_api = API(auth) # Load model for prediction with open('cluster_model.pkl', 'rb') as f: u = pickle._Unpickler(f) u.encoding = 'latin1' kmeans = u.load() #Routes @app.route('/api/getUserDetails', methods=['GET']) #get user details from twitter api def getUserDetails(): # Get twitter data
def start(args, userin): """Function that starts the virtual assistant with the correct mode according to command-line arguments. Args: args: Command-line arguments. userin: :class:`dragonfire.utilities.TextToAction` instance. """ if 'TRAVIS' in os.environ or args["db"] == "mysql": engine = create_engine('mysql+pymysql://' + Config.MYSQL_USER + ':' + Config.MYSQL_PASS + '@' + Config.MYSQL_HOST + '/' + Config.MYSQL_DB) else: engine = create_engine('sqlite:///dragonfire.db', connect_args={'check_same_thread': False}, echo=True) Base.metadata.create_all(engine) Base.metadata.bind = engine DBSession = sessionmaker(bind=engine) db_session = DBSession() learner.db_session = db_session if args["server"]: import dragonfire.api as API # API of Dragonfire import tweepy # An easy-to-use Python library for accessing the Twitter API from tweepy import OAuthHandler from tweepy import Stream from dragonfire.twitter import MentionListener if Config.TWITTER_CONSUMER_KEY != 'CONSUMER_KEY': auth = OAuthHandler(Config.TWITTER_CONSUMER_KEY, Config.TWITTER_CONSUMER_SECRET) auth.set_access_token(Config.TWITTER_ACCESS_KEY, Config.TWITTER_ACCESS_SECRET) userin.twitter_api = tweepy.API(auth) print("Listening Twitter mentions...") l = MentionListener(args, userin) stream = Stream(auth, l) stream.filter(track=['DragonfireAI'], async=True) API.Run(nlp, learner, omniscient, dc, coref, userin, args["server"], args["port"], db_session) else: global user_full_name global user_prefix if args["cli"]: her = VirtualAssistant(args, userin, user_full_name, user_prefix) while (True): com = raw_input("Enter your command: ") thread.start_new_thread(her.command, (com, )) time.sleep(0.5) elif args["gspeech"]: from dragonfire.sr.gspeech import GspeechRecognizer her = VirtualAssistant(args, userin, user_full_name, user_prefix) recognizer = GspeechRecognizer() recognizer.recognize(her) else: from dragonfire.sr.deepspeech import DeepSpeechRecognizer her = VirtualAssistant(args, userin, user_full_name, user_prefix) recognizer = DeepSpeechRecognizer() recognizer.recognize(her)