Commit 30034d9e authored by Benjamin Beeker's avatar Benjamin Beeker

setting up build script

parents
Pipeline #10823572 failed
# Created by .ignore support plugin (hsz.mobi)
**.iml
/.idea/*
**.log
**.log.zip
**/target
stages:
- build
- verify
- install
before_script:
- echo '<settings>
<servers>
<server>
<id>Comhix-Releases</id>
<username>'$NEXUS_USER'</username>
<password>'$NEXUS_PASSWORD'</password>
</server>
<server>
<id>Comhix-Snapshots</id>
<username>'$NEXUS_USER'</username>
<password>'$NEXUS_PASSWORD'</password>
</server>
</servers>
</settings>' > $HOME/.m2/settings.xml
cache:
key: "$CI_BUILD_REF_NAME"
paths:
- target/
- .m2/
variables:
MAVEN_OPTS: "-Dmaven.repo.local=.m2/repository"
build-job:
image: maven:alpine
stage: build
script:
- mvn clean compile test $MAVEN_CLI_OPTS
verify-dependencies:
image: maven:alpine
stage: install
only:
- release
script:
- mvn verify -Prelease $MAVEN_CLI_OPTS
install-job:
image: maven:3
stage: install
only:
- master
- release
script:
- mvn install generate-sources generate-test-sources source:jar javadoc:jar deploy $MAVEN_CLI_OPTS
artifacts:
name: "${CI_BUILD_REF_NAME}_${CI_BUILD_REF}"
paths:
target/*.jar
\ No newline at end of file
This diff is collapsed.
<?xml version="1.0" encoding="UTF-8"?>
<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>de.comhix.twitch</groupId>
<version>0.0.1-SNAPSHOT</version>
<artifactId>twitch-api</artifactId>
<name>Twitch API</name>
<description>An API to access Twitch</description>
<licenses>
<license>
<name>CC-BY-NC-SA</name>
<url>https://creativecommons.org/licenses/by-nc-sa/3.0/de/</url>
</license>
</licenses>
<developers>
<developer>
<name>Benjamin Beeker</name>
<email>Benjamin.Beeker@comhix.de</email>
<timezone>Europe/Berlin</timezone>
<organization>ComHix</organization>
</developer>
</developers>
<scm>
<connection>scm:git@gitlab.com:NoZomIBK/twitch-api.git</connection>
<developerConnection>scm:git@gitlab.com:NoZomIBK/twitch-api.git</developerConnection>
<url>https://gitlab.com/NoZomIBK/twitch-api</url>
</scm>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<repository.base>http://nexus.comhix.de/repository/maven-</repository.base>
</properties>
<dependencies>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.4.2</version>
</dependency>
<dependency>
<groupId>org.glassfish.grizzly</groupId>
<artifactId>grizzly-http-server</artifactId>
<version>2.3.28</version>
</dependency>
<dependency>
<groupId>pircbot</groupId>
<artifactId>pircbot</artifactId>
<version>1.5.0</version>
</dependency>
<dependency>
<groupId>de.comhix.commons</groupId>
<artifactId>logging</artifactId>
<version>1.2.0</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>20.0</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>
<dependency>
<groupId>io.reactivex.rxjava2</groupId>
<artifactId>rxjava</artifactId>
<version>RELEASE</version>
</dependency>
<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
<version>4.1.0</version>
<exclusions>
<exclusion>
<artifactId>guava</artifactId>
<groupId>com.google.guava</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core-java8</artifactId>
<version>1.0.0m1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>2.0.2-beta</version>
<scope>test</scope>
</dependency>
</dependencies>
<repositories>
<repository>
<id>Comhix-Releases</id>
<releases>
<enabled>true</enabled>
<checksumPolicy>ignore</checksumPolicy>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
<name>ComHix releases</name>
<url>${repository.base}releases</url>
</repository>
<repository>
<id>Comhix-Snapshots</id>
<name>ComHix snapshots</name>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>daily</updatePolicy>
<checksumPolicy>ignore</checksumPolicy>
</snapshots>
<url>${repository.base}snapshots</url>
</repository>
</repositories>
<distributionManagement>
<repository>
<id>Comhix-Releases</id>
<url>${repository.base}releases</url>
</repository>
<snapshotRepository>
<id>Comhix-Snapshots</id>
<url>${repository.base}snapshots</url>
</snapshotRepository>
</distributionManagement>
<profiles>
<profile>
<id>release</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>1.4</version>
<executions>
<execution>
<id>enforce-versions</id>
<goals>
<goal>enforce</goal>
</goals>
<configuration>
<rules>
<dependencyConvergence/>
<requireReleaseDeps/>
<requireReleaseVersion/>
</rules>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>1.4</version>
<executions>
<execution>
<id>enforce-versions</id>
<goals>
<goal>enforce</goal>
</goals>
<configuration>
<rules>
<dependencyConvergence/>
</rules>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
<version>3.4</version>
<dependencies>
<dependency><!-- add support for ssh/scp -->
<groupId>org.apache.maven.wagon</groupId>
<artifactId>wagon-ftp</artifactId>
<version>2.9</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<version>2.5.3</version>
<configuration>
<releaseProfiles>release</releaseProfiles>
<tagNameFormat>v@{project.version}</tagNameFormat>
<autoVersionSubmodules>true</autoVersionSubmodules>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
package de.comhix.twitch.api;
import de.comhix.twitch.api.data.ChatJoin;
import de.comhix.twitch.api.data.ChatMessage;
import de.comhix.twitch.api.data.ChatPart;
import de.comhix.twitch.api.irc.ChatConnector;
import io.reactivex.Completable;
import io.reactivex.Observable;
import io.reactivex.schedulers.Schedulers;
import org.jibble.pircbot.PircBot;
public class ChatProvider {
public static final String TWITCH_IRC_SERVER = "irc.chat.twitch.tv";
private String oAuthToken;
private String name;
private final String chatChannel;
private ChatConnector bot;
private boolean closed = false;
public ChatProvider(String oAuthToken, String name, String chatChannel) {
this.oAuthToken = oAuthToken;
this.name = name;
this.chatChannel = chatChannel;
}
private Observable<ChatConnector> getChatConnector() {
if (bot != null) {
return Observable.just(bot).observeOn(Schedulers.io());
}
return Observable.fromCallable(() -> {
bot = new ChatConnector(name);
bot.connect(TWITCH_IRC_SERVER, 6667, "oauth:" + oAuthToken);
bot.joinChannel(chatChannel);
return bot;
}).observeOn(Schedulers.io());
}
public Observable<ChatJoin> onJoin() {
checkClosed();
return getChatConnector().flatMap(ChatConnector::onJoin);
}
public Observable<ChatPart> onPart() {
checkClosed();
return getChatConnector().flatMap(ChatConnector::onPart);
}
public Observable<ChatMessage> onMessage() {
checkClosed();
return getChatConnector().flatMap(ChatConnector::onMessage);
}
public Completable sendMessage(String message) {
checkClosed();
return getChatConnector()
.firstOrError()
.doOnSuccess(chatConnector -> chatConnector.sendMessage(chatChannel, message))
.toCompletable();
}
private void checkClosed(){
if (closed) {
throw new IllegalStateException("this TwitchApi instance is closed");
}
}
void close() {
this.closed=true;
getChatConnector().subscribe(PircBot::disconnect);
}
}
package de.comhix.twitch.api;
import com.google.common.base.Joiner;
import com.google.common.base.Preconditions;
import com.google.gson.Gson;
import de.comhix.twitch.api.data.DetailedUser;
import de.comhix.twitch.api.data.User;
import de.comhix.twitch.api.data.UsersResponse;
import de.comhix.twitch.api.oauth.ClientInformation;
import de.comhix.twitch.api.oauth.OAuthResponse;
import de.comhix.twitch.api.oauth.SelfhostedTwitchAuthenticator;
import de.comhix.twitch.api.oauth.TwitchAuthenticator;
import io.reactivex.Observable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;
/**
* base class
*/
public class TwitchApi {
private final ClientInformation clientInformation;
private final String oauthToken;
private final String chatChannel;
private ChatProvider chatProvider;
private OkHttpClient client = new OkHttpClient.Builder().build();
private boolean closed=false;
public TwitchApi(ClientInformation clientInformation, String oauthToken, String chatChannel) {
this.clientInformation = Preconditions.checkNotNull(clientInformation);
this.oauthToken = Preconditions.checkNotNull(oauthToken);
this.chatChannel = Preconditions.checkNotNull(chatChannel);
}
public static TwitchAuthenticator.SecondStep startOAuth(ClientInformation clientInformation, String callbackUrl) {
return new TwitchAuthenticator(clientInformation).doAuth(callbackUrl);
}
public static io.reactivex.Observable<OAuthResponse> doListeningOAuth(ClientInformation clientInformation, String landingHTML, int port) {
return new SelfhostedTwitchAuthenticator(clientInformation, landingHTML, port).doOAuth();
}
public static Observable<OAuthResponse> doListeningOAuth(ClientInformation clientInformation) {
return doListeningOAuth(clientInformation, SelfhostedTwitchAuthenticator.RESPONSE_HTML, SelfhostedTwitchAuthenticator.DEFAULT_PORT);
}
public void close(){
closed = true;
chatProvider.close();
}
private void checkClosed(){
if (closed) {
throw new IllegalStateException("this TwitchApi instance is closed");
}
}
public Observable<ChatProvider> getChatProvider() {
checkClosed();
if (chatProvider == null) {
return getOwnInfo().map(ownInfo -> {
chatProvider = new ChatProvider(oauthToken, ownInfo.getName(), chatChannel);
return chatProvider;
});
}
return Observable.just(chatProvider);
}
public Observable<DetailedUser> getOwnInfo() {
checkClosed();
return Observable.fromCallable(() -> {
Request.Builder request = new Request.Builder().url("https://api.twitch.tv/kraken/user")
.addHeader("Client-ID", clientInformation.getClientId())
.addHeader("Authorization", "OAuth " + oauthToken);
InputStream body = client.newCall(request.build()).execute().body().byteStream();
return new Gson().fromJson(new InputStreamReader(body), DetailedUser.class);
}).observeOn(Schedulers.io());
}
public Observable<List<User>> getUsers(List<String> loginNames) {
checkClosed();
return Observable.fromCallable(() -> {
Request.Builder request = new Request.Builder().url("https://api.twitch.tv/kraken/users?login=" + Joiner.on(",").join(loginNames))
.addHeader("Client-ID", clientInformation.getClientId());
InputStream body = client.newCall(request.build()).execute().body().byteStream();
return new Gson().fromJson(new InputStreamReader(body), UsersResponse.class).users;
}).observeOn(Schedulers.io());
}
}
\ No newline at end of file
package de.comhix.twitch.api.data;
/**
* @author Benjamin Beeker
*/
public class ChatJoin {
private final String user;
public ChatJoin(String user) {
this.user = user;
}
public String getUser() {
return user;
}
}
package de.comhix.twitch.api.data;
/**
* @author Benjamin Beeker
*/
public class ChatMessage {
private final String sender;
private final String message;
public ChatMessage(String sender, String message) {
this.sender = sender;
this.message = message;
}
public String getMessage() {
return message;
}
public String getSender() {
return sender;
}
}
package de.comhix.twitch.api.data;
/**
* @author Benjamin Beeker
*/
public class ChatPart {
private final String user;
public ChatPart(String user) {
this.user = user;
}
public String getUser() {
return user;
}
}
package de.comhix.twitch.api.data;
import com.google.gson.annotations.SerializedName;
/**
* @author Benjamin Beeker
*/
public class DetailedUser extends User {
private String email;
@SerializedName("email_verified")
private boolean emailVerified;
private boolean partnered;
private Notifications notifications;
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public boolean isEmailVerified() {
return emailVerified;
}
public void setEmailVerified(boolean emailVerified) {
this.emailVerified = emailVerified;
}
public boolean isPartnered() {
return partnered;
}
public void setPartnered(boolean partnered) {
this.partnered = partnered;
}
public Notifications getNotifications() {
return notifications;
}
public void setNotifications(Notifications notifications) {
this.notifications = notifications;
}
public static class Notifications {
private boolean email;
private boolean push;
public boolean isPush() {
return push;
}
public void setPush(boolean push) {
this.push = push;
}
public boolean isEmail() {
return email;
}
public void setEmail(boolean email) {
this.email = email;
}
}
}
package de.comhix.twitch.api.data;
import com.google.common.base.MoreObjects;
import com.google.gson.annotations.SerializedName;
import java.util.Date;
/**
* @author Benjamin Beeker
*/
public class User {
private String type;
private String name;
@SerializedName("created_at")
private Date createdAt;
@SerializedName("updated_at")
private Date updatedAt;
private String logo;
@SerializedName("_id")
private int id;
@SerializedName("display_name")
private String displayName;
private String bio;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getCreatedAt() {
return createdAt;
}
public void setCreatedAt(Date createdAt) {
this.createdAt = createdAt;
}
public Date getUpdatedAt() {
return updatedAt;
}
public void setUpdatedAt(Date updatedAt) {
this.updatedAt = updatedAt;
}
public String getLogo() {
return logo;
}
public void setLogo(String logo) {
this.logo = logo;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getDisplayName() {
return displayName;
}
public void setDisplayName(String displayName) {
this.displayName = displayName;
}
public String getBio() {
return bio;
}
public void setBio(String bio) {
this.bio = bio;
}
@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("type", type)
.add("name", name)
.add("createdAt", createdAt)
.add("updatedAt", updatedAt)
.add("logo", logo)
.add("id", id)
.add("displayName", displayName)
.add("bio", bio)
.toString();
}
}
package de.comhix.twitch.api.data;
import com.google.gson.annotations.SerializedName;
import java.util.List;
/**
* @author Benjamin Beeker
*/
public class UsersResponse {
@SerializedName("_total")
public int total;
public List<User> users;
}
package de.comhix.twitch.api.irc;
import de.comhix.twitch.api.data.ChatJoin;
import de.comhix.twitch.api.data.ChatMessage;
import de.comhix.twitch.api.data.ChatPart;
import io.reactivex.Observable;
import io.reactivex.subjects.PublishSubject;
import org.apache.commons.lang3.tuple.Pair;
import org.jibble.pircbot.PircBot;
import org.jibble.pircbot.User;