2020-11-12 21:19:45 +00:00
|
|
|
package me.kavin.piped;
|
|
|
|
|
2021-03-29 13:59:10 +00:00
|
|
|
import io.activej.inject.Injector;
|
2022-01-31 04:17:16 +00:00
|
|
|
import me.kavin.piped.consts.Constants;
|
2021-07-16 22:40:46 +00:00
|
|
|
import me.kavin.piped.utils.DatabaseSessionFactory;
|
2020-11-12 21:19:45 +00:00
|
|
|
import me.kavin.piped.utils.DownloaderImpl;
|
2021-07-16 22:40:46 +00:00
|
|
|
import me.kavin.piped.utils.Multithreading;
|
|
|
|
import me.kavin.piped.utils.ResponseHelper;
|
2021-08-20 20:22:39 +00:00
|
|
|
import me.kavin.piped.utils.obj.db.PubSub;
|
2022-02-15 11:55:08 +00:00
|
|
|
import me.kavin.piped.utils.obj.db.User;
|
2022-01-31 04:17:16 +00:00
|
|
|
import org.hibernate.Session;
|
|
|
|
import org.hibernate.Transaction;
|
|
|
|
import org.hibernate.query.Query;
|
|
|
|
import org.schabi.newpipe.extractor.NewPipe;
|
|
|
|
import org.schabi.newpipe.extractor.localization.Localization;
|
|
|
|
|
2022-02-15 11:55:08 +00:00
|
|
|
import javax.persistence.criteria.CriteriaBuilder;
|
|
|
|
import javax.persistence.criteria.CriteriaQuery;
|
|
|
|
import java.util.*;
|
2022-01-31 04:17:16 +00:00
|
|
|
import java.util.concurrent.TimeUnit;
|
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
|
|
|
|
2021-03-29 13:59:10 +00:00
|
|
|
Injector.useSpecializer();
|
2021-02-24 09:52:29 +00:00
|
|
|
|
2021-07-16 22:40:46 +00:00
|
|
|
new Timer().scheduleAtFixedRate(new TimerTask() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
try {
|
|
|
|
Session s = DatabaseSessionFactory.createSession();
|
|
|
|
|
2022-02-15 11:55:08 +00:00
|
|
|
CriteriaBuilder cb = s.getCriteriaBuilder();
|
|
|
|
CriteriaQuery<PubSub> criteria = cb.createQuery(PubSub.class);
|
|
|
|
var root = criteria.from(PubSub.class);
|
|
|
|
var userRoot = criteria.from(User.class);
|
|
|
|
criteria.select(root)
|
|
|
|
.where(cb.and(
|
|
|
|
cb.lessThan(root.get("subbedAt"), System.currentTimeMillis() - TimeUnit.DAYS.toMillis(4)),
|
|
|
|
cb.isMember(root.get("id"), userRoot.<Collection<String>>get("subscribed_ids"))
|
|
|
|
)).distinct(true);
|
|
|
|
|
|
|
|
List<PubSub> pubSubList = s.createQuery(criteria).list();
|
2021-07-16 22:40:46 +00:00
|
|
|
|
2021-08-20 20:22:39 +00:00
|
|
|
Collections.shuffle(pubSubList);
|
2021-07-16 22:40:46 +00:00
|
|
|
|
2021-08-20 20:22:39 +00:00
|
|
|
for (PubSub pubsub : pubSubList)
|
|
|
|
if (pubsub != null)
|
2021-07-20 21:27:28 +00:00
|
|
|
Multithreading.runAsyncLimitedPubSub(() -> {
|
|
|
|
Session sess = DatabaseSessionFactory.createSession();
|
|
|
|
try {
|
2021-08-20 20:22:39 +00:00
|
|
|
ResponseHelper.subscribePubSub(pubsub.getId(), sess);
|
2021-07-21 12:37:43 +00:00
|
|
|
} catch (Exception e) {
|
2021-07-20 21:27:28 +00:00
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
sess.close();
|
|
|
|
});
|
2021-07-16 22:40:46 +00:00
|
|
|
|
|
|
|
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",
|
2022-01-31 04:17:16 +00:00
|
|
|
System.currentTimeMillis() - TimeUnit.DAYS.toMillis(Constants.FEED_RETENTION));
|
2021-07-16 22:40:46 +00:00
|
|
|
|
|
|
|
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));
|
|
|
|
|
2021-03-29 13:59:10 +00:00
|
|
|
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
|
|
|
}
|