This repository has been archived by the owner on Jun 13, 2020. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Obtain Live streaming channel Id.
- Loading branch information
Showing
11 changed files
with
268 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
heckenlights-backend/src/main/java/de/paluch/heckenlights/application/RuleService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* Copyright 2016 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package de.paluch.heckenlights.application; | ||
|
||
import java.io.IOException; | ||
import java.util.Collection; | ||
|
||
import javax.xml.bind.JAXB; | ||
|
||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.core.io.Resource; | ||
import org.springframework.stereotype.Component; | ||
|
||
import de.paluch.heckenlights.model.Rule; | ||
import de.paluch.heckenlights.model.Rules; | ||
|
||
/** | ||
* @author Mark Paluch | ||
*/ | ||
@Component | ||
public class RuleService { | ||
|
||
private final Resource ruleLocation; | ||
private final Rules rules = new Rules(); | ||
|
||
private long lastModified; | ||
|
||
public RuleService(@Value("${rules.location}") Resource ruleLocation) throws IOException { | ||
|
||
this.ruleLocation = ruleLocation; | ||
this.lastModified = ruleLocation.lastModified(); | ||
|
||
updateRules(); | ||
} | ||
|
||
public boolean isChanged() { | ||
try { | ||
return lastModified != ruleLocation.lastModified(); | ||
} catch (IOException e) { | ||
return false; | ||
} | ||
} | ||
|
||
public void updateRules() throws IOException { | ||
|
||
Rules updated = JAXB.unmarshal(ruleLocation.getFile(), Rules.class); | ||
lastModified = ruleLocation.lastModified(); | ||
updateRules(rules, updated); | ||
} | ||
|
||
public final Rules getRules() { | ||
return rules; | ||
} | ||
|
||
private void updateRules(Rules instance, Rules updated) { | ||
instance.getRules().clear(); | ||
instance.getRules().addAll(updated.getRules()); | ||
|
||
instance.setDefaultAction(updated.getDefaultAction()); | ||
instance.setTimeunit(updated.getTimeunit()); | ||
instance.setTimezone(updated.getTimezone()); | ||
} | ||
} |
115 changes: 115 additions & 0 deletions
115
heckenlights-backend/src/main/java/de/paluch/heckenlights/client/YouTubeClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package de.paluch.heckenlights.client; | ||
|
||
import java.io.File; | ||
import java.io.FileReader; | ||
import java.io.IOException; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Component; | ||
|
||
import com.google.api.client.auth.oauth2.Credential; | ||
import com.google.api.client.auth.oauth2.StoredCredential; | ||
import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp; | ||
import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver; | ||
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow; | ||
import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets; | ||
import com.google.api.client.http.HttpTransport; | ||
import com.google.api.client.http.javanet.NetHttpTransport; | ||
import com.google.api.client.json.JsonFactory; | ||
import com.google.api.client.json.jackson2.JacksonFactory; | ||
import com.google.api.client.util.store.DataStore; | ||
import com.google.api.client.util.store.FileDataStoreFactory; | ||
import com.google.api.services.youtube.YouTube; | ||
import com.google.api.services.youtube.model.LiveBroadcastListResponse; | ||
import com.google.common.cache.CacheBuilder; | ||
import com.google.common.cache.CacheLoader; | ||
import com.google.common.cache.LoadingCache; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
|
||
/** | ||
* @author Mark Paluch | ||
*/ | ||
@Component | ||
@Slf4j | ||
public class YouTubeClient { | ||
|
||
private static final HttpTransport HTTP_TRANSPORT = new NetHttpTransport(); | ||
private static final JsonFactory JSON_FACTORY = new JacksonFactory(); | ||
private static final String CREDENTIALS_DIRECTORY = "oauth-credentials"; | ||
private static final List<String> scopes = Collections.singletonList("https://www.googleapis.com/auth/youtube.readonly"); | ||
private final GoogleClientSecrets clientSecrets; | ||
private final LocalServerReceiver localReceiver; | ||
private final LoadingCache<String, String> cache; | ||
|
||
public YouTubeClient(@Value("${server.port}") int serverPort) throws IOException { | ||
|
||
File file = new File("client_secret.json"); | ||
|
||
if (file.exists()) { | ||
clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new FileReader(file)); | ||
localReceiver = new LocalServerReceiver.Builder().setPort(serverPort + 10).build(); | ||
} else { | ||
|
||
log.warn("No {}, YouTubeClient disabled", file); | ||
|
||
clientSecrets = null; | ||
localReceiver = null; | ||
} | ||
|
||
cache = CacheBuilder.newBuilder().expireAfterWrite(1, TimeUnit.HOURS).build(new CacheLoader<String, String>() { | ||
@Override | ||
public String load(String s) throws Exception { | ||
return fetchStreamingId(); | ||
} | ||
}); | ||
} | ||
|
||
public Credential authorize() throws IOException { | ||
|
||
FileDataStoreFactory fileDataStoreFactory = new FileDataStoreFactory(new File(CREDENTIALS_DIRECTORY)); | ||
DataStore<StoredCredential> datastore = fileDataStoreFactory.getDataStore("listbroadcasts"); | ||
|
||
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, | ||
scopes).setCredentialDataStore(datastore).build(); | ||
|
||
// Authorize. | ||
return new AuthorizationCodeInstalledApp(flow, localReceiver).authorize("user"); | ||
} | ||
|
||
public String getYouTubeStreamingId() { | ||
|
||
try { | ||
return cache.get("youtube-streaming-id"); | ||
} catch (ExecutionException e) { | ||
throw new IllegalStateException(e.getCause()); | ||
} | ||
} | ||
|
||
private String fetchStreamingId() throws IOException { | ||
|
||
// This object is used to make YouTube Data API requests. | ||
YouTube youtube = new YouTube.Builder(HTTP_TRANSPORT, JSON_FACTORY, authorize()).setApplicationName("heckenlights") | ||
.build(); | ||
|
||
// Create a request to list broadcasts. | ||
YouTube.LiveBroadcasts.List liveBroadcastRequest = youtube.liveBroadcasts().list("id,status"); | ||
|
||
// Indicate that the API response should not filter broadcasts | ||
// based on their status. | ||
liveBroadcastRequest.setBroadcastStatus("active"); | ||
liveBroadcastRequest.setBroadcastType("persistent"); | ||
|
||
LiveBroadcastListResponse response = liveBroadcastRequest.execute(); | ||
|
||
if (response.getItems().isEmpty()) { | ||
return ""; | ||
} | ||
|
||
return response.getItems().get(0).getId(); | ||
} | ||
} |
Oops, something went wrong.