Skip to content
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

Timeline + Advanced Search #16

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/main/java/me/jhenrique/main/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import me.jhenrique.manager.TwitterCriteria;
import me.jhenrique.model.Tweet;

import java.util.List;

public class Main {

public static void main(String[] args) {
Expand All @@ -12,6 +14,7 @@ public static void main(String[] args) {
*/
TwitterCriteria criteria = null;
Tweet t = null;
List<Tweet> tweetList = null;

/**
* Example 1 - Get tweets by username
Expand Down Expand Up @@ -65,6 +68,27 @@ public static void main(String[] args) {
System.out.println("Mentions: " + t.getMentions());
System.out.println("Hashtags: " + t.getHashtags());
System.out.println();


/**
* Example 4 - Get all possible tweets by username (timeline tweets + advanced search tweets)
**/
System.out.println("### Example 4 - Get all possible tweets by username (timeline tweets + advanced search tweets) [github]");

System.out.println("Retrieving all tweets from github...");

criteria = TwitterCriteria.create()
.setUsername("@github");

tweetList = TweetManager.getTweets(criteria);

for (Tweet tweet : tweetList)
System.out.println("@" + tweet.getUsername() + " : " + tweet.getText() + tweet.getDate());


System.out.println();
System.out.println("Printed " + tweetList.size() + " tweets from @github.");

}

}
34 changes: 30 additions & 4 deletions src/main/java/me/jhenrique/manager/TweetManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public class TweetManager {
* @return JSON response used by Twitter to build its results
* @throws Exception
*/
private static String getURLResponse(String username, String since, String until, String querySearch, String scrollCursor) throws Exception {
private static String getURLResponse(String username, String since, String until, String querySearch, String scrollCursor, boolean advancedSearch, String maxId) throws Exception {
String appendQuery = "";
if (username != null) {
appendQuery += "from:"+username;
Expand All @@ -58,7 +58,15 @@ private static String getURLResponse(String username, String since, String until
appendQuery += " "+querySearch;
}

String url = String.format("https://twitter.com/i/search/timeline?f=realtime&q=%s&src=typd&max_position=%s", URLEncoder.encode(appendQuery, "UTF-8"), scrollCursor);
String url = null;

if(advancedSearch)
url = String.format("https://twitter.com/i/search/timeline?f=realtime&q=%s&src=typd&max_position=%s", URLEncoder.encode(appendQuery, "UTF-8"), scrollCursor);
else
if(maxId == null)
url = String.format("https://twitter.com/i/profiles/show/%s/timeline/with_replies", username);
else
url = String.format("https://twitter.com/i/profiles/show/%s/timeline/with_replies?max_position=%s", username, maxId);

HttpGet httpGet = new HttpGet(url);
HttpEntity resp = defaultHttpClient.execute(httpGet).getEntity();
Expand All @@ -76,9 +84,24 @@ public static List<Tweet> getTweets(TwitterCriteria criteria) {

try {
String refreshCursor = null;
String refreshMaxId = null;
String mostRecentTweetId = null;
boolean advancedSearch = false;
if (criteria.getUsername() == null )
advancedSearch = true;

outerLace: while (true) {
JSONObject json = new JSONObject(getURLResponse(criteria.getUsername(), criteria.getSince(), criteria.getUntil(), criteria.getQuerySearch(), refreshCursor));
refreshCursor = json.getString("min_position");
JSONObject json = new JSONObject(getURLResponse(criteria.getUsername(), criteria.getSince(), criteria.getUntil(), criteria.getQuerySearch(), refreshCursor, advancedSearch, refreshMaxId));

if (json.has("min_position") && !json.isNull("min_position"))
refreshCursor = json.getString("min_position");

if(json.has("min_position") && json.isNull("min_position") && !advancedSearch) {
advancedSearch = true;
refreshCursor = "TWEET-"+ refreshCursor + "-" + mostRecentTweetId;
json = new JSONObject(getURLResponse(criteria.getUsername(), criteria.getSince(), criteria.getUntil(), criteria.getQuerySearch(), refreshCursor, advancedSearch, refreshMaxId));
}

Document doc = Jsoup.parse((String) json.get("items_html"));
Elements tweets = doc.select("div.js-stream-tweet");

Expand All @@ -93,6 +116,8 @@ public static List<Tweet> getTweets(TwitterCriteria criteria) {
int favorites = Integer.valueOf(tweet.select("span.ProfileTweet-action--favorite span.ProfileTweet-actionCount").attr("data-tweet-stat-count").replaceAll(",", ""));
long dateMs = Long.valueOf(tweet.select("small.time span.js-short-timestamp").attr("data-time-ms"));
String id = tweet.attr("data-tweet-id");
if (mostRecentTweetId == null) mostRecentTweetId = id;

String permalink = tweet.attr("data-permalink-path");

String geo = "";
Expand Down Expand Up @@ -120,6 +145,7 @@ public static List<Tweet> getTweets(TwitterCriteria criteria) {
if (criteria.getMaxTweets() > 0 && results.size() >= criteria.getMaxTweets()) {
break outerLace;
}
refreshMaxId = id;
}
}
} catch (Exception e) {
Expand Down