def main(): # This call to simple_twit.create_api will create the api object that # Tweepy needs in order to make authenticated requests to Twitter's API. # Do not remove or change this function call. # Pass the variable "api" holding this Tweepy API object as the first # argument to simple_twit functions. api = simple_twit.create_api()] simple_twit.version() # Project 04 Exercises # Exercise 1 - Get and print 10 tweets from your home timeline tweet10 = simple_twit.get_home_timeline(api=api, count=20) for t in tweet10: print(t.full_text) # Exercise 2 - Get and print 10 tweets from another user's timeline Spy10 = simple_twit.get_user_timeline(api = api, user= "******", count = 20) for st in Spy10: print(st.full_text) # Exercise 3 - Post 1 tweet to your timeline. simple_twit.send_tweet(api=api, text="I tweeted my first tweet, Hold my head") # Exercise 4 - Post 1 media tweet to your timeline. simple_twit.send_media_tweet(api=api, text="This is a picture from my archives", filename= "C:/Users/Glen F/OneDrive/Desktop/test.png") simple_twit.send_tweet(api=api, text="test #2 I think, media isn't working")
def main(): # This call to simple_twit.create_api will create the api object that # Tweepy needs in order to make authenticated requests to Twitter's API. # Do not remove or change this function call. # Pass the variable "api" holding this Tweepy API object as the first # argument to simple_twit functions. api = simple_twit.create_api() # YOUR CODE BEGINS HERE name = "tmia_IAE101" user = simple_twit.get_user(api, name) time_line = simple_twit.get_user_timeline(api, name, count=5) home_time_line = simple_twit.get_home_timeline(api, count=5) #weather bot key = "440cb9d576a4f8df23fb00a4e3a2dde2" latitude = "40.916168" longitude = "-73.118309" url = f"https://api.darksky.net/forecast/{key}/{latitude},{longitude}" data = requests.get(url).json() currently = data['currently']["summary"] current_temp = data['currently']['temperature'] try: precipitation = data["currently"]["precipType"] except KeyError: pass precipitation_probability = str(data["currently"]["precipProbability"] * 100) if float(precipitation_probability) in [100, 100.0]: try: message = f"Currently in Stony Brook: {currently} with a temperature of {current_temp}°F\nPrecipitation probability: {precipitation_probability}%\nAnd now a word from Ollie: " except KeyError: pass tweet = simple_twit.send_media_tweet(api, message, "ollie_raining_sideways.jpg") thanks = simple_twit.send_tweet(api, "Thanks Ollie.") elif float(precipitation_probability) in range(50, 100): try: message = f"Curreently: {currently} with a temperature of {current_temp}°F\nPrecipitation probability: {precipitation_probability}%" except KeyError: pass tweet = simple_twit.send_media_tweet(api, message, "ollie.jpg") thanks = simple_twit.send_tweet(api, "Thanks Ollie.") else: message = f"Currently in Stony Brook: {currently} with a temperature of {current_temp}°F\nPrecipitation probability: {precipitation_probability}%" tweet = simple_twit.send_tweet(api, message) simple_twit.version()
def main(): # This call to simple_twit.create_api will create the api object that # Tweepy needs in order to make authenticated requests to Twitter's API. # Do not remove or change this function call. # Pass the variable "api" holding this Tweepy API object as the first # argument to simple_twit functions. api = simple_twit.create_api() simple_twit.version() # Project 04 Exercises # Exercise 1 - Get and print 10 tweets from your home timeline # Exercise 2 - Get and print 10 tweets from another user's timeline # Exercise 3 - Post 1 tweet to your timeline. # Exercise 4 - Post 1 media tweet to your timeline. # YOUR BOT CODE BEGINS HERE # mytweets = simple_twit.get_home_timeline(api,10) # print("-------------10 Tweets from my timeline-------------") # print() # # # # # for tweet in mytweets: # print(tweet.full_text) # # # print() # elsetweets = simple_twit.get_user_timeline(api, "nice_tips_bot", 10) # print("-------------10 Tweets from @nice_tips_bot-------------") # print() # for tweet in elsetweets: # print(tweet.full_text) print("-------------Posting a tweet-------------") while True: event = get_event() image_status = download_image(api, get_image(event["url"])) message = "Today in year {y}, {e}".format(y=event["year"], e=event["event"]) print(message) if (image_status == 1): simple_twit.send_media_tweet(api, message, "temp.jpg") os.remove("temp.jpg") elif (image_status == 0): simple_twit.send_tweet(api, message) time.sleep(900)
def main(): # This call to simple_twit.create_api will create the api object that # Tweepy needs in order to make authenticated requests to Twitter's API. api = simple_twit.create_api() simple_twit.version() CONFIG_FILE = "twitter_bot.config" consumer_key = "#################"; consumer_secret = "################" f = open(CONFIG_FILE, "r") config = json.load(f) access_token = config["access_token"] access_token_secret = config["access_secret"] f.close() auth = OAuthHandler(consumer_key, consumer_secret) auth.set_access_token(access_token, access_token_secret) myListener = MyStreamListener() myStream = tweepy.Stream(auth, listener = myListener) keywords = ['Yankees', 'Cole', 'Cashman'] myStream.filter(track = keywords) tweets = myListener.tweets#tweets is a list of string objects now #print(tweets) #tweets.extend(simple_twit.get_home_timeline(api, 3)) sid = SentimentIntensityAnalyzer() aggs=[0,0,0,0]#element 0 is total compound, element 1 is total negative, element 2 is total neutral, element 3 is total positive for tweet in tweets: #print(tweet) ss = sid.polarity_scores(tweet) x=0 for k in sorted(ss):#k is key #print('{0}: {1}, '.format(k, ss[k]), end='') #print(ss[k]) #print() aggs[x]+=ss[k] x+=1 #print("AGGREGATE") #print(aggs[0]) #print(aggs[1]) #print(aggs[2]) #print(aggs[3]) aggtext = ("For the query using keywords " + str(keywords) + ", the average sentiment values are as follows. Compound "+str(aggs[0]/15.0)+", negative " + str(aggs[1]/15.0)+", neutral " + str(aggs[2]/15.0)+", positive " + str(aggs[3]/15.0)) print(aggtext) #for tweet in tweets: #print("MY PRINT " + tweet) #simple_twit.send_tweet(api,tweet) simple_twit.send_tweet(api, aggtext)
def main(): # This call to simple_twit.create_api will create the api object that # Tweepy needs in order to make authenticated requests to Twitter's API. # Do not remove or change this function call. # Pass the variable "api" holding this Tweepy API object as the first # argument to simple_twit functions. api = simple_twit.create_api() simple_twit.version() # Project 04 Exercises ## # Exercise 1 - Get and print 10 tweets from your home timeline ## myTimeline = simple_twit.get_home_timeline(api, 10) ## for tweet in myTimeline: ## print(tweet.id) ## print(type(tweet.user)) ## print(tweet.author.name) ## print(tweet.user.name) ## print(tweet.full_text) ## print() ## ## # Exercise 2 - Get and print 10 tweets from another user's timeline ## careBotTimeline = simple_twit.get_user_timeline(api,"tinycarebot", 10) ## for tweet in careBotTimeline: ## print(tweet.id) ## print(type(tweet.user)) ## print(tweet.author.name) ## print(tweet.user.name) ## print(tweet.full_text) ## print() ## ## # Exercise 3 - Post 1 tweet to your timeline. ## myPost = simple_twit.send_tweet(api, "Exercise #3 on mytwitterbot.py (third run)") ## print(type(myPost)) ## ## # Exercise 4 - Post 1 media tweet to your timeline. ## myMediaPost = simple_twit.send_media_tweet(api, "Exercise #4 on mytwitterbot.py", "cat.jpg") ## print(type(myMediaPost)) # YOUR BOT CODE BEGINS HERE while True: year = datetime.now() currentYear = year.year today = date.today() d = today.strftime("%B %d, %Y") present = datetime.now() future = datetime(currentYear, 1, 1, 12, 0, 0) difference = present - future newDifference = 366 - difference.days tweet = "Today is " + str(d) + "." tweet2 = "Days left till " + str(currentYear + 1) + ": " + str(newDifference) if newDifference == 0: finalTweet = "HAPPY NEW YEAR!" else: finalTweet = tweet+ "\n"+ tweet2 print(finalTweet) myPost = simple_twit.send_tweet(api, finalTweet) print(type(myPost)) time.sleep(86400)
def main(): # This call to simple_twit.create_api will create the api object that # Tweepy needs in order to make authenticated requests to Twitter's API. # Do not remove or change this function call. # Pass the variable "api" holding this Tweepy API object as the first # argument to simple_twit functions. api = simple_twit.create_api() simple_twit.version() # Project 04 Exercises # # Exercise 1 - Get and print 10 tweets from your home timeline # print('Exercise 1:') # home_tweets = simple_twit.get_home_timeline(api, 10) # for tweet in home_tweets: # print('Name: ' + str(tweet.author.name)) # print('ID: ' + str(tweet.id)) # print('Tweet: ' + str(tweet.full_text)) # print() # # # Exercise 2 - Get and print 10 tweets from another user's timeline # print('Exercise 2:') # elon_tweets = simple_twit.get_user_timeline(api, 'elonmusk', 10) # for tweet in elon_tweets: # print('Name: ' + str(tweet.author.name)) # print('ID: ' + str(tweet.id)) # print('Tweet: ' + str(tweet.full_text)) # print() # # print(simple_twit.get_user(api, 'elonmusk')) # # Exercise 3 - Post 1 tweet to your timeline. # print('Exercise 3:') # post_tweet = 'This is a test tweet for exercise 3' # simple_twit.send_tweet(api, post_tweet) # print(post_tweet) # print() # # # Exercise 4 - Post 1 media tweet to your timeline. # print('Exercise 4:') # post_media_tweet_text = 'This is a test tweet text for exercise 4' # simple_twit.send_media_tweet(api, post_media_tweet_text, 'test31.png') # print(post_media_tweet_text) # YOUR BOT CODE BEGINS HERE marvel_lines = [ 'We\'re Fighting An Army Of Robots And I Have A Bow And Arrow. None Of This Makes Sense.', 'Whatever It Takes...', 'If You\'re Nothing Without The Suit, Then You Shouldn\'t Have It.', 'I Have Nothing To Prove To You.', 'She\'s Not Alone.', 'I Can Do This All Day.', 'That\'s My Secret Cap; I\'m Always Angry.', 'Wakanda Forever!', 'I Am Iron Man.', 'We Have A Hulk.' 'There Was An Idea...', 'We Are Groot!', 'If We Can\'t Protect The Earth, You Can Be Damn Well Sure We\'ll Avenge It!', 'Dormammu, I\'ve Come To Bargain.', 'Part of the journey is the end.', 'No amount of money ever bought a second of time.', 'SteveRogersSad.png', 'captain-america-civil-war.png', 'captain-marvel.png', 'thor-horse.png', 'thor-adopted.png', 'tony-spaceship.png', 'spiderman-fury.png', 'captain-marvel-w-friend.png', ] while True: pick = random.choice(marvel_lines) print(pick) if pick.split('.')[-1] == 'png': simple_twit.send_media_tweet(api, 'An iconic marvel image', pick) else: simple_twit.send_tweet(api, pick) time.sleep(900)