Skip to content
This repository has been archived by the owner on Jul 21, 2024. It is now read-only.

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Langsdorf committed Jun 2, 2018
0 parents commit 0ffc626
Show file tree
Hide file tree
Showing 40 changed files with 6,030 additions and 0 deletions.
27 changes: 27 additions & 0 deletions .classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" path="src/examples/java"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/target/
/.settings/
.classpath
.settings
23 changes: 23 additions & 0 deletions .project
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>blackbird</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
</natures>
</projectDescription>
4 changes: 4 additions & 0 deletions .settings/org.eclipse.core.resources.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
eclipse.preferences.version=1
encoding//src/main/java=UTF-8
encoding//src/test/java=UTF-8
encoding/<project>=UTF-8
5 changes: 5 additions & 0 deletions .settings/org.eclipse.jdt.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5
org.eclipse.jdt.core.compiler.compliance=1.5
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.source=1.5
4 changes: 4 additions & 0 deletions .settings/org.eclipse.m2e.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
activeProfiles=
eclipse.preferences.version=1
resolveWorkspaceProjects=true
version=1
65 changes: 65 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.github.langsdorf</groupId>
<artifactId>blackbird</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<description>A simple Twitter API for Java</description>
<name>BlackBird</name>
<url>https://github.com/Langsdorf/BlackBird</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.6</maven.compiler.source>
<maven.compiler.target>1.6</maven.compiler.target>
</properties>

<licenses>
<license>
<name>MIT</name>
<url>https://github.com/Langsdorf/BlackBird/LICENCE</url>
</license>

</licenses>

<developers>
<developer>
<id>Langsdorf</id>
<name>Thiago Langsdorf</name>
<email>langsdorf@outlook.com.br</email>
<url>https://github.com/Langsdorf/</url>
<roles>
<role>owner</role>
<role>developer</role>
</roles>
<timezone>-3</timezone>
</developer>
</developers>

<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.22</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.github.scribejava</groupId>
<artifactId>scribejava-apis</artifactId>
<version>5.4.0</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.5</version>
</dependency>
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.github.langsdorf.blackbird;

import com.github.langsdorf.blackbird.api.DirectMessageAPI;
import com.github.langsdorf.blackbird.api.UserAPI;
import com.github.langsdorf.blackbird.directmessage.DirectMessage;
import com.github.langsdorf.blackbird.exception.TwitterException;

public class DirectMessageExample {

public static void main(String[] args) throws TwitterException, InterruptedException {
BlackBird blackBird = new BlackBird(args[0], args[1], args[2], args[3]);
sendDM(blackBird, "thlangsdorf", "Using BlackBird! :D"); // sending a dm to "thlangsdorf"
}

public static void sendDM(BlackBird session, String username, String text) throws TwitterException {
DirectMessageAPI directmessage_api = session.getDirectMessageAPI();
UserAPI user_api = session.getUserAPI();

long userID = user_api.getUser(username).getUserId(); // Get user id by username
DirectMessage dm_sent = directmessage_api.sendDirectMessage(userID, text);

System.out.println("DM ID: " + dm_sent.getId());
System.out.println("DM Sender: " + dm_sent.getSenderId());
System.out.println("DM Target: " + dm_sent.getRecipientId());
System.out.println("DM Date: " + dm_sent.getCreatedAt().toString());
System.out.println("DM Text: " + dm_sent.getMessage());
}

}
32 changes: 32 additions & 0 deletions src/examples/java/com/github/langsdorf/blackbird/TweetExample.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.github.langsdorf.blackbird;

import com.github.langsdorf.blackbird.api.TweetAPI;
import com.github.langsdorf.blackbird.exception.TwitterException;
import com.github.langsdorf.blackbird.tweet.Tweet;

public class TweetExample {

public static void main(String[] args) throws TwitterException {
BlackBird blackBird = new BlackBird(args[0], args[1], args[2], args[3]);
long tw_id = createTweet(blackBird, "Check out my github profile: https://github.com/Langsdorf");
deleteTweet(blackBird, tw_id);
}

public static long createTweet(BlackBird session, String text) throws TwitterException {
TweetAPI tweet_api = session.getTweetAPI();
Tweet tweet = tweet_api.createTweet(text);
System.out.println("Tweet text: " + tweet.getText());
System.out.println("Tweet created at: " + tweet.getCreatedAt());
System.out.println("Tweet ID: " + tweet.getTweetId());
return tweet.getTweetId();
}

public static void deleteTweet(BlackBird session, long id) throws TwitterException {
TweetAPI tweet_api = session.getTweetAPI();
Tweet tweet = tweet_api.deleteTweet(id);
System.out.println("Deleted tweet text: " + tweet.getText());
System.out.println("Deleted tweet created at: " + tweet.getCreatedAt());
System.out.println("Deleted tweet ID: " + tweet.getTweetId());
}

}
67 changes: 67 additions & 0 deletions src/examples/java/com/github/langsdorf/blackbird/UserExample.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.github.langsdorf.blackbird;

import com.github.langsdorf.blackbird.api.UserAPI;
import com.github.langsdorf.blackbird.exception.TwitterException;
import com.github.langsdorf.blackbird.user.User;

public class UserExample {

public static void main(String[] args) throws TwitterException {
BlackBird blackBird = new BlackBird(args[0], args[1], args[2], args[3]);

blockUser(blackBird, "finkd"); //blocking the user called "finkd"
unblockUser(blackBird, "finkd"); //unblocking the user called "finkd"
muteUser(blackBird, "finkd"); //muting the user called "finkd"
unmuteUser(blackBird, "finkd"); //unmuting the user called "finkd"
followUser(blackBird, "finkd"); //follow the user called "finkd"
unfollowUser(blackBird, "finkd"); //unfollow the user called "finkd"
getFollowersName(blackBird, "thlangsdorf"); //get thlangsdorf's followers name
}

public static void blockUser(BlackBird session, String target_name) throws TwitterException {
UserAPI user_api = session.getUserAPI();
User user = user_api.blockUser(target_name);
System.out.println("Blocked user name: " + user.getName());
}

public static void unblockUser(BlackBird session, String target_name) throws TwitterException {
UserAPI user_api = session.getUserAPI();
User user = user_api.unblockUser(target_name);
System.out.println("Unblocked user name: " + user.getName());
}

public static void muteUser(BlackBird session, String target_name) throws TwitterException {
UserAPI user_api = session.getUserAPI();
User user = user_api.muteUser(target_name);
System.out.println("Muting user: " + user.getName());
}

public static void unmuteUser(BlackBird session, String target_name) throws TwitterException {
UserAPI user_api = session.getUserAPI();
User user = user_api.unmuteUser(target_name);
System.out.println("Unmuting user: " + user.getName());
}


public static void followUser(BlackBird session, String target_name) throws TwitterException {
UserAPI user_api = session.getUserAPI();
User user = user_api.followUser(target_name);
System.out.println("Following " + user.getName());
}

public static void unfollowUser(BlackBird session, String target_name) throws TwitterException {
UserAPI user_api = session.getUserAPI();
User user = user_api.unfollowUser(target_name);
System.out.println("Unfollowing " + user.getName());
}

public static void getFollowersName(BlackBird session, String target_name) throws TwitterException {
UserAPI user_api = session.getUserAPI();
System.out.println(target_name + "'s followers:");
for (User user : user_api.getFollowersList(target_name, -1, "")) {
String name = user.getName();
System.out.println(name);
}
}

}
107 changes: 107 additions & 0 deletions src/main/java/com/github/langsdorf/blackbird/BlackBird.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package com.github.langsdorf.blackbird;

import java.io.IOException;
import java.util.concurrent.ExecutionException;

import com.github.langsdorf.blackbird.api.DirectMessageAPI;
import com.github.langsdorf.blackbird.api.TweetAPI;
import com.github.langsdorf.blackbird.api.UserAPI;
import com.github.langsdorf.blackbird.exception.TwitterException;
import com.github.langsdorf.blackbird.factory.DirectMessageFactory;
import com.github.langsdorf.blackbird.factory.TweetFactory;
import com.github.langsdorf.blackbird.factory.UserFactory;
import com.github.langsdorf.blackbird.http.URLList;
import com.github.scribejava.apis.TwitterApi;
import com.github.scribejava.core.builder.ServiceBuilder;
import com.github.scribejava.core.model.OAuth1AccessToken;
import com.github.scribejava.core.model.OAuth1RequestToken;
import com.github.scribejava.core.model.OAuthRequest;
import com.github.scribejava.core.model.Response;
import com.github.scribejava.core.model.Verb;
import com.github.scribejava.core.oauth.OAuth10aService;

import lombok.AccessLevel;
import lombok.Getter;
import lombok.Setter;
import lombok.SneakyThrows;

@Setter(AccessLevel.PACKAGE)
@Getter
public class BlackBird implements URLList {

private String consumerKey;
private String consumerSecret;

private String accessToken;
private String accessTokenSecret;

private OAuth10aService oauthService;
private OAuth1AccessToken oauthAccessToken;
private OAuth1RequestToken oauthRequestToken;

private DirectMessageFactory directMessageFactory;
private DirectMessageAPI directMessageAPI;

private UserFactory userFactory;
private UserAPI userAPI;

private TweetFactory tweetFactory;
private TweetAPI tweetAPI;

public BlackBird(String consumerKey, String consumerSecret) {
setConsumerKey(consumerKey);
setConsumerSecret(consumerSecret);
}

public BlackBird(String consumerKey, String consumerSecret, String accessToken, String accessTokenSecret) {
setConsumerKey(consumerKey);
setConsumerSecret(consumerSecret);
setAccessToken(accessToken);
setAccessTokenSecret(accessTokenSecret);
auth();
}

@SneakyThrows(Exception.class)
private void auth() {
OAuth10aService service = new ServiceBuilder(getConsumerKey()).apiSecret(getConsumerSecret())
.build(TwitterApi.instance());
OAuth1RequestToken requestToken = service.getRequestToken();
OAuth1AccessToken oauthAccessToken = new OAuth1AccessToken(getAccessToken(), getAccessTokenSecret());

setOauthAccessToken(oauthAccessToken);
setOauthService(service);
setOauthRequestToken(requestToken);
checkCredentials();
}

@SneakyThrows(Exception.class)
public void checkCredentials() {
String URL = PROTECTED_RESOURCE_URL;
OAuthRequest request = new OAuthRequest(Verb.GET, URL);
getOauthService().signRequest(getOauthAccessToken(), request);
Response response = getOauthService().execute(request);
if (response.getCode() >= 200 && response.getCode() < 300) {
init();
} else {
throw new TwitterException("Missing or incorrect authentication credentials.");
}
}

public BlackBird pinAuth(String pin) throws IOException, InterruptedException, ExecutionException {
OAuth1AccessToken accessToken = oauthService.getAccessToken(oauthRequestToken, pin);
setOauthAccessToken(accessToken);
checkCredentials();
return this;
}

void init() {
setDirectMessageFactory(new DirectMessageFactory(this));
setUserFactory(new UserFactory(this));
setTweetFactory(new TweetFactory(this));

setDirectMessageAPI(new DirectMessageAPI(this));
setUserAPI(new UserAPI(this));
setTweetAPI(new TweetAPI(this));
}

}
Loading

0 comments on commit 0ffc626

Please sign in to comment.