Skip to content

Commit

Permalink
Add property-handling and skeleton-property file
Browse files Browse the repository at this point in the history
To better handle access tokens and device codes and to make them more
persistent, I've decided to implement property-handling using a
property file.

In the given skeleton-property file the first two values (secret and id)
are already pre-filled (seems it has to be this way? but seems weird...)
and the other two will be generated on the first run of the program.

I may have to remove the first two values in the future, but for now
they stay in.
  • Loading branch information
engeld committed May 21, 2020
1 parent 694f962 commit f11dbd7
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 34 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ local.properties
.externalToolBuilders/

# Locally stored "Eclipse launch configurations"
*.launch
*.launch
/target/
16 changes: 13 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
# trakt-average

## History
## Background

### History
Everytime I watch something and it gets scrobbled to [trakt](https://trakt.tv), I meticulously rate the item. So I was always annoyed when I finished watching a season or a show and it didn't use my rated episodes to calculate my season and/or show average rating - why should I have to rate this separately, again?

## How this works
### How this works
It finds shows where either a season or a show is completed and then calculates the average over those items. Then it prints out those averages. In a second step, those values will be written back to trakt to update the season/show ratings.

## Why Java?
### Why Java?
Because my java was getting rusty and I wanted to brush it up a little bit. Maybe I'll rewrite it in another language later, we'll see.

## How to use

### Use precompiled binary

Download

### Use the source, Luke
4 changes: 4 additions & 0 deletions app.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
CLIENT_SECRET=d590f595fda389db5ca36a2f365f2a76008cdb1feef966b1c0856ee9d29d5127
CLIENT_ID=e671fcd13abf819e7aa221602c8edb077990179c0f70e904ab9d4b17262bba93
ACCESS_TOKEN=
DEVICE_CODE=
93 changes: 63 additions & 30 deletions src/main/java/cc/engeld/traktaverage/App.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
package cc.engeld.traktaverage;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Scanner;

import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
Expand Down Expand Up @@ -40,47 +47,67 @@
*/
public class App
{
private static final String CLIENT_ID = "";
private static final String CLIENT_SECRET = "";
private static final String DEVICE_CODE = "";
private static final String REDIRECT_URI = "urn:ietf:wg:oauth:2.0:oob";

private static final Logger LOG = LogManager.getLogger();
private static final TraktV2 trakt = new TraktV2(CLIENT_ID, CLIENT_SECRET, REDIRECT_URI);

protected static TraktV2 getTrakt() {
private static final Logger LOG = LogManager.getLogger();
private static Properties prop = new Properties();

private static TraktV2 trakt = null;
public static void setTrakt(TraktV2 trakt) {
App.trakt = trakt;
}
protected static TraktV2 getTrakt() {
return trakt;
}

private static String accessToken = "";
private static void loadProperties() {
LOG.trace("loadProperties(): enter");
try (InputStream input = new FileInputStream("app.properties")) {
prop.load(input);
} catch (IOException ex) {
LOG.error("loadProperties(): " + ex.toString());
}
LOG.trace("loadProperties(): leave");
}

private static void loadTrakt() {
LOG.trace("loadTrakt(): enter");
setTrakt(trakt = new TraktV2(
prop.getProperty("CLIENT_ID"),
prop.getProperty("CLIENT_SECRET"),
"urn:ietf:wg:oauth:2.0:oob"));
LOG.trace("loadTrakt(): leave");
}

public static void auth() {
if(DEVICE_CODE.isEmpty()) {
if(prop.getProperty("DEVICE_CODE").isEmpty()) {
Response<DeviceCode> codeResponse = null;
try {
codeResponse = getTrakt().generateDeviceCode();
DeviceCode deviceCode = codeResponse.body();
System.out.println("Device Code: " + deviceCode.device_code);
System.out.println("User Code: " + deviceCode.user_code);
System.out.println("Enter the user code at the following URI: " + deviceCode.verification_url);
System.out.println("Set the TEST_DEVICE_CODE variable to run the access token test");

System.out.println("Press \"ENTER\" when finished...");
Scanner scanner = new Scanner(System.in);
scanner.nextLine();

prop.setProperty("DEVICE_CODE", deviceCode.device_code);
} catch (IOException e) {
e.printStackTrace();
}
}

// get accesstoken
if(accessToken == "") {
if(prop.getProperty("ACCESS_TOKEN").isEmpty()) {
try {
Response<AccessToken> response = getTrakt().exchangeDeviceCodeForAccessToken(DEVICE_CODE);
Response<AccessToken> response = getTrakt().exchangeDeviceCodeForAccessToken(prop.getProperty("DEVICE_CODE"));

if(response.isSuccessful()) {
AccessToken accessToken = response.body();
System.out.println("Token: " + accessToken.access_token + " created at " + accessToken.created_at);
prop.setProperty("ACCESS_TOKEN",accessToken.access_token);
}else {
if (response.code() == 401) {
System.out.println("authorization required, supply a valid OAuth access token");
// authorization required, supply a valid OAuth access token
} else {
System.out.println("the request failed for some other reason");
System.out.println(response.code());
Expand All @@ -94,19 +121,23 @@ public static void auth() {


public static void main( String[] args ) {
Configurator.setRootLevel(Level.TRACE);

Configurator.setRootLevel(Level.WARN);
LOG.trace("main(): enter");
// auth();

loadProperties();
loadTrakt();
auth();

LOG.info("main(): accessToken = " + accessToken);
trakt.accessToken(accessToken);
LOG.info("main(): accessToken = " + prop.getProperty("ACCESS_TOKEN"));
trakt.accessToken(prop.getProperty("ACCESS_TOKEN"));
Users users = trakt.users();

getAllRatedEpisodesGroupedByShows(users);

cleanupProperties();
LOG.trace("main(): leave");
}

private static void getAllRatedEpisodesGroupedByShows(Users users) {
LOG.trace("getAllRatedEpisodesByShows(): enter");
try {
Expand Down Expand Up @@ -196,15 +227,17 @@ private static void getAllRatedEpisodesGroupedByShows(Users users) {
}
LOG.trace("getAllRatedEpisodesByShows(): leave");
}


public static boolean containsShow(Collection<Show> c, String showTitle) {
for(Show o : c) {
if(o != null && o.title.equals(showTitle)) {
return true;
}
}
return false;
private static void cleanupProperties() {
LOG.trace("cleanupProperties(): enter");
try (OutputStream output = new FileOutputStream("app.properties")){
prop.store(output, null);
} catch (FileNotFoundException e) {
LOG.error("cleanupProperties(): " + e.toString());
} catch (IOException e) {
LOG.error("cleanupProperties(): " + e.toString());
}
LOG.trace("cleanupProperties(): leave");
}

}
Expand Down

0 comments on commit f11dbd7

Please sign in to comment.