├── .github └── workflows │ └── gradle.yml ├── .gitignore ├── LICENSE ├── README.md ├── build.gradle ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── settings.gradle └── src └── main ├── java ├── controller │ ├── Controller.java │ └── Main.java ├── service │ └── TwitchUtil.java ├── utils │ ├── Configs.java │ └── HttpClient.java └── viewbot │ └── ViewBot.java └── resources ├── foo.fxml ├── sample.fxml └── styles.css /.github/workflows/gradle.yml: -------------------------------------------------------------------------------- 1 | # This workflow will build a Java project with Gradle 2 | # For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-gradle 3 | 4 | name: Java 5 | 6 | on: 7 | push: 8 | branches: [ master ] 9 | pull_request: 10 | branches: [ master ] 11 | 12 | jobs: 13 | build: 14 | 15 | runs-on: ubuntu-latest 16 | 17 | steps: 18 | - uses: actions/checkout@v2 19 | - name: Set up JDK 11 20 | uses: actions/setup-java@v1 21 | with: 22 | java-version: 11 23 | - name: Grant execute permission for gradlew 24 | run: chmod +x gradlew 25 | - name: Build with Gradle 26 | run: ./gradlew build 27 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Project exclude paths 2 | /.gradle/ 3 | /build/ 4 | /build/classes/java/main/ 5 | 6 | # IntelliJ 7 | .idea 8 | 9 | # OSx 10 | .DS_Store 11 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Vadim Krivitskii 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ![Build](https://github.com/OGSegu/Twitch-View-Bot/workflows/Java/badge.svg) 2 | 3 | 4 | # OUTDATED 5 | ### This project won't be supported. 6 | 7 | # Twitch View Bot 8 | 9 | __disclaimer:__ This project was created only for educational purpose and really fast. It probably has a lot of bugs and bad architecture. 10 | However, the main goal is to show the way of sending requests which Twitch will count as a viewer. 11 | 12 | ### Description 13 | First open-source really working view bot for Twitch. My project don't use Selenium, it works only on HTTP requests. 14 | 15 | ### Installation 16 | ``git clone https://github.com/OGSegu/Twitch_ViewBot.git`` 17 | ``./gradlew build`` 18 | 19 | 20 | __Contributions are welcomed.__ 21 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id 'java' 3 | id 'org.openjfx.javafxplugin' version '0.0.9' 4 | } 5 | 6 | jar { 7 | manifest { 8 | attributes 'Main-Class': 'controller.Main' 9 | } 10 | from { 11 | configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) } 12 | } 13 | } 14 | 15 | 16 | group 'org.example' 17 | version '1.0-SNAPSHOT' 18 | 19 | repositories { 20 | mavenCentral() 21 | } 22 | 23 | dependencies { 24 | testCompile group: 'junit', name: 'junit', version: '4.12' 25 | implementation group: 'org.json', name: 'json', version: '20201115' 26 | implementation group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.5.13' 27 | } 28 | 29 | javafx { 30 | version = "15.0.1" 31 | modules = [ 'javafx.controls', 'javafx.fxml' ] 32 | } -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OGSegu/Twitch-View-Bot/8a333b9e3b5aacf65c763fdea002bb4e3eb4fe8c/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-6.3-bin.zip 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | # 4 | # Copyright 2015 the original author or authors. 5 | # 6 | # Licensed under the Apache License, Version 2.0 (the "License"); 7 | # you may not use this file except in compliance with the License. 8 | # You may obtain a copy of the License at 9 | # 10 | # https://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | # See the License for the specific language governing permissions and 16 | # limitations under the License. 17 | # 18 | 19 | ############################################################################## 20 | ## 21 | ## Gradle start up script for UN*X 22 | ## 23 | ############################################################################## 24 | 25 | # Attempt to set APP_HOME 26 | # Resolve links: $0 may be a link 27 | PRG="$0" 28 | # Need this for relative symlinks. 29 | while [ -h "$PRG" ] ; do 30 | ls=`ls -ld "$PRG"` 31 | link=`expr "$ls" : '.*-> \(.*\)$'` 32 | if expr "$link" : '/.*' > /dev/null; then 33 | PRG="$link" 34 | else 35 | PRG=`dirname "$PRG"`"/$link" 36 | fi 37 | done 38 | SAVED="`pwd`" 39 | cd "`dirname \"$PRG\"`/" >/dev/null 40 | APP_HOME="`pwd -P`" 41 | cd "$SAVED" >/dev/null 42 | 43 | APP_NAME="Gradle" 44 | APP_BASE_NAME=`basename "$0"` 45 | 46 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 47 | DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' 48 | 49 | # Use the maximum available, or set MAX_FD != -1 to use that value. 50 | MAX_FD="maximum" 51 | 52 | warn () { 53 | echo "$*" 54 | } 55 | 56 | die () { 57 | echo 58 | echo "$*" 59 | echo 60 | exit 1 61 | } 62 | 63 | # OS specific support (must be 'true' or 'false'). 64 | cygwin=false 65 | msys=false 66 | darwin=false 67 | nonstop=false 68 | case "`uname`" in 69 | CYGWIN* ) 70 | cygwin=true 71 | ;; 72 | Darwin* ) 73 | darwin=true 74 | ;; 75 | MINGW* ) 76 | msys=true 77 | ;; 78 | NONSTOP* ) 79 | nonstop=true 80 | ;; 81 | esac 82 | 83 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 84 | 85 | 86 | # Determine the Java command to use to start the JVM. 87 | if [ -n "$JAVA_HOME" ] ; then 88 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 89 | # IBM's JDK on AIX uses strange locations for the executables 90 | JAVACMD="$JAVA_HOME/jre/sh/java" 91 | else 92 | JAVACMD="$JAVA_HOME/bin/java" 93 | fi 94 | if [ ! -x "$JAVACMD" ] ; then 95 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 96 | 97 | Please set the JAVA_HOME variable in your environment to match the 98 | location of your Java installation." 99 | fi 100 | else 101 | JAVACMD="java" 102 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 103 | 104 | Please set the JAVA_HOME variable in your environment to match the 105 | location of your Java installation." 106 | fi 107 | 108 | # Increase the maximum file descriptors if we can. 109 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 110 | MAX_FD_LIMIT=`ulimit -H -n` 111 | if [ $? -eq 0 ] ; then 112 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 113 | MAX_FD="$MAX_FD_LIMIT" 114 | fi 115 | ulimit -n $MAX_FD 116 | if [ $? -ne 0 ] ; then 117 | warn "Could not set maximum file descriptor limit: $MAX_FD" 118 | fi 119 | else 120 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 121 | fi 122 | fi 123 | 124 | # For Darwin, add options to specify how the application appears in the dock 125 | if $darwin; then 126 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 127 | fi 128 | 129 | # For Cygwin or MSYS, switch paths to Windows format before running java 130 | if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then 131 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 132 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 133 | 134 | JAVACMD=`cygpath --unix "$JAVACMD"` 135 | 136 | # We build the pattern for arguments to be converted via cygpath 137 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 138 | SEP="" 139 | for dir in $ROOTDIRSRAW ; do 140 | ROOTDIRS="$ROOTDIRS$SEP$dir" 141 | SEP="|" 142 | done 143 | OURCYGPATTERN="(^($ROOTDIRS))" 144 | # Add a user-defined pattern to the cygpath arguments 145 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 146 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 147 | fi 148 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 149 | i=0 150 | for arg in "$@" ; do 151 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 152 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 153 | 154 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 155 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 156 | else 157 | eval `echo args$i`="\"$arg\"" 158 | fi 159 | i=`expr $i + 1` 160 | done 161 | case $i in 162 | 0) set -- ;; 163 | 1) set -- "$args0" ;; 164 | 2) set -- "$args0" "$args1" ;; 165 | 3) set -- "$args0" "$args1" "$args2" ;; 166 | 4) set -- "$args0" "$args1" "$args2" "$args3" ;; 167 | 5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 168 | 6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 169 | 7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 170 | 8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 171 | 9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 172 | esac 173 | fi 174 | 175 | # Escape application args 176 | save () { 177 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done 178 | echo " " 179 | } 180 | APP_ARGS=`save "$@"` 181 | 182 | # Collect all arguments for the java command, following the shell quoting and substitution rules 183 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" 184 | 185 | exec "$JAVACMD" "$@" 186 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @rem 2 | @rem Copyright 2015 the original author or authors. 3 | @rem 4 | @rem Licensed under the Apache License, Version 2.0 (the "License"); 5 | @rem you may not use this file except in compliance with the License. 6 | @rem You may obtain a copy of the License at 7 | @rem 8 | @rem https://www.apache.org/licenses/LICENSE-2.0 9 | @rem 10 | @rem Unless required by applicable law or agreed to in writing, software 11 | @rem distributed under the License is distributed on an "AS IS" BASIS, 12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | @rem See the License for the specific language governing permissions and 14 | @rem limitations under the License. 15 | @rem 16 | 17 | @if "%DEBUG%" == "" @echo off 18 | @rem ########################################################################## 19 | @rem 20 | @rem Gradle startup script for Windows 21 | @rem 22 | @rem ########################################################################## 23 | 24 | @rem Set local scope for the variables with windows NT shell 25 | if "%OS%"=="Windows_NT" setlocal 26 | 27 | set DIRNAME=%~dp0 28 | if "%DIRNAME%" == "" set DIRNAME=. 29 | set APP_BASE_NAME=%~n0 30 | set APP_HOME=%DIRNAME% 31 | 32 | @rem Resolve any "." and ".." in APP_HOME to make it shorter. 33 | for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi 34 | 35 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 36 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" 37 | 38 | @rem Find java.exe 39 | if defined JAVA_HOME goto findJavaFromJavaHome 40 | 41 | set JAVA_EXE=java.exe 42 | %JAVA_EXE% -version >NUL 2>&1 43 | if "%ERRORLEVEL%" == "0" goto init 44 | 45 | echo. 46 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 47 | echo. 48 | echo Please set the JAVA_HOME variable in your environment to match the 49 | echo location of your Java installation. 50 | 51 | goto fail 52 | 53 | :findJavaFromJavaHome 54 | set JAVA_HOME=%JAVA_HOME:"=% 55 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 56 | 57 | if exist "%JAVA_EXE%" goto init 58 | 59 | echo. 60 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 61 | echo. 62 | echo Please set the JAVA_HOME variable in your environment to match the 63 | echo location of your Java installation. 64 | 65 | goto fail 66 | 67 | :init 68 | @rem Get command-line arguments, handling Windows variants 69 | 70 | if not "%OS%" == "Windows_NT" goto win9xME_args 71 | 72 | :win9xME_args 73 | @rem Slurp the command line arguments. 74 | set CMD_LINE_ARGS= 75 | set _SKIP=2 76 | 77 | :win9xME_args_slurp 78 | if "x%~1" == "x" goto execute 79 | 80 | set CMD_LINE_ARGS=%* 81 | 82 | :execute 83 | @rem Setup the command line 84 | 85 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 86 | 87 | 88 | @rem Execute Gradle 89 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 90 | 91 | :end 92 | @rem End local scope for the variables with windows NT shell 93 | if "%ERRORLEVEL%"=="0" goto mainEnd 94 | 95 | :fail 96 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 97 | rem the _cmd.exe /c_ return code! 98 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 99 | exit /b 1 100 | 101 | :mainEnd 102 | if "%OS%"=="Windows_NT" endlocal 103 | 104 | :omega 105 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'Twitch View Bot' 2 | 3 | -------------------------------------------------------------------------------- /src/main/java/controller/Controller.java: -------------------------------------------------------------------------------- 1 | package controller; 2 | 3 | import javafx.fxml.FXML; 4 | import javafx.scene.control.*; 5 | import javafx.stage.FileChooser; 6 | import service.TwitchUtil; 7 | import viewbot.ViewBot; 8 | 9 | import java.io.BufferedReader; 10 | import java.io.File; 11 | import java.io.FileReader; 12 | import java.io.IOException; 13 | import java.util.concurrent.LinkedBlockingQueue; 14 | 15 | public class Controller { 16 | 17 | private final TwitchUtil twitchUtil = new TwitchUtil(); 18 | 19 | private final FileChooser fileChooser = new FileChooser(); 20 | 21 | private final LinkedBlockingQueue proxyQueue = new LinkedBlockingQueue<>(); 22 | 23 | private ViewBot viewBot; 24 | 25 | @FXML 26 | private Button startButton; 27 | 28 | @FXML 29 | private TextField channelNameField; 30 | 31 | @FXML 32 | private TextArea logArea; 33 | 34 | @FXML 35 | private Slider slider; 36 | 37 | @FXML 38 | private Label labelViewers; 39 | 40 | @FXML 41 | private Label viewCount; 42 | 43 | { 44 | fileChooser.setTitle("Choose proxy file"); 45 | fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("Text Files", "*.txt")); 46 | } 47 | 48 | public void initialize() { 49 | slider.valueProperty().addListener(((observable, oldValue, newValue) -> 50 | labelViewers.setText(String.valueOf(newValue.intValue())) 51 | )); 52 | } 53 | 54 | @FXML 55 | public void addCount() { 56 | viewCount.setText(String.valueOf(Integer.parseInt(viewCount.getText()) + 1)); 57 | } 58 | 59 | @FXML 60 | public void resetCount() { 61 | viewCount.setText("0"); 62 | } 63 | 64 | @FXML 65 | public void writeToLog(String text) { 66 | logArea.appendText(text + "\n"); 67 | } 68 | 69 | @FXML 70 | private void start() { 71 | if (!startButton.getText().equals("START")) { 72 | startButton.setText("START"); 73 | } else { 74 | if (proxyQueue.isEmpty()) { 75 | writeToLog("Proxy not loaded"); 76 | return; 77 | } 78 | String target = channelNameField.getText(); 79 | if (!isChannelValid(target)) { 80 | channelNameField.getStyleClass().add("error"); 81 | writeToLog("Wrong channel . Try again"); 82 | return; 83 | } 84 | channelNameField.getStyleClass().remove("error"); 85 | 86 | viewBot = new ViewBot(this, proxyQueue, target, Integer.parseInt(labelViewers.getText())); 87 | 88 | Thread viewBotThread = new Thread(viewBot::start); 89 | viewBotThread.start(); 90 | startButton.setText("STOP"); 91 | } 92 | } 93 | 94 | private boolean isChannelValid(String target) { 95 | if (target.isBlank() || target.isEmpty()) { 96 | return false; 97 | } 98 | boolean isChannelLive; 99 | try { 100 | String channelId = twitchUtil.getChannelId(target); 101 | isChannelLive = twitchUtil.isChannelLive(channelId); 102 | } catch (Exception e) { 103 | return false; 104 | } 105 | return isChannelLive; 106 | } 107 | 108 | @FXML 109 | public void stopViewBot() { 110 | if (viewBot != null) { 111 | resetCount(); 112 | cleanLogArea(); 113 | writeToLog("Stopped"); 114 | } 115 | } 116 | 117 | @FXML 118 | private void loadProxy() { 119 | File file = fileChooser.showOpenDialog(Main.mainStage); 120 | if (file != null) { 121 | try (BufferedReader bufferedReader = new BufferedReader(new FileReader(file))) { 122 | String proxy; 123 | proxyQueue.clear(); 124 | while ((proxy = bufferedReader.readLine()) != null) { 125 | proxyQueue.add(proxy); 126 | } 127 | } catch (IOException e) { 128 | System.out.println("Something went wrong"); 129 | } 130 | writeToLog("Proxy loaded: " + proxyQueue.size()); 131 | } else { 132 | writeToLog("File was not found"); 133 | } 134 | } 135 | 136 | @FXML 137 | public void cleanLogArea() { 138 | logArea.clear(); 139 | } 140 | 141 | public Button getStartButton() { 142 | return startButton; 143 | } 144 | 145 | } 146 | -------------------------------------------------------------------------------- /src/main/java/controller/Main.java: -------------------------------------------------------------------------------- 1 | package controller; 2 | 3 | import javafx.application.Application; 4 | import javafx.fxml.FXMLLoader; 5 | import javafx.scene.Scene; 6 | import javafx.stage.Stage; 7 | 8 | import java.net.URL; 9 | import java.util.Objects; 10 | 11 | public class Main { 12 | 13 | public static Stage mainStage; 14 | 15 | public static void main(String[] args) { 16 | UI.main(args); 17 | } 18 | 19 | public static class UI extends Application { 20 | 21 | public static void main(String[] args) { 22 | Application.launch(); 23 | } 24 | 25 | @Override 26 | public void start(Stage stage) throws Exception { 27 | mainStage = stage; 28 | stage.setTitle("Twitch View Bot 0.1"); 29 | Scene content = FXMLLoader.load(getResource("/sample.fxml", "Scene file cannot be null")); 30 | content.getStylesheets().add(getResource("/styles.css", "Style file cannot be null").toExternalForm()); 31 | stage.setScene(content); 32 | stage.setResizable(false); 33 | stage.show(); 34 | stage.setOnCloseRequest(e -> System.exit(0)); 35 | } 36 | 37 | private URL getResource(String name, String message) { 38 | return Objects.requireNonNull(getClass().getResource(name), message); 39 | } 40 | 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /src/main/java/service/TwitchUtil.java: -------------------------------------------------------------------------------- 1 | package service; 2 | 3 | import org.apache.http.HttpEntity; 4 | import org.apache.http.HttpHeaders; 5 | import org.apache.http.client.methods.CloseableHttpResponse; 6 | import org.apache.http.client.methods.HttpGet; 7 | import org.apache.http.util.EntityUtils; 8 | import org.json.JSONArray; 9 | import org.json.JSONException; 10 | import org.json.JSONObject; 11 | import utils.HttpClient; 12 | 13 | import java.io.IOException; 14 | 15 | public class TwitchUtil { 16 | 17 | public static final String CLIENT_ID = "b31o4btkqth5bzbvr9ub2ovr79umhh"; 18 | private static final String CLIENT_ID_HEADER_NAME = "Client-ID"; 19 | 20 | private static final String CHANNEL_INFO = "https://api.twitch.tv/kraken/users?login="; 21 | private static final String CHANNEL_STREAM = "https://api.twitch.tv/kraken/streams/"; 22 | 23 | private final HttpClient httpClient = new HttpClient(); 24 | 25 | 26 | public HttpGet createHttpGet(String url) { 27 | HttpGet httpGet = new HttpGet(url); 28 | httpGet.addHeader(HttpHeaders.ACCEPT, "application/vnd.twitchtv.v5+json"); 29 | httpGet.addHeader(CLIENT_ID_HEADER_NAME, CLIENT_ID); 30 | return httpGet; 31 | } 32 | 33 | public String getChannelId(String login) throws IOException, JSONException { 34 | HttpGet httpGet = createHttpGet(CHANNEL_INFO + login); 35 | CloseableHttpResponse response = httpClient.execute(httpGet); 36 | HttpEntity entity = response.getEntity(); 37 | String body = EntityUtils.toString(entity); 38 | EntityUtils.consume(entity); 39 | response.close(); 40 | 41 | String channelId; 42 | JSONObject jsonObject = new JSONObject(body); 43 | JSONArray usersArray = jsonObject.getJSONArray("users"); 44 | JSONObject userObject = usersArray.getJSONObject(0); 45 | channelId = userObject.getString("_id"); 46 | 47 | return channelId; 48 | } 49 | 50 | public boolean isChannelLive(String channelId) throws IOException, JSONException { 51 | HttpGet httpGet = createHttpGet(CHANNEL_STREAM + channelId); 52 | CloseableHttpResponse response = httpClient.execute(httpGet); 53 | HttpEntity entity = response.getEntity(); 54 | String body = EntityUtils.toString(entity); 55 | EntityUtils.consume(entity); 56 | response.close(); 57 | 58 | JSONObject jsonObject = new JSONObject(body); 59 | return !jsonObject.isNull("stream"); 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /src/main/java/utils/Configs.java: -------------------------------------------------------------------------------- 1 | package utils; 2 | 3 | public final class Configs { 4 | 5 | public static final String USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36"; 6 | 7 | public static final String ACCEPT_VIDEO = "application/x-mpegURL, application/vnd.apple.mpegurl, application/json, text/plain"; 8 | 9 | public static final String GET_VIDEO = "https://usher.ttvnw.net/api/channel/hls/%s.m3u8?" + 10 | "allow_source=true&baking_bread=true&baking_brownies=true&baking_brownies_timeout=1050&" + 11 | "fast_bread=true&p=3168255&player_backend=mediaplayer&playlist_include_framerate=true&" + 12 | "reassignments_supported=false&rtqos=business_logic_reverse&cdm=wv&sig=%s&token=%s"; 13 | 14 | public static final String ACCEPT_INFO = "application/vnd.twitchtv.v5+json; charset=UTF-8"; 15 | 16 | public static final String GET_INFO = "https://api.twitch.tv/api/channels/" + 17 | "%s/access_token?need_https=true&oauth_token=&" + 18 | "platform=web&player_backend=mediaplayer&player_type=site&client_id=%s"; 19 | 20 | public static final String ACCEPT_LANG = "en-us"; 21 | 22 | public static final String CONTENT_INFO = "application/json; charset=UTF-8"; 23 | 24 | public static final String REFERER = "https://www.twitch.tv/"; 25 | 26 | private Configs() { 27 | // to prevent from being explicitly instantiated 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/utils/HttpClient.java: -------------------------------------------------------------------------------- 1 | package utils; 2 | 3 | import org.apache.http.HttpHost; 4 | import org.apache.http.auth.AuthScope; 5 | import org.apache.http.auth.UsernamePasswordCredentials; 6 | import org.apache.http.client.CredentialsProvider; 7 | import org.apache.http.client.methods.CloseableHttpResponse; 8 | import org.apache.http.client.methods.HttpUriRequest; 9 | import org.apache.http.impl.client.BasicCredentialsProvider; 10 | import org.apache.http.impl.client.CloseableHttpClient; 11 | import org.apache.http.impl.client.HttpClientBuilder; 12 | import org.apache.http.impl.client.HttpClients; 13 | import org.apache.http.impl.conn.BasicHttpClientConnectionManager; 14 | 15 | import java.io.IOException; 16 | 17 | public class HttpClient { 18 | 19 | private final CloseableHttpClient client; 20 | 21 | public HttpClient(String ip, int port) { 22 | this(ip, port, null, null); 23 | } 24 | 25 | public HttpClient(String hostname, int port, String user, String pass) { 26 | HttpHost superProxy = new HttpHost(hostname, port); 27 | HttpClientBuilder builder = HttpClients.custom(); 28 | if (user != null && pass != null) { 29 | CredentialsProvider provider = new BasicCredentialsProvider(); 30 | provider.setCredentials(new AuthScope(superProxy), new UsernamePasswordCredentials(user, pass)); 31 | builder.setDefaultCredentialsProvider(provider); 32 | } 33 | client = builder 34 | .setConnectionManager(new BasicHttpClientConnectionManager()) 35 | .setProxy(superProxy) 36 | .build(); 37 | } 38 | 39 | public HttpClient() { 40 | client = HttpClients.createDefault(); 41 | } 42 | 43 | public CloseableHttpResponse execute(HttpUriRequest request) throws IOException { 44 | return client.execute(request); 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /src/main/java/viewbot/ViewBot.java: -------------------------------------------------------------------------------- 1 | package viewbot; 2 | 3 | import controller.Controller; 4 | import javafx.application.Platform; 5 | import org.apache.http.HttpHeaders; 6 | import org.apache.http.client.methods.CloseableHttpResponse; 7 | import org.apache.http.client.methods.HttpGet; 8 | import org.apache.http.client.methods.HttpHead; 9 | import org.apache.http.util.EntityUtils; 10 | import org.json.JSONException; 11 | import org.json.JSONObject; 12 | import utils.Configs; 13 | import utils.HttpClient; 14 | 15 | import java.io.IOException; 16 | import java.net.URLEncoder; 17 | import java.nio.charset.StandardCharsets; 18 | import java.util.concurrent.ExecutorService; 19 | import java.util.concurrent.Executors; 20 | import java.util.concurrent.LinkedBlockingQueue; 21 | import java.util.concurrent.TimeUnit; 22 | 23 | import static service.TwitchUtil.CLIENT_ID; 24 | 25 | public class ViewBot { 26 | 27 | private final Controller controller; 28 | 29 | private final LinkedBlockingQueue proxyQueue; 30 | 31 | private final String target; 32 | 33 | private final int threads; 34 | 35 | private ExecutorService threadPool; 36 | 37 | public ViewBot(Controller controller, LinkedBlockingQueue proxyQueue, String target, int threads) { 38 | this.controller = controller; 39 | this.proxyQueue = proxyQueue; 40 | this.target = target; 41 | this.threads = threads; 42 | } 43 | 44 | private void writeToLog(String msg) { 45 | Platform.runLater(() -> controller.writeToLog(msg)); 46 | } 47 | 48 | public void start() { 49 | threadPool = Executors.newFixedThreadPool(threads); 50 | writeToLog("ViewBot has been started with: " + threads + " threads"); 51 | for (int i = 0; i < threads; i++) { 52 | this.threadPool.execute(getExecutable()); 53 | } 54 | while (!controller.getStartButton().getText().equals("START")) { 55 | try { 56 | TimeUnit.MILLISECONDS.sleep(2000); 57 | } catch (InterruptedException e) { 58 | Thread.currentThread().interrupt(); 59 | } 60 | } 61 | stop(); 62 | } 63 | 64 | private Runnable getExecutable() { 65 | return () -> { 66 | String[] proxyEntry = new String[0]; 67 | try { 68 | proxyEntry = proxyQueue.take().split(":"); 69 | } catch (InterruptedException e) { 70 | Thread.currentThread().interrupt(); 71 | } 72 | String hostname = proxyEntry[0]; 73 | int port = Integer.parseInt(proxyEntry[1]); 74 | HttpClient httpClient; 75 | if (proxyEntry.length == 4) { 76 | String user = proxyEntry[2]; 77 | String pass = proxyEntry[3]; 78 | httpClient = new HttpClient(hostname, port, user, pass); 79 | } else if (proxyEntry.length == 2) { 80 | httpClient = new HttpClient(hostname, port); 81 | } else { 82 | writeToLog("Invalid proxy configuration. Continuing..."); 83 | return; 84 | } 85 | String[] info; 86 | try { 87 | info = getInfo(httpClient); 88 | if (info.length == 0) { 89 | writeToLog("Bad proxy. Continuing..."); 90 | return; 91 | } 92 | String token = info[0]; 93 | String sig = info[1]; 94 | boolean viewWasSent = false; 95 | while (true) { 96 | String videoSequenceURL = getVideoSequence(httpClient, token, sig); 97 | if (videoSequenceURL.isEmpty()) 98 | return; 99 | sendView(httpClient, videoSequenceURL); 100 | if (!viewWasSent) { 101 | viewWasSent = true; 102 | Platform.runLater(controller::addCount); 103 | } 104 | TimeUnit.MILLISECONDS.sleep(5000); 105 | } 106 | } catch (InterruptedException e) { 107 | Thread.currentThread().interrupt(); 108 | } catch (IOException | JSONException e) { 109 | writeToLog("Bad proxy: " + hostname); 110 | } 111 | }; 112 | } 113 | 114 | public void stop() { 115 | threadPool.shutdown(); 116 | threadPool.shutdownNow(); 117 | while (true) { 118 | try { 119 | writeToLog("Shutting down threads..."); 120 | if (threadPool.awaitTermination(2000, TimeUnit.MILLISECONDS)) { 121 | break; 122 | } 123 | } catch (InterruptedException e) { 124 | Thread.currentThread().interrupt(); 125 | } 126 | } 127 | Platform.runLater(controller::stopViewBot); 128 | } 129 | 130 | private void sendView(HttpClient client, String url) throws IOException { 131 | HttpHead headRequest = new HttpHead(url); 132 | headRequest.setHeader(HttpHeaders.USER_AGENT, Configs.USER_AGENT); 133 | headRequest.setHeader(HttpHeaders.ACCEPT, Configs.ACCEPT_VIDEO); 134 | client.execute(headRequest); 135 | } 136 | 137 | private String getVideoSequence(HttpClient client, String token, String sig) throws IOException { 138 | String url = String.format(Configs.GET_VIDEO, target, sig, token); 139 | System.out.println(url); 140 | HttpGet getRequest = new HttpGet(url); 141 | getRequest.setHeader(HttpHeaders.USER_AGENT, Configs.USER_AGENT); 142 | getRequest.setHeader(HttpHeaders.ACCEPT, Configs.ACCEPT_VIDEO); 143 | CloseableHttpResponse response = client.execute(getRequest); 144 | String body = EntityUtils.toString(response.getEntity()); 145 | if (body == null) { 146 | writeToLog("Can't get video sequence"); 147 | return ""; 148 | } 149 | return "https://" + body.substring(body.indexOf("https://") + "https://".length(), body.indexOf(".m3u8")) + ".m3u8"; 150 | } 151 | 152 | private String[] getInfo(HttpClient httpClient) throws JSONException, IOException { 153 | String[] resultArray = new String[2]; 154 | String url = String.format(Configs.GET_INFO, target, CLIENT_ID); 155 | HttpGet getRequest = new HttpGet(url); 156 | getRequest.setHeader(HttpHeaders.USER_AGENT, Configs.USER_AGENT); 157 | getRequest.setHeader(HttpHeaders.ACCEPT, Configs.ACCEPT_INFO); 158 | getRequest.setHeader(HttpHeaders.CONTENT_TYPE, Configs.CONTENT_INFO); 159 | getRequest.setHeader(HttpHeaders.ACCEPT_LANGUAGE, Configs.ACCEPT_LANG); 160 | getRequest.setHeader(HttpHeaders.REFERER, Configs.REFERER + target); 161 | try (CloseableHttpResponse response = httpClient.execute(getRequest)) { 162 | String body = EntityUtils.toString(response.getEntity()); 163 | JSONObject jsonObject = new JSONObject(body); 164 | resultArray[0] = URLEncoder.encode(jsonObject.getString("token").replace("\\", ""), StandardCharsets.UTF_8); 165 | resultArray[1] = jsonObject.getString("sig").replace("\\", ""); 166 | } 167 | return resultArray; 168 | } 169 | 170 | } 171 | -------------------------------------------------------------------------------- /src/main/resources/foo.fxml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 31 |