简体   繁体   English

tweepy:找到转发的原始作者

[英]tweepy: finding the original author of a retweet

I'm kind of a python newb and stuck with tweepy here. 我有点像python newb,在这里卡住了tweepy。

What I'm trying to do is bring in a bunch of user and tweet objects into a neo4j database with tweet and retweet relationships. 我想做的是将一堆用户和tweet对象引入具有tweet和转推关系的neo4j数据库中。 My problem is in determining if a given status object is a retweet and if so the screen_name and id_str of the original author. 我的问题是确定给定的状态对象是否为转推,如果是,则为原作者的screen_name和id_str。

I can see the data if I print out tweet.retweets but I can't figure out how to get to it. 如果我打印出tweet.retweets,我可以看到数据,但是我不知道如何去处理它。 tweepy's docs mention something about object models and for more information check out ModelsReference but google isn't helping me much here. tweepy的文档中提到了一些有关对象模型的信息,有关更多信息,请查看ModelsReference,但google在这里没有太大帮助。

any help would be great, even just pointing me in the right direction. 任何帮助都是很好的,即使只是指向正确的方向。 Thanks 谢谢

Sample code 样例代码

tweets=api.get_timeling(1234556)
twitter_user=api.get_user(123456)
for tweet in tweets:
    neo4j_create_tweet_node(tweet)
    if tweet.user.id == twitter_user.id:
        create_tweet_relationship(twitter_user,tweet)
    elif tweet.user.id != twitter_user.id:
        create_retweet_relationship(twitter_user,tweet)

假设tweet是转推,originalAuthorID = tweet.retweeted_status.user.id_str;

According to the Twitter API documentation : 根据Twitter API文档

Retweets can be distinguished from typical Tweets by the existence of a retweeted_status attribute. 通过存在retweeted_status属性,可以将retweeted_status与典型的retweeted_status区分开。 This attribute contains a representation of the original Tweet that was retweeted. 此属性包含被转发的原始Tweet的表示。 Note that retweets of retweets do not show representations of the intermediary retweet, but only the original tweet. 请注意,转发的转发不会显示中间转发的表示,而只会显示原始的转发。

I would use hasattr() to look for the presence of the retweeted_status atttibute in each Tweetpy tweet object. 我将使用hasattr()查找每个Tweetpy tweet对象中是否存在retweeted_status属性。

The following code (where create_tweet_relationship() and create_retweet_relationship() are functions you have defined as in your example) seems like it should would work: create_retweet_relationship()下面的代码(其中create_tweet_relationship()create_retweet_relationship()是您在示例中定义的函数)似乎应该可以工作:

for tweet in tweets:
    if hasattr(tweet, 'retweeted_status'):
         create_tweet_relationship(tweet.retweeted_status.author, tweet)
    else:
         create_retweet_relationship(tweet.author, tweet)

If the tweet is a retweet of another, the original retweeted status is included in the JSON object in the field "retweeted_status". 如果该推文是另一条推文,则原始转发状态包括在JSON对象的“ retweeted_status”字段中。 You'd get the user information there under the "user" field. 您将在“用户”字段下获得用户信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM