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

92 lines
3.2 KiB
Java
Raw Normal View History

2020-11-12 21:19:45 +00:00
package me.kavin.piped;
import java.util.Collections;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.TimeUnit;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.query.Query;
2020-11-12 21:19:45 +00:00
import org.schabi.newpipe.extractor.NewPipe;
import org.schabi.newpipe.extractor.localization.Localization;
import io.activej.inject.Injector;
import me.kavin.piped.utils.DatabaseHelper;
import me.kavin.piped.utils.DatabaseSessionFactory;
2020-11-12 21:19:45 +00:00
import me.kavin.piped.utils.DownloaderImpl;
import me.kavin.piped.utils.Multithreading;
import me.kavin.piped.utils.ResponseHelper;
2020-11-12 21:19:45 +00:00
public class Main {
public static void main(String[] args) throws Exception {
2021-02-24 09:52:29 +00:00
NewPipe.init(new DownloaderImpl(), new Localization("en", "US"));
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 {
Session s = DatabaseSessionFactory.createSession();
List<String> channels = DatabaseHelper.getGlobalSubscribedChannelIds(s);
DatabaseHelper.getPubSubFromIds(s, channels).forEach(pubsub -> {
if (System.currentTimeMillis() - pubsub.getSubbedAt() < TimeUnit.DAYS.toMillis(4))
channels.remove(pubsub.getId());
});
Collections.shuffle(channels);
for (String channelId : channels)
2021-07-20 21:27:28 +00:00
if (channelId != null)
Multithreading.runAsyncLimitedPubSub(() -> {
Session sess = DatabaseSessionFactory.createSession();
try {
ResponseHelper.subscribePubSub(channelId, sess);
2021-07-21 12:37:43 +00:00
} catch (Exception e) {
2021-07-20 21:27:28 +00:00
e.printStackTrace();
}
sess.close();
});
s.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}, 0, TimeUnit.MINUTES.toMillis(90));
new Timer().scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
try {
Session s = DatabaseSessionFactory.createSession();
Transaction tr = s.getTransaction();
tr.begin();
Query<?> query = s.createQuery("delete from Video where uploaded < :time").setParameter("time",
System.currentTimeMillis() - TimeUnit.DAYS.toMillis(10));
System.out.println(String.format("Cleanup: Removed %o old videos", query.executeUpdate()));
tr.commit();
s.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}, 0, TimeUnit.MINUTES.toMillis(60));
new ServerLauncher().launch(args);
2021-02-05 18:28:02 +00:00
2021-02-24 10:26:26 +00:00
}
2020-11-12 21:19:45 +00:00
}