I'm having trouble getting the user_id from a specific tweet_id. Is there a way to get the author of a tweet id using Tweepy?
For the author of that tweet, or retweeted/quoted status try.
status = api.get_status(id)
author = status.author
retweeted_status_author = status.retweeted_status.author
quoted_status_author = status.quoted_status.author
Related
I am using academic account to retrieve tweet information but I don't know how to get the status_id, I thought the conversation_id would be the same as status_id but when I track back, apparently it is not. What should I add to the tweet field?
for response in tweepy.Paginator(client.search_all_tweets,
query = 'query -is:retweet lang:en',
user_fields = ['username', 'public_metrics', 'description', 'location'],
tweet_fields = ['created_at', 'geo', 'public_metrics', 'text','id','conversation_id'],
expansions = ['author_id', 'geo.place_id'],
start_time = ['2020-01-01T00:00:00Z'],
end_time = ['2020-12-12T00:00:00Z']):
time.sleep(1)
tweets.append(response)
result
You've already got it - "id" is the status id
Tweets are the basic atomic building block of all things Twitter.
Tweets are also known as “status updates.” The Tweet object has a long
list of ‘root-level’ attributes, including fundamental attributes such
as id, created_at, and text
https://developer.twitter.com/en/docs/twitter-api/v1/data-dictionary/object-model/tweet
It may be a bit confusing because references to that id are labeled things like "in_reply_to_status_id" - but there is no field called "status_id" - it's just id.
I'm trying to assign a client role 'edit' to a group 'abc'.
All entities exists(clients, roles, group)
group_id = admin.get_group_by_path('/abc')['id']
id_client = admin.get_client_id('myclient')
admin.assign_group_client_roles(group_id, id_client,
{'name': 'edit'})
I get an error keycloak.exceptions.KeycloakPostError: 404: b'{"error":"Role not found"}'
Where can be an error?
Looking a bit a the open source code, I think you can try the following:
group_id = admin.get_group_by_path('/abc')['id']
id_client = admin.get_client_id('myclient')
clt_role = admin.get_client_role(client_id=id_client, role_name="edit")
admin.assign_group_client_roles(group_id, id_client, clt_role)
I'm new to Python, and so I'm struggling a bit with this. Basically, the code below gets the text of tweets with the hashtag bitcoin in it, and I want to extract the date and author as well as the text. I've tried different things, but stuck rn.
Greatly appreciate any help with this.
import pandas as pd
import numpy as np
import tweepy
api_key = '*'
api_secret_key = '*'
access_token = '*'
access_token_secret = '*'
authentication = tweepy.OAuthHandler(consumer_key, consumer_secret_key)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(authentication, wait_on_rate_limit=True)
#Get tweets about Bitcoin and filter out any retweets
search_term = '#bitcoin -filter:retweets'
tweets = tweepy.Cursor(api.search_tweets, q=search_term, lang='en', since='2018-11-01', tweet_mode='extended').items(50)
all_tweets = [tweet.full_text for tweet in tweets]
df = pd.DataFrame(all_tweets, columns=['Tweets'])
df.head()
If you use dir(tweet) then you see all variables and functions in object tweet
author
contributors
coordinates
created_at
destroy
display_text_range
entities
extended_entities
favorite
favorite_count
favorited
full_text
geo
id
id_str
in_reply_to_screen_name
in_reply_to_status_id
in_reply_to_status_id_str
in_reply_to_user_id
in_reply_to_user_id_str
is_quote_status
lang
metadata
parse
parse_list
place
possibly_sensitive
retweet
retweet_count
retweeted
retweets
source
source_url
truncated
user
And there is created_at
all_tweets = []
for tweet in tweets:
#print('\n'.join(dir(tweet)))
all_tweets.append( [tweet.full_text, tweet.created_at] )
df = pd.DataFrame(all_tweets, columns=['Tweets', 'Created At'])
df.head()
Result:
Tweets Created At
0 #Ralvero Of course $KAWA ready for 100x 🚀#ETH ... 2022-03-26 13:51:06+00:00
1 Pairs:1INCHUSDT \n SELL:1.58500\n Time :3/26/2... 2022-03-26 13:51:06+00:00
2 #hotcrosscom #iSafePal 🌐 First LIVE Dapp: Cylu... 2022-03-26 13:51:04+00:00
3 #Justdoitalex #Isabel_Schnabel Finally a truth... 2022-03-26 13:51:03+00:00
4 #Bitcoin has rejected for the fourth time the ... 2022-03-26 13:50:55+00:00
But your code have problem with since because it seems it was removed in version 3.8
See: Collect tweets in a specific time period in Tweepy, until and since doesn't work
I'm trying to fetch Tweets from multiple Twitter accounts and then create a database with the TWEETS and the source of the TWEET " user name " by using the following code
posts = api.user_timeline(screen_name = 'AlArabiya_Brk', count = 100 , lang =
"ar",tweet_mode="extended")
df = pd.DataFrame([tweet.full_text for tweet in posts], columns = [ 'Tweets'])
but I have a question: how can I add more than one account? I tried doing:
posts = api.user_timeline(screen_name = ['AlArabiya_Brk','AJABreaking'], count = 100
,lang ="ar",tweet_mode="extended")
but didn't get the desired output
You'll need to make multiple calls with that method.
That API endpoint only allows a single screen name input.
I am creating a book review website and currently encountering some difficulties in terms of appending (by looping) individual reviews to a book's detailed page.
I have come up with a crude idea but not yet complete.
Here's my code in the last 3 lines, I am going through a for loop to list the reviews - I am using the foreign key user_id or review.user_id to get the username and have it appended to the review.
It looks like it should have more than just .fetchone() How should I go about and finish this? I want the review to have its username by using user_id from users table and accurately place them on the correct individual reviews.
#app.route('/book/<string:isbn>', methods = ['POST','GET'])
def book(isbn):
#import columns from database
res = db.execute("SELECT * FROM books WHERE isbn = :isbn", {"isbn": isbn}).fetchone()
#assign book's id value to a variable.
book_id = res[-1]
if book_id is None:
raise "No book id!"
#import api from Goodreads (stats)
r = requests.get("https://www.goodreads.com/book/review_counts.json", params={"key": "L3FHyOR3IhCo3kctcUz3zg", "isbns": isbn})
if r.status_code != 200:
raise ValueError
reviews_count=r.json()["books"][0]["reviews_count"]
average_rating=r.json()["books"][0]["average_rating"]
username = session.get("username")
if request.method == "POST":
# get form data and date & time
review = request.form.get("comment")
rating = request.form.get("rating")
date = datetime.now()
# fetch user's id from users table
user_id, = db.execute("SELECT id FROM users WHERE username = :username",{"username":username}).fetchone()
print(f"user id is {user_id}")
# add review to the review table
db.execute("INSERT INTO reviews (book_id, user_id, review, rating, date) VALUES (:book_id, :user_id, :review, :rating, :date)", {"date":date, "review":review, "rating":rating, "user_id":user_id, "book_id":book_id})
db.commit()
# add reviews to the page using for loop
reviews = db.execute("SELECT * FROM reviews WHERE book_id = :book_id", {"book_id": book_id}).fetchall()
for review in reviews:
u = db.execute("SELECT username FROM users WHERE user_id = :user_id", {"user_id": review.user_id}).fetchone()
#what to do here to append username to each review?
return render_template("book.html", reviews_count = reviews_count, average_rating = average_rating, email = username, review_username=)