Collect tweets from a certain country - python-3.x

I am trying to run the Twitter Streaming API to collect only tweets from a certain country.
Trying to write long/lat coordinates as a filter gives the syntax error:
positional argument follows keyword argument
I'm willing to use the geotag to filter by country or city, but have no idea what needs to be written into the code to do that.
A solution to stream only tweets from an approximate location would be great.
# Import the necessary methods from tweepy library
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream
# Variables that contain the user credentials to access Twitter API
access_token = "put your keys here"
access_token_secret = "puts your keys here"
consumer_key = "puts your keys here"
consumer_secret = "puts your keys here"
#This is a basic listener that just prints received tweets to stdout.
class StdOutListener(StreamListener):
def on_data(self, data):
print(data)
return True
def on_error(self, status):
print(status)
if __name__ == '__main__':
#This handles Twitter authentification and the connection to Twitter
Streaming API
l = StdOutListener()
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
stream = Stream(auth, l)

You need to supply the bounding box for your location in latitude and longitude. Something like this,
stream.filter(locations=[-74,40,-73,41])
Here is Twitter's documentation.

Related

How to make a Mystreamlistener class for tweepy

`import tweepy
import stream
import openai
from PIL import Image
from io import BytesIO
# Twitter API credentials
consumer_key = "removed"
consumer_secret = "removed"
access_key = "removed"
access_secret = "removed"
# OpenAI API credentials
openai_key = "removed"
# set up tweepy to authenticate with Twitter
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)
# set up OpenAI
openai.api_key = openai_key
# create a tweepy API object
api = tweepy.API(auth)
# function to handle incoming tweets that mention your account
def handle_mention(tweet):
# get the text of the tweet
text = tweet.text
# generate a response using ChatGPT
response = openai.Completion.create(
model="text-davinci-002",
prompt=text,
temperature=0.5,
max_tokens=1024,
top_p=1,
frequency_penalty=0,
presence_penalty=0
)
# take a screenshot of the ChatGPT response
img = Image.new("RGB", (1024, 1024), "white")
d = ImageDraw.Draw(img)
font = ImageFont.truetype("font.ttf", 36)
d.text((10, 10), response, font=font, fill=(0, 0, 0))
img_bytes = BytesIO()
img.save(img_bytes, format="PNG")
img_bytes.seek(0)
# tweet the screenshot
api.update_with_media(
filename="response.png",
file=img_bytes,
in_reply_to_status_id=tweet.id
)
# function to listen for tweets that mention your account
def listen_for_mentions():
# create a tweepy Stream object to listen for mentions
stream = tweepy.stream(auth, listener=MyStreamListener(), bearer_token="removed", track=["#askAIguy"])
# create a tweepy StreamListener to handle incoming tweets
class MyStreamListener(stream):
def on_status(self, status):
# only handle tweets that mention #askaiguy
if "#" in status.text and "#askaiguy" in status.text:
# parse the tweet text to extract the mention of #askaiguy
tweet_text = status.text.lower()
mention_start = tweet_text.index("#askaiguy")
mention_end = mention_start + len("#askaiguy")
mention = tweet_text[mention_start:mention_end]
# handle the mention by calling the handle_mention() function
handle_mention(status)
# start listening for mentions of your account
listen_for_mentions()`
This is the error response I keep getting:
Traceback (most recent call last):
File "main.py", line 67, in <module>
class MyStreamListener(stream):
# TypeError: module() takes at most 2 arguments (3 given)
I know there was an update to the tweepy API and it merged StreamListener into Stream. Can someone please give me some guidance as to what I'm missing?
I've tried changing the name of the funciton, rearranging the structure, removing the arguments given, etc. I've spent about 4 hours working on this problem researching and I've gotten progressively closer but I can't seem to get the bot to listen for mentions.

Error when trying to get full_text for a Tweet using Tweepy and Python

I am trying to use Tweepy and streaming to track Tweets in real time. I am using the following which works fine:
import tweepy
import configparser
import sys
#read configs
config = configparser.ConfigParser()
config.read('config.ini')
api_key = config['twitter']['api_key']
api_key_secret = config['twitter']['api_key_secret']
access_token = config['twitter']['access_token']
access_token_secret = config['twitter']['access_token_secret']
class StreamCollector(tweepy.Stream):
def on_status(self, status):
if not hasattr(status, 'retweeted_status') and status.in_reply_to_screen_name == None and status.i\
s_quote_status == False:
if status.author.followers_count > 100000:
print('Twitter Handle: #'+status.author.screen_name)
print('Followers:',status.author.followers_count)
print('Tweet:',status.text)
print('\n')
#print(status.user.screen_name.encode('UTF-8'))
stream = StreamCollector(api_key,api_key_secret,access_token, access_token_secret)
stream.filter(track=["table"])
However, I want to produce the untruncated Tweet. I tried substituting status.text for status.full_text but I got the error:
AttributeError: 'Status' object has no attribute 'full_text'
My version of Tweepy is 4.5.0 and Python is 3.9.9.
The tweepy.API has a compatibility mode and extended mode. The extended mode should allow you to get the full text of the Tweet.
ref: Extended Tweets
Here is the code with the extended mode call.
import sys
import tweepy
import configparser
#read configs
config = configparser.ConfigParser()
config.read('config.ini')
api_key = config['twitter']['api_key']
api_key_secret = config['twitter']['api_key_secret']
access_token = config['twitter']['access_token']
access_token_secret = config['twitter']['access_token_secret']
class StreamCollector(tweepy.Stream):
def on_status(self, status):
if not hasattr(status, 'retweeted_status'):
if status.in_reply_to_screen_name is None and status.is_quote_status is False:
if status.author.followers_count > 100000:
print(f'Twitter Handle: #{status.author.screen_name}')
print(f'Followers: {status.author.followers_count}')
if 'extended_tweet' in status._json:
full_text = status._json['extended_tweet']['full_text']
print(f'Tweet: {full_text}')
elif 'extended_tweet' not in status._json:
print(f'Tweet: {status.text}')
print('\n')
stream = StreamCollector(api_key,api_key_secret,access_token, access_token_secret)
stream.filter(track=["table"])
Streaming is covered in Tweepy's documentation on extended Tweets:
By default, the Status objects from streams may contain an extended_tweet attribute representing the equivalent field in the raw data/payload for the Tweet. This attribute/field will only exist for extended Tweets, containing a dictionary of sub-fields. The full_text sub-field/key of this dictionary will contain the full, untruncated text of the Tweet

Tweepy return tweets for a particuler screen name only if the user tweets a new tweet

I am using python and Tweepy in a script that loops and prints the tweet id, the time it was created and the full text. The problem am having is that it keeps on printing the same id and full text if there is no new tweet, is it possible to have an if statement or a listener that will only print the id, time created and full text only once and wait until the user creates a new tweet.
Below is my code:
import tweepy
import tkinter
import time
CONSUMER_KEY= 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
CONSUMER_SECRET= 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
ACCESS_TOKEN= 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
ACCESS_TOKEN_SECRET= 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
api = tweepy.API(auth, wait_on_rate_limit=True, wait_on_rate_limit_notify=True)
# set parser=tweepy.parsers.JSONParser() if you want a nice printed json response.
user = api.me()
while True:
tweets = api.user_timeline(screen_name = "elonmusk",
count=1,
include_rts = False,
tweet_mode = 'extended'
)
for detailsInTweet in tweets:
print("ID: {}".format(detailsInTweet.id))
# Tweet creation time
print(detailsInTweet.created_at, "\n")
# Full tweet text
print(detailsInTweet.full_text, "\n")
print("\n")
time.sleep(5)

How to block specific countries on YouTube Search List from YouTube API?

I am trying to "block" X country from the search list, but I can't use an operator such as -"X" also I tried some other operators and techniques but none of that worked. I get this Error :
TypeError: bad operand type for unary -: 'str'
I don't know how can I tell YouTube to say I don't want this X, Y, and Z countries on my search list.
here's my code:
import os
import google_auth_oauthlib.flow
import googleapiclient.discovery
import googleapiclient.errors
scopes = ["https://www.googleapis.com/auth/youtube.force-ssl"]
def main():
# Disable OAuthlib's HTTPS verification when running locally.
# *DO NOT* leave this option enabled in production.
os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1"
api_service_name = "youtube"
api_version = "v3"
client_secrets_file = "YOUR_CLIENT_SECRET_FILE.json"
# Get credentials and create an API client
flow = google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file(
client_secrets_file, scopes)
credentials = flow.run_console()
youtube = googleapiclient.discovery.build(
api_service_name, api_version, credentials=credentials)
request = youtube.search().list(
part="snippet",
eventType="completed",
order="videoCount",
regionCode="X", # X is a country code as an example
safeSearch="moderate",
topicId="/m/01k8wb",
type="video",
videoDuration="short",
videoLicense="youtube"
)
response = request.execute()
print(response)
if __name__ == "__main__":
main()
It is not possible and, here is the report from Google :
https://issuetracker.google.com/150359410

How can I stop tweets from repeating?

I am running a code that gets tweets from the Twitter API and saving them to a txt file. the code is as follows:
from tweepy import Stream
from tweepy import OAuthHandler
from tweepy.streaming import StreamListener
import json
import sentmod as s
#consumer key, consumer secret, access token, access secret.
ckey= "xxxxxxxxxxxxxxxxxxx"
csecret="xxxxxxxxxxxxxxxxxxx"
atoken="xxxxxxxxxxxxxxxxxxx"
asecret="xxxxxxxxxxxxxxxxxxx"
class listener(StreamListener):
def on_data(self, data):
all_data = json.loads(data)
tweet = all_data["text"]
sentiment_value, confidence = s.sentiment(tweet)
tweet.encode('ascii', 'ignore')
tweets= open("tweets.txt","a",encoding="utf-8")
tweets.write(tweet)
tweets.write('\n\n\n')
tweets.close()
print(tweet, sentiment_value, confidence)
if confidence*100 >= 60:
output = open("twitter-out.txt","a")
output.write(sentiment_value)
output.write('\n\n\n')
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"],languages=['en']) #locations=[]
When I add the tweets to a text file the tweets sometimes get repeated tweets, how can I fix that?

Resources