mirror of
https://github.com/JasonHHouse/gaps.git
synced 2026-01-30 07:09:39 -06:00
Telegram integration working
This commit is contained in:
@@ -18,7 +18,7 @@ import org.jetbrains.annotations.NotNull;
|
||||
@JsonFormat(shape = JsonFormat.Shape.OBJECT)
|
||||
public enum Schedule {
|
||||
|
||||
HOURLY("Hourly", "0 48 * * * *", 0),
|
||||
HOURLY("Hourly", "0 13 * * * *", 0),
|
||||
DAILY_4AM("Daily", "0 0 4 * * ?", 1),
|
||||
EVERY_MONDAY("Weekly", "0 0 4 ? * MON *", 2),
|
||||
EVERY_TWO_WEEKS("Bi-weekly", "0 0 4 1,15 * ? *", 3),
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.jasonhhouse.gaps;
|
||||
|
||||
import com.jasonhhouse.gaps.notifications.NotificationAgent;
|
||||
import com.jasonhhouse.gaps.notifications.TelegramNotificationAgent;
|
||||
import com.jasonhhouse.gaps.service.IoService;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
public class NotificationConfiguration {
|
||||
|
||||
private final IoService ioService;
|
||||
|
||||
public NotificationConfiguration(IoService ioService) {
|
||||
this.ioService = ioService;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public NotificationAgent getElement() {
|
||||
return new TelegramNotificationAgent(ioService);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,6 +1,17 @@
|
||||
/*
|
||||
*
|
||||
* Copyright 2020 Jason H House
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.jasonhhouse.gaps;
|
||||
|
||||
import com.jasonhhouse.gaps.service.IoService;
|
||||
import com.jasonhhouse.gaps.service.NotificationService;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -19,16 +30,17 @@ public class SearchGapsTask implements Runnable {
|
||||
private final IoService ioService;
|
||||
private final PlexQuery plexQuery;
|
||||
private final GapsUrlGenerator gapsUrlGenerator;
|
||||
private final NotificationService notificationService;
|
||||
|
||||
public SearchGapsTask(GapsService gapsService, GapsSearch gapsSearch, IoService ioService, PlexQuery plexQuery, GapsUrlGenerator gapsUrlGenerator) {
|
||||
public SearchGapsTask(GapsService gapsService, GapsSearch gapsSearch, IoService ioService, PlexQuery plexQuery, GapsUrlGenerator gapsUrlGenerator, NotificationService notificationService) {
|
||||
this.gapsService = gapsService;
|
||||
this.gapsSearch = gapsSearch;
|
||||
this.ioService = ioService;
|
||||
this.plexQuery = plexQuery;
|
||||
this.gapsUrlGenerator = gapsUrlGenerator;
|
||||
this.notificationService = notificationService;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
LOGGER.info("run()");
|
||||
@@ -38,6 +50,8 @@ public class SearchGapsTask implements Runnable {
|
||||
return;
|
||||
}
|
||||
|
||||
checkPlexServers();
|
||||
|
||||
updatePlexLibraries();
|
||||
|
||||
updateLibraryMovies();
|
||||
@@ -45,19 +59,32 @@ public class SearchGapsTask implements Runnable {
|
||||
findRecommendedMovies();
|
||||
}
|
||||
|
||||
private void updateLibraryMovies() {
|
||||
LOGGER.info("updateLibraryMovies");
|
||||
private void checkPlexServers() {
|
||||
LOGGER.info("checkPlexServers()");
|
||||
|
||||
for (PlexServer plexServer : gapsService.getPlexSearch().getPlexServers()) {
|
||||
for(PlexLibrary plexLibrary : plexServer.getPlexLibraries()) {
|
||||
Payload payload = plexQuery.queryPlexServer(plexServer);
|
||||
if (payload.getCode() == Payload.PLEX_CONNECTION_SUCCEEDED.getCode()) {
|
||||
notificationService.plexServerConnectSuccessful(plexServer);
|
||||
} else {
|
||||
notificationService.plexServerConnectFailed(plexServer, payload.getReason());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void updateLibraryMovies() {
|
||||
LOGGER.info("updateLibraryMovies()");
|
||||
for (PlexServer plexServer : gapsService.getPlexSearch().getPlexServers()) {
|
||||
for (PlexLibrary plexLibrary : plexServer.getPlexLibraries()) {
|
||||
HttpUrl url = gapsUrlGenerator.generatePlexLibraryUrl(plexServer, plexLibrary);
|
||||
List<Movie> ownedMovies =plexQuery.findAllPlexMovies(generateOwnedMovieMap(), url);
|
||||
List<Movie> ownedMovies = plexQuery.findAllPlexMovies(generateOwnedMovieMap(), url);
|
||||
ioService.writeOwnedMoviesToFile(ownedMovies, plexLibrary.getMachineIdentifier(), plexLibrary.getKey());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void updatePlexLibraries() {
|
||||
LOGGER.info("updatePlexLibraries");
|
||||
LOGGER.info("updatePlexLibraries()");
|
||||
//Update each Plex Library from each Plex Server
|
||||
for (PlexServer plexServer : gapsService.getPlexSearch().getPlexServers()) {
|
||||
Payload getLibrariesResults = plexQuery.getLibraries(plexServer);
|
||||
@@ -70,9 +97,9 @@ public class SearchGapsTask implements Runnable {
|
||||
}
|
||||
|
||||
private void findRecommendedMovies() {
|
||||
LOGGER.info("findRecommendedMovies");
|
||||
LOGGER.info("findRecommendedMovies()");
|
||||
for (PlexServer plexServer : gapsService.getPlexSearch().getPlexServers()) {
|
||||
for(PlexLibrary plexLibrary : plexServer.getPlexLibraries()) {
|
||||
for (PlexLibrary plexLibrary : plexServer.getPlexLibraries()) {
|
||||
gapsSearch.run(plexLibrary.getMachineIdentifier(), plexLibrary.getKey());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
/*
|
||||
*
|
||||
* Copyright 2020 Jason H House
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.jasonhhouse.gaps.notifications;
|
||||
|
||||
public interface NotificationAgent {
|
||||
boolean isEnabled();
|
||||
|
||||
void sendMessage(String level, String message);
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
package com.jasonhhouse.gaps.notifications;
|
||||
|
||||
import com.jasonhhouse.gaps.service.IoService;
|
||||
import java.io.IOException;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import okhttp3.HttpUrl;
|
||||
import okhttp3.MediaType;
|
||||
import okhttp3.OkHttpClient;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.RequestBody;
|
||||
import okhttp3.Response;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class TelegramNotificationAgent implements NotificationAgent {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(TelegramNotificationAgent.class);
|
||||
private static final long TIMEOUT = 2500;
|
||||
private final String botId;
|
||||
private final String chatId;
|
||||
private final OkHttpClient client;
|
||||
|
||||
private final IoService ioService;
|
||||
|
||||
public TelegramNotificationAgent(IoService ioService) {
|
||||
this.ioService = ioService;
|
||||
|
||||
client = new OkHttpClient.Builder()
|
||||
.connectTimeout(TIMEOUT, TimeUnit.MILLISECONDS)
|
||||
.readTimeout(TIMEOUT, TimeUnit.MILLISECONDS)
|
||||
.writeTimeout(TIMEOUT, TimeUnit.MILLISECONDS)
|
||||
.build();
|
||||
|
||||
botId = "1302139119:AAEuphtEuopXaTpitiGlWCY-3l33SBkPe2o"; //Get from IoService
|
||||
chatId = "1041081317"; //Get from IoService
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEnabled() {
|
||||
//ToDo Check IoService
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendMessage(String level, String message) {
|
||||
|
||||
HttpUrl url = new HttpUrl.Builder()
|
||||
.scheme("https")
|
||||
.host("api.telegram.org")
|
||||
.addPathSegment(String.format("bot%s", botId))
|
||||
.addPathSegment("sendMessage")
|
||||
.build();
|
||||
|
||||
String telegramMessage = String.format("{\"chat_id\":\"%s\", \"text\":\"%s\", \"parse_mode\":\"HTML\"}", chatId, message);
|
||||
LOGGER.info("telegramMessage {}", telegramMessage);
|
||||
RequestBody body = RequestBody.create(telegramMessage, MediaType.get("application/json"));
|
||||
|
||||
Request request = new Request.Builder()
|
||||
.url(url)
|
||||
.post(body)
|
||||
.build();
|
||||
|
||||
try (Response response = client.newCall(request).execute()) {
|
||||
if (response.isSuccessful()) {
|
||||
LOGGER.error("Telegram message sent via {}", url);
|
||||
} else {
|
||||
LOGGER.error("Error with telegram Url: {} Body returned {}", url, response.body().toString());
|
||||
}
|
||||
|
||||
} catch (IOException e) {
|
||||
LOGGER.error(String.format("Error with telegram Url: %s", url), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.jasonhhouse.gaps.service;
|
||||
|
||||
import com.jasonhhouse.gaps.PlexLibrary;
|
||||
import com.jasonhhouse.gaps.PlexServer;
|
||||
|
||||
public interface Notification {
|
||||
|
||||
void plexServerConnectFailed(PlexServer plexServer, String error);
|
||||
|
||||
void plexServerConnectSuccessful(PlexServer plexServer);
|
||||
|
||||
void plexLibraryScanFailed(PlexServer plexServer, PlexLibrary plexLibrary, String error);
|
||||
|
||||
void plexLibraryScanSuccessful(PlexServer plexServer, PlexLibrary plexLibrary);
|
||||
|
||||
void tmdbConnectionFailed(String error);
|
||||
|
||||
void tmdbConnectionSuccessful();
|
||||
|
||||
void recommendedMoviesSearchStarted(PlexServer plexServer, PlexLibrary plexLibrary);
|
||||
|
||||
void recommendedMoviesSearchFailed(PlexServer plexServer, PlexLibrary plexLibrary, String error);
|
||||
|
||||
void recommendedMoviesSearchFinished(PlexServer plexServer, PlexLibrary plexLibrary);
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
package com.jasonhhouse.gaps.service;
|
||||
|
||||
import com.jasonhhouse.gaps.PlexLibrary;
|
||||
import com.jasonhhouse.gaps.PlexServer;
|
||||
import com.jasonhhouse.gaps.notifications.NotificationAgent;
|
||||
import java.util.List;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class NotificationService implements Notification {
|
||||
|
||||
private final List<NotificationAgent> notificationAgents;
|
||||
|
||||
public NotificationService(List<NotificationAgent> notificationAgents) {
|
||||
this.notificationAgents = notificationAgents;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void plexServerConnectFailed(PlexServer plexServer, String error) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void plexServerConnectSuccessful(PlexServer plexServer) {
|
||||
notificationAgents.forEach(notificationAgent -> notificationAgent.sendMessage("INFO", String.format("<strong>Automatic Search</strong>\nConnection to Plex Server %s Successful", plexServer.getFriendlyName())));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void plexLibraryScanFailed(PlexServer plexServer, PlexLibrary plexLibrary, String error) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void plexLibraryScanSuccessful(PlexServer plexServer, PlexLibrary plexLibrary) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void tmdbConnectionFailed(String error) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void tmdbConnectionSuccessful() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void recommendedMoviesSearchStarted(PlexServer plexServer, PlexLibrary plexLibrary) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void recommendedMoviesSearchFailed(PlexServer plexServer, PlexLibrary plexLibrary, String error) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void recommendedMoviesSearchFinished(PlexServer plexServer, PlexLibrary plexLibrary) {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -45,11 +45,11 @@ public class SchedulerService {
|
||||
private ScheduledFuture<?> scheduledFuture;
|
||||
|
||||
@Autowired
|
||||
public SchedulerService(IoService ioService, GapsService gapsService, GapsSearch gapsSearch, @Qualifier("Gaps") TaskScheduler scheduler, PlexQuery plexQuery, GapsUrlGenerator gapsUrlGenerator) {
|
||||
public SchedulerService(IoService ioService, GapsService gapsService, GapsSearch gapsSearch, @Qualifier("Gaps") TaskScheduler scheduler, PlexQuery plexQuery, GapsUrlGenerator gapsUrlGenerator, NotificationService notificationService) {
|
||||
this.ioService = ioService;
|
||||
this.gapsService = gapsService;
|
||||
this.scheduler = scheduler;
|
||||
this.searchGapsTask = new SearchGapsTask(gapsService, gapsSearch, ioService, plexQuery, gapsUrlGenerator);
|
||||
this.searchGapsTask = new SearchGapsTask(gapsService, gapsSearch, ioService, plexQuery, gapsUrlGenerator, notificationService);
|
||||
}
|
||||
|
||||
public void setSchedule(int intSchedule) throws IOException {
|
||||
|
||||
Reference in New Issue
Block a user