|
| 1 | +package de.mediathekview.mserver.crawler.livestream; |
| 2 | + |
| 3 | +import de.mediathekview.mlib.daten.Film; |
| 4 | +import de.mediathekview.mlib.daten.FilmUrl; |
| 5 | +import de.mediathekview.mlib.daten.Resolution; |
| 6 | +import de.mediathekview.mlib.daten.Sender; |
| 7 | +import de.mediathekview.mlib.messages.listener.MessageListener; |
| 8 | +import de.mediathekview.mserver.base.config.MServerConfigManager; |
| 9 | +import de.mediathekview.mserver.base.messages.ServerMessages; |
| 10 | +import de.mediathekview.mserver.crawler.basic.AbstractCrawler; |
| 11 | +import de.mediathekview.mserver.progress.listeners.SenderProgressListener; |
| 12 | + |
| 13 | +import java.io.IOException; |
| 14 | +import java.net.URL; |
| 15 | +import java.util.ArrayList; |
| 16 | +import java.util.Collection; |
| 17 | +import java.util.HashMap; |
| 18 | +import java.util.HashSet; |
| 19 | +import java.util.List; |
| 20 | +import java.util.Set; |
| 21 | +import java.util.UUID; |
| 22 | +import java.util.concurrent.ForkJoinPool; |
| 23 | +import java.util.concurrent.RecursiveTask; |
| 24 | + |
| 25 | +import org.apache.commons.lang3.StringUtils; |
| 26 | +import org.apache.logging.log4j.LogManager; |
| 27 | +import org.apache.logging.log4j.Logger; |
| 28 | + |
| 29 | +public class LivestreamCrawler extends AbstractCrawler { |
| 30 | + private static final Logger LOG = LogManager.getLogger(LivestreamCrawler.class); |
| 31 | + |
| 32 | + public LivestreamCrawler( |
| 33 | + final ForkJoinPool aForkJoinPool, |
| 34 | + final Collection<MessageListener> aMessageListeners, |
| 35 | + final Collection<SenderProgressListener> aProgressListeners, |
| 36 | + final MServerConfigManager rootConfig) { |
| 37 | + super(aForkJoinPool, aMessageListeners, aProgressListeners, rootConfig); |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + public Sender getSender() { |
| 42 | + return Sender.WDR3; |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + protected RecursiveTask<Set<Film>> createCrawlerTask() { |
| 47 | + |
| 48 | + KodiM3UParser m3uparser = new KodiM3UParser(); |
| 49 | + String data = ""; |
| 50 | + Set<Film> channels = new HashSet<>(); |
| 51 | + try { |
| 52 | + data = this.getConnection().requestBodyAsString(LivestreamConstants.URL_LIVESTREAMS); |
| 53 | + List<HashMap<String, String>> streamInfo = m3uparser.parse(data); |
| 54 | + for (HashMap<String, String> stream : streamInfo) { |
| 55 | + if (LivestreamConstants.sender.containsKey(stream.get("tvg-name"))) { |
| 56 | + Film f = new Film( |
| 57 | + UUID.randomUUID(), |
| 58 | + LivestreamConstants.sender.get(stream.get("tvg-name")), |
| 59 | + stream.get("name") + " Livestream", |
| 60 | + "Livestream", |
| 61 | + null, null); |
| 62 | + f.addUrl(Resolution.NORMAL, new FilmUrl(new URL(stream.get("url")), 0L)); |
| 63 | + channels.add(f); |
| 64 | + } |
| 65 | + } |
| 66 | + //System.out.println("################################"); |
| 67 | + //for (HashMap<String, String> stream : streamInfo) { |
| 68 | + // System.out.println(stream.get("tvg-name") + "#" + stream.get("name") + "#" + stream.get("url")); |
| 69 | + //} |
| 70 | + } catch (final IOException e) { |
| 71 | + LOG.fatal("Exception in {} crawler.", getSender().getName(), e); |
| 72 | + } |
| 73 | + printMessage( |
| 74 | + ServerMessages.DEBUG_ALL_SENDUNG_FOLGEN_COUNT, getSender().getName(), channels.size()); |
| 75 | + getAndSetMaxCount(channels.size()); |
| 76 | + return new LivestreamToFilm(channels); |
| 77 | + } |
| 78 | + |
| 79 | + // TODO: move to class when stable |
| 80 | + class LivestreamToFilm extends RecursiveTask<Set<Film>> { |
| 81 | + private static final long serialVersionUID = 1L; |
| 82 | + Set<Film> setOfFilm; |
| 83 | + LivestreamToFilm (Set<Film> setOfFilm) { |
| 84 | + this.setOfFilm = setOfFilm; |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + protected Set<Film> compute() { |
| 89 | + return setOfFilm; |
| 90 | + } |
| 91 | + |
| 92 | + } |
| 93 | + |
| 94 | + // TODO: move to class when stable |
| 95 | + class KodiM3UParser { |
| 96 | + //#EXTM3U |
| 97 | + //#EXTINF:-1 tvg-name="Das Erste HD" tvg-id="DasErste.de" group-title="IPTV-DE" tvg-logo="https://raw.githubusercontent.com/jnk22/kodinerds-iptv/master/logos/tv/daserstehd.png",Das Erste HD |
| 98 | + //https://mcdn.daserste.de/daserste/de/master.m3u8 |
| 99 | + List<HashMap<String, String>> parse(String aM3U8Data) { |
| 100 | + String[] lines = StringUtils.split(aM3U8Data, '\n'); |
| 101 | + ArrayList<HashMap<String, String>> data = new ArrayList<>(); |
| 102 | + HashMap<String, String> information = new HashMap<>(); |
| 103 | + for (String line : lines) { |
| 104 | + if (!line.isBlank() && !line.equalsIgnoreCase("#EXTM3U") && !line.equalsIgnoreCase("#")) { |
| 105 | + if (line.startsWith("#EXTINF")) { |
| 106 | + // remove extint |
| 107 | + line = line.substring(8); |
| 108 | + // read name (last position after ,) |
| 109 | + information.put("name", line.substring(line.lastIndexOf(",")+1)); |
| 110 | + // remove name but add space for parsing |
| 111 | + line = line.substring(0,line.lastIndexOf(",")) + " "; |
| 112 | + // remove length (first position) |
| 113 | + information.put("length", line.substring(0,line.indexOf(" "))); |
| 114 | + line = line.substring(line.indexOf(" ")+1); |
| 115 | + // bag |
| 116 | + String[] dictList = line.split("\" "); |
| 117 | + for (String valueKeyPair : dictList) { |
| 118 | + String key = valueKeyPair.substring(0,valueKeyPair.indexOf("=")); |
| 119 | + String value = valueKeyPair.substring(valueKeyPair.indexOf("=")+1); |
| 120 | + value = value.replace("\"", ""); |
| 121 | + information.put(key.trim(),value.trim()); |
| 122 | + } |
| 123 | + } else { |
| 124 | + information.put("url", line); |
| 125 | + data.add(information); |
| 126 | + information = new HashMap<>(); |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + return data; |
| 131 | + } |
| 132 | + |
| 133 | + |
| 134 | + } |
| 135 | +} |
0 commit comments