69 lines
1.7 KiB
Java
69 lines
1.7 KiB
Java
package de.mrbesen.youtubecrawler;
|
|
|
|
import java.io.File;
|
|
import java.util.Scanner;
|
|
|
|
import org.apache.log4j.Logger;
|
|
|
|
import de.mrbesen.telegram.TelegramAPI;
|
|
import de.mrbesen.telegram.commands.CommandHandler;
|
|
import de.mrbesen.telegram.objects.TUser;
|
|
|
|
public class Main implements CommandHandler{
|
|
|
|
public static void main(String[] args) {
|
|
new Main().run();
|
|
}
|
|
|
|
Crawler cra;
|
|
|
|
private void run() {
|
|
//init Logger
|
|
new Log(); // init logging, set format etc
|
|
Logger log = Logger.getLogger(Main.class.getName());
|
|
|
|
//loading config
|
|
new Config(new File("crawl.conf"));
|
|
|
|
//starting crawler
|
|
cra = new Crawler();
|
|
Thread t = new Thread(cra, "Crawler");
|
|
t.start();
|
|
|
|
//starting BOT API
|
|
TelegramAPI tapi = new TelegramAPI(Config.prop.getProperty("telegramapi.key"));
|
|
tapi.getCommandManager().registerCommand("random", this);
|
|
tapi.setHelpText("Send the command /random to get a random video.");
|
|
tapi.start();
|
|
|
|
//CLI
|
|
Scanner s = new Scanner(System.in);
|
|
String in;
|
|
while((in= s.nextLine()) != null && t.isAlive()) {
|
|
if(in.equalsIgnoreCase("stop")) {
|
|
tapi.stop();
|
|
cra.stop();
|
|
break;
|
|
} else if(in.equalsIgnoreCase("add")) {
|
|
log.info("please enter ytid:");
|
|
String id = s.nextLine().trim();
|
|
if(id.length() > 9 && id.length() < 13) {
|
|
cra.addtoCrawl(id);
|
|
log.info("added.");
|
|
}
|
|
}
|
|
}
|
|
s.close();
|
|
log.info("Terminated.");
|
|
}
|
|
|
|
@Override
|
|
public boolean onCommand(TUser sender, String cmd, String[] args) {
|
|
if(cmd.equals("random")) {
|
|
String ytid = cra.getDB().getRandom();
|
|
sender.sendMessage("https://youtube.com/watch?v=" + ytid);
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
} |