Piped-Backend/src/main/java/me/kavin/piped/Main.java

147 lines
6.0 KiB
Java
Raw Normal View History

2020-11-12 21:19:45 +00:00
package me.kavin.piped;
import io.activej.inject.Injector;
2022-06-17 15:37:10 +00:00
import jakarta.persistence.criteria.CriteriaBuilder;
import me.kavin.piped.consts.Constants;
import me.kavin.piped.utils.*;
2022-09-17 14:59:26 +00:00
import me.kavin.piped.utils.obj.db.PlaylistVideo;
import me.kavin.piped.utils.obj.db.Video;
2022-07-19 08:20:42 +00:00
import org.hibernate.Session;
import org.hibernate.StatelessSession;
import org.schabi.newpipe.extractor.NewPipe;
import org.schabi.newpipe.extractor.localization.ContentCountry;
import org.schabi.newpipe.extractor.localization.Localization;
import org.schabi.newpipe.extractor.services.youtube.YoutubeThrottlingDecrypter;
2022-04-24 17:01:00 +00:00
import org.schabi.newpipe.extractor.services.youtube.extractors.YoutubeStreamExtractor;
import java.io.IOException;
import java.util.*;
import java.util.concurrent.TimeUnit;
2022-09-17 14:59:26 +00:00
import java.util.stream.Collectors;
2020-11-12 21:19:45 +00:00
public class Main {
public static void main(String[] args) throws Exception {
NewPipe.init(new DownloaderImpl(), new Localization("en", "US"), ContentCountry.DEFAULT, Multithreading.getCachedExecutor());
YoutubeStreamExtractor.forceFetchAndroidClient(true);
2022-04-24 17:01:00 +00:00
YoutubeStreamExtractor.forceFetchIosClient(true);
2020-11-12 21:19:45 +00:00
Injector.useSpecializer();
2021-02-24 09:52:29 +00:00
new Timer().scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
try {
System.out.printf("ThrottlingCache: %o entries%n", YoutubeThrottlingDecrypter.getCacheSize());
YoutubeThrottlingDecrypter.clearCache();
} catch (Exception e) {
e.printStackTrace();
}
}
}, 0, TimeUnit.MINUTES.toMillis(60));
2022-07-19 08:20:42 +00:00
if (!Constants.DISABLE_SERVER)
new Thread(() -> {
try {
new ServerLauncher().launch(args);
} catch (Exception e) {
throw new RuntimeException(e);
}
}).start();
try (Session ignored = DatabaseSessionFactory.createSession()) {
System.out.println("Database connection is ready!");
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
if (Constants.DISABLE_TIMERS)
return;
new Timer().scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
try (StatelessSession s = DatabaseSessionFactory.createStatelessSession()) {
2022-09-17 14:59:26 +00:00
final Random random = new Random();
s.createNativeQuery("SELECT id FROM pubsub WHERE subbed_at < :subbedTime AND id IN (" +
"SELECT DISTINCT channel FROM users_subscribed" +
" UNION " +
"SELECT id FROM unauthenticated_subscriptions WHERE subscribed_at > :unauthSubbed" +
")", String.class)
.setParameter("subbedTime", System.currentTimeMillis() - TimeUnit.DAYS.toMillis(4))
.setParameter("unauthSubbed", System.currentTimeMillis() - TimeUnit.DAYS.toMillis(Constants.SUBSCRIPTIONS_EXPIRY))
.stream()
.filter(Objects::nonNull)
.collect(Collectors.toUnmodifiableSet())
.stream()
.sorted(Comparator.comparingInt(o -> random.nextInt()))
.parallel()
.forEach(id -> Multithreading.runAsyncLimitedPubSub(() -> {
try {
ResponseHelper.subscribePubSub(id);
} catch (IOException e) {
ExceptionHandler.handle(e);
}
}));
} catch (Exception e) {
e.printStackTrace();
}
}
}, 0, TimeUnit.MINUTES.toMillis(90));
new Timer().scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
try (StatelessSession s = DatabaseSessionFactory.createStatelessSession()) {
var cb = s.getCriteriaBuilder();
var cd = cb.createCriteriaDelete(Video.class);
var root = cd.from(Video.class);
cd.where(cb.lessThan(root.get("uploaded"), System.currentTimeMillis() - TimeUnit.DAYS.toMillis(Constants.FEED_RETENTION)));
2022-07-04 17:44:16 +00:00
var tr = s.beginTransaction();
var query = s.createMutationQuery(cd);
System.out.printf("Cleanup: Removed %o old videos%n", query.executeUpdate());
tr.commit();
} catch (Exception e) {
e.printStackTrace();
}
}
}, 0, TimeUnit.MINUTES.toMillis(60));
new Timer().scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
try (StatelessSession s = DatabaseSessionFactory.createStatelessSession()) {
CriteriaBuilder cb = s.getCriteriaBuilder();
var pvQuery = cb.createCriteriaDelete(PlaylistVideo.class);
var pvRoot = pvQuery.from(PlaylistVideo.class);
var subQuery = pvQuery.subquery(me.kavin.piped.utils.obj.db.Playlist.class);
var subRoot = subQuery.from(me.kavin.piped.utils.obj.db.Playlist.class);
subQuery.select(subRoot.join("videos").get("id"));
pvQuery.where(cb.not(pvRoot.get("id").in(subQuery)));
var tr = s.beginTransaction();
s.createMutationQuery(pvQuery).executeUpdate();
tr.commit();
}
}
}, 0, TimeUnit.MINUTES.toMillis(60));
2021-02-24 10:26:26 +00:00
}
2020-11-12 21:19:45 +00:00
}