Twitter: Get tweets for a particular time period - twitter

I am new to Twitter APIs. I am implementing a module in Java which shows popularity of a #hashtag. So I have to collect tweets for regular time interval.
Is there a way to get all the tweets that contains particular #hashtag, so I can the count total tweets each time and get the popularity? Below URL give me tweets but it's not consistent. Doesn't give me all tweets.
http://search.twitter.com/search.json?q=%23example

Why are you using urls use Api Twitter 4j
http://twitter4j.org/en/
You can perform this function eaisly with that
With Twitter4J, you can easily integrate your Java application with the Twitter service.
But Twitter4J is an unofficial library.
Use this function of that api
Search for Tweets
// The factory instance is re-useable and thread safe.
Twitter twitter = TwitterFactory.getSingleton();
Query query = new Query("source:twitter4j yusukey");
QueryResult result = twitter.search(query);
for (Status status : result.getStatuses()) {
System.out.println("#" + status.getUser().getScreenName() + ":" + status.getText());
}
Check this url for snippets
http://twitter4j.org/en/code-examples.html

Related

How to retrieve conversations and replies of old tweets using Tweepy from conversation_id?

I have the Academic Research access to Twitter's API and have been using Tweepy to access the API. My problem is I cannot retrieve the tweets from older tweets
This is the code attempting to retrieve the tweets using the conversation_id, from 2014
# https://twitter.com/NintendoAmerica/status/535462600294035456
start_time = '2014-11-01T00:00:00Z'
end_time = '2014-12-12T00:00:00Z'
tweets = client.search_all_tweets(query = 'conversation_id:535462600294035456', max_results = 500, start_time=start_time, end_time=end_time)
and the output is:
Response(data=[<Tweet id=535465221679489024 text='#NintendoAmerica #Pokemon [this was a link I had to remove]'>], includes={}, errors=[], meta={'newest_id': '535465221679489024', 'oldest_id': '535465221679489024', 'result_count': 1})
which is only one seemingly random tweet amongst many.
However, when I tried running the same code on a more recent tweet, it retrieved all the tweets. I do not have to specify a start/end time because it's a tweet from the past 30 days.
# https://twitter.com/380kmh/status/1545477360916373504
tweets = client.search_all_tweets(query = 'conversation_id:1545477360916373504', max_results = 500)
the output was complete (shortened, I removed the tweets):
Response(data=[...], meta={'newest_id': '1546465585093087235', 'oldest_id': '1545477768229670912', 'result_count': 18})
I followed Tweepy's documentation here, using Client.search_all_tweets:
https://docs.tweepy.org/en/stable/client.html#tweepy.Client.search_all_tweets
I also tried using Postman to retrieve the tweets but it came out empty, even though I followed the documentation here:
https://developer.twitter.com/en/docs/twitter-api/tweets/search/api-reference/get-tweets-search-all#tab0
Query: https://api.twitter.com/2/tweets/search/all?query=conversation_id%3A537923834557771776&start_time=2014-11-01T00:00:00.000Z&end_time=2014-12-18T00:00:00.000Z&tweet.fields=in_reply_to_user_id,text
Output:
{
"meta": {
"result_count": 0
}
}
What am I doing wrong?

Social Studio Radian6 Get Tweets Data

I want to fetch various social media accounts data through Radian6 API (particularly Twitter).
I have my defined url as:
headers = {"access_token": key}
url = 'https://api.socialstudio.radian6.com/v3/posts?topics=1234567&startDate='
+ startDate + '&endDate=' + endDate
resp = session.get(url, headers=headers)
Json = json.loads(resp.content)
where duration between start and end date covers all my records in the current day.
In the JSON that I am getting, for the data fetched from sources other than twitter, I am getting the content text but for tweets, I am not getting any data.
Do I need to add special privileges to my account or is getting tweets data not possible through the API, or is it I am doing something wrong. Need help in this.

How to search for multiple twitter ids in one request using twitter4j library

How to search for multiple twitter ids in one request using twitter4j library. Even using in clause in Search parameter would be helpful. Please help!
Please find some sample code below to fetch tweets based on tweet_ids.
Like other twitter API's, this API is also rate limited, in a single call you can fetch status only for 100 ids. Check the documentation for more details.
Also use latest version of twitter4j lib.
ConfigurationBuilder config = new ConfigurationBuilder();
config.setOAuthConsumerKey("consumerKey");
config.setOAuthConsumerSecret("consumerSecret");
config.setOAuthAccessToken("accessToken");
config.setOAuthAccessTokenSecret("accessSecret");
Twitter twitter = new TwitterFactory(config.build()).getInstance();
long ids[] = new long [3];
ids[0] = 568363361278296064l;
ids[1] = 568378166512726017l;
ids[2] = 570544187394772992l;
ResponseList<Status> statuses = twitter.lookup(ids);
for (Status status : statuses) {
System.out.println(status.getText());
}

Issue with fetching tweets from timeline as Twitter doesnt return 'next_results'

I am trying to fetch tweets from Twitter timeline using the max_id field present in next_results as demonstrated here.(Twython search API with next_results)
For most of the queries I get the desired number(500+) of tweets but for some I get less than 200 tweets.
When looking into what is returned in 'next_results I found that after returning just 100 tweets, the response has next_results field missing. There cannot be just 200 tweets for the queried topic as I am searching for this topic when it is trending and on Twitter I can see more than 200 tweets.
Is anyone else also facing this issue. Is there any workaround ?
Below is the output where in the second response the 'next_results' field is missing.
TEST_PROGRAM >> Calling twitter to get tweets for #TheWorstLieEver
{u'count': 100, u'completed_in': 0.066, u'max_id_str': u'432942833725497345', u'since_id_str': u'0', **u'next_results': u'?max_id=432540545630494719&q=%23TheWorstLieEver&count=100&include
_entities=1&result_type=mixed'**, u'refresh_url': u'?since_id=432942833725497345&q=%23TheWorstLieEver&result_type=mixed&include_entities=1', u'since_id': 0, u'query': u'%23TheWorstLieEver
', u'max_id': 432942833725497345L}
{u'count': 100, u'completed_in': 0.111, u'max_id_str': u'432540545630494719', u'since_id_str': u'0', u'refresh_url': u'?since_id=432540545630494719&q=%23TheWorstLieEver&result_type=mixe
d&include_entities=1', u'since_id': 0, u'query': u'%23TheWorstLieEver', u'max_id': 432540545630494719L}
I got the answer. If you are looking for volume, you need to use Streaming API instead. Twitter search API focuses on relevance of query and hence might not return all tweets. Twitter documentation says Before getting involved, it's important to know that the Search API is focused on relevance and not completeness. This means that some Tweets and users may be missing from search results. If you want to match for completeness you should consider using a Streaming API instead.

Get Tweets with Pictures using twitter search api

Is it possible to get tweets which contain photos? I am currently using twitter search api and getting all the tweets having entity details by setting include_entities=true. I see picture details under media object but is there anyway to filter and get tweets objects which just have these media items. Or is there anyway in Twitter4j to do this query?
There is no specific way to specify that I need only photos or videos but you can filter the results based on filter:links or filter:images or filter:videos in your query along with include_entities=true.
For Example: To get the tweets that contain links since 2012-01-31, you query should have include_entities parameter as well as filter:links as shown as follows:
https://search.twitter.com/search.json?q=from%3Agoogle%20since%3A2012-01-31%20filter%3Alinks&include_entities=true"
As your need is to filter your tweets based on images/photos, I think you should use filter:images.
An example of your case would look like:
https://search.twitter.com/search.json?q=from%3Agoogle%20since%3A2012-01-31%20filter%3Aimages&include_entities=true"
Hope this helps.
With Latest twitter API I couldn't get filters work and I couldn't find either any explanation in their docs. Althought you can get all the tweets and then parse only the media ones. You can fire this if inside your parsing script:
if(this.entities.media != null){
//Parse the tweet
}
This is not the best solution but the worst part comes to twitter who's giving you more information and using more of its own resources.
In the lastest twitter API you can do it in the ConfigurationBuilder instance, before creating the Twitter instance:
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(false);
cb.setOAuthConsumerKey(API_KEY);
cb.setOAuthConsumerSecret(API_SECRET);
cb.setOAuthAccessToken(ACCESS_TOKEN);
cb.setOAuthAccessTokenSecret(SECRET_KEY);
// enabling include_entities parameters
cb.setIncludeEntitiesEnabled(true);
Twitter twitterInstance = new TwitterFactory(cb.build()).getInstance();
Also, after enabling the entities, in the search string you have to had the condition "filter:images".
List<String> keywords = new ArrayList<String>();
keywords.add("#pet");
keywords.add("cat");
// String.join for Java 8
String twitterSearchString = "((" + String.join(" OR ", keywords) + ")";
// adding the filter condition
twitterSearchString += " AND filter:images)";
Query q = new Query(twitterSearchString);
And you will get just results with images (tested with twitter4j-core 4.0.4).
For filter in twitter API you can check official document for latest version as on date Apr 02 2019
https://developer.twitter.com/en/docs/tweets/search/guides/standard-operators.html

Resources