Java Reference
In-Depth Information
stats[0] = statsTable.select(
"a[data-element-term=tweet_stats]").text();
stats[1] = statsTable.select(
"a[data-element-term=following_stats]").text();
stats[2] = statsTable.select(
"a[data-element-term=follower_stats]").text();
} catch (IndexOutOfBoundsException e) {
// table.js-mini-profile-stats could not be found
// Perhaps this user has enabled the new Twitter profile?
Element statsTable = result.select("ul.ProfileNav-list").get(0);
stats[0] = statsTable.select(
"a[data-nav=tweets] span.ProfileNav-value").text();
stats[1] = statsTable.select(
"a[data-nav=following] span.ProfileNav-value").text();
stats[2] = statsTable.select(
"a[data-nav=followers] span.ProfileNav-value").text();
}
return stats;
}
public static String getUsername() throws IOException {
Connection connection = Jsoup.connect(" https://twitter.com" )
.cookies(COOKIES);
Document result = connection.get();
COOKIES.putAll(connection.response().cookies());
return result.select("div[role=navigation] li.profile a.js-
nav").attr("href").replace("/", "");
}
public static boolean login(String username, String password)
throws IOException {
COOKIES.clear(); // Clear all cookies
// Request Twitter homepage to set first cookies and get token
String authenticityToken = getAuthenticityToken();
// Then login using a POST request
Connection connection = Jsoup.connect(" https://twitter.com/sessions")
.data("session[username_or_email]", username)
.data("session[password]", password)
.data("remember_me", "1")
.data("return_to_ssl", "true")
.data("redirect_after_login", "/")
.data("remember_me", "1")
.data("authenticity_token", authenticityToken)
.cookies(COOKIES);
Document result = connection.post();
COOKIES.putAll(connection.response().cookies());
if (result.html().contains("action=\" https://twitter.com/sessions\"")) {
// The sign-in form is still present in the result
// The login was probably incorrect
return false;
}
return true;
}
private static String getAuthenticityToken() throws IOException {
Search WWH ::




Custom Search