Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Are you getting datetime of when the user favorites or the tweet created date for liked tweets? #1610

Closed
youssefavx opened this issue Jun 15, 2021 · 3 comments
Labels
API This is regarding Twitter's API Discussion This was converted/moved from/to a discussion Invalid This is not valid Question This is a question

Comments

@youssefavx
Copy link

youssefavx commented Jun 15, 2021

Hi, I just tried both tweepy and the twitter api (v2) and noticed an interesting discrepancy.

On tweepy, I used this:

api = tweepy.API(auth)
User = "StackOverflow"
tweepy.Cursor(api.favorites, id=User).items(20)

And I believe I used v1.

When I got the liked tweets by the user, the dates were different than when I tried to get the same tweets using v2 like so:

import requests
import os
import json
from types import SimpleNamespace

def convert_json_to_python_object(data):

    # Parse JSON into an object with attributes corresponding to dict keys.
    x = json.loads(data, object_hook=lambda d: SimpleNamespace(**d))
    return x



username = 'Stackoverflow'
bearer_token = 'BEARER'
headers = {
    'Authorization': f"Bearer {bearer_token}",
}
max_results = 5
tweet_fields = 'created_at'
user_fields = 'username,description'
expansions = 'author_id'

response = requests.get(f'https://api.twitter.com/2/users/{get_ids_from_usernames(username)}/liked_tweets?max_results={max_results}&tweet.fields={tweet_fields}&expansions={expansions}&user.fields={user_fields}', headers=headers)
results.append(response.content)
response_as_object = convert_json_to_python_object(response.content)

When I then do this to the object:

response_as_object.data[0].created_at

I noticed that the created date differs from the date that I get when I use tweepy.

So I'm wondering what tweepy does here, because I actually want to get the dates that tweepy gets (if they are the date that the tweet was liked, but the documentation does not say anything about how to do that: https://developer.twitter.com/en/docs/twitter-api/tweets/likes/api-reference/get-users-id-liked_tweets#tab1 )

Or at least I dont see where or how to do so.

Would love any help whatsoever on how to do this with the twitter v2 api!

@Harmon758
Copy link
Member

The GET /2/users/:id/liked_tweets endpoint was only just released a few weeks ago and support for it hasn't been added yet (see #1472), but using an implementation I have in a stash and haven't pushed yet, I'm unable to reproduce this:

>>> favorites = api.get_favorites(screen_name="StackOverflow")
>>> favorites[0].created_at
datetime.datetime(2021, 6, 2, 16, 40, 36, tzinfo=datetime.timezone.utc)

>>> client.get_user(username="StackOverflow")
Response(data=<User id=128700677 name=Stack Overflow username=StackOverflow>, includes={}, errors=[], meta={})
>>> response = client.get_liked_tweets(id=128700677, tweet_fields=["created_at"])
>>> response.data[0].created_at
datetime.datetime(2021, 6, 2, 16, 40, 36)

Both of these match with the most recent liked Tweet at https://twitter.com/StackOverflow/likes currently being https://twitter.com/spolsky/status/1400130239573942274.

As for getting when a Tweet was favorited/liked, that's not something Twitter's API supports.

@Harmon758 Harmon758 added API This is regarding Twitter's API No Reproduction This is not reproducible Question This is a question labels Jun 15, 2021
@youssefavx
Copy link
Author

youssefavx commented Jun 15, 2021

I cannot thank you enough for what you just posted. You made me realize that the problem actually has nothing to do with any discrepancy between the two, but rather that when I received the data from twitter, I didn't correctly parse it so that the dates match the correct tweets (specifically users, since those come separate from the tweet). (My bad! really sorry!)

"As for getting when a Tweet was favorited/liked, that's not something Twitter's API supports."

Also thank you for that, was wondering whether that was even a feature!

@youssefavx
Copy link
Author

youssefavx commented Jun 15, 2021

This is what I was doing:

for favorite, user_data in zip(one_hundred_favorites.data, one_hundred_favorites.includes.users):

I was assuming that for every tweet, there was a username in the corresponding index of that tweet.

But it seems that is not the case.

After checking the length of tweets I get, and the length of users, I get 100 tweets, and 66 users.

@Harmon758 Harmon758 added Invalid This is not valid and removed No Reproduction This is not reproducible labels Jun 17, 2021
@tweepy tweepy locked and limited conversation to collaborators Jun 17, 2021
@Harmon758 Harmon758 added the Discussion This was converted/moved from/to a discussion label Jun 17, 2021

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
API This is regarding Twitter's API Discussion This was converted/moved from/to a discussion Invalid This is not valid Question This is a question
Projects
None yet
Development

No branches or pull requests

2 participants