Commit changes.

This commit is contained in:
FireMasterK 2021-02-24 15:22:29 +05:30
parent 947d6d5787
commit dd2ee203bb
No known key found for this signature in database
GPG Key ID: 8DFF5DD33E93DB58
22 changed files with 650 additions and 531 deletions

View File

@ -19,7 +19,7 @@ dependencies {
implementation 'org.bouncycastle:bcprov-jdk15on:1.67'
implementation 'org.mongodb:mongodb-driver-sync:4.2.1'
implementation 'io.projectreactor.netty:reactor-netty:1.0.4'
implementation 'com.github.TeamNewPipe:NewPipeExtractor:v0.20.8'
implementation 'com.github.TeamNewPipe:NewPipeExtractor:SNAPSHOT'
implementation 'com.github.TeamNewPipe:nanojson:1d9e1aea9049fc9f85e68b43ba39fe7be1c1f751'
implementation 'com.fasterxml.jackson.core:jackson-core:2.12.1'
implementation 'com.fasterxml.jackson.core:jackson-annotations:2.12.1'
@ -27,6 +27,8 @@ dependencies {
implementation 'org.json:json:20201115'
implementation 'com.github.ben-manes.caffeine:caffeine:2.8.6'
implementation 'com.rometools:rome:1.15.0'
implementation 'com.github.ipfs:java-ipfs-http-client:v1.3.3'
implementation 'net.java.dev.jna:jna-platform:5.7.0'
}
shadowJar {

View File

@ -1,6 +1,13 @@
package me.kavin.piped;
import io.netty.util.AsciiString;
import static io.netty.handler.codec.http.HttpHeaderNames.ACCESS_CONTROL_ALLOW_ORIGIN;
import static io.netty.handler.codec.http.HttpHeaderNames.CACHE_CONTROL;
import static io.netty.handler.codec.http.HttpHeaderNames.CONTENT_TYPE;
import static io.netty.handler.codec.http.HttpHeaderValues.APPLICATION_JSON;
import static io.netty.handler.codec.http.HttpHeaderValues.TEXT_PLAIN;
import java.nio.charset.StandardCharsets;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.schabi.newpipe.extractor.NewPipe;
import org.schabi.newpipe.extractor.localization.Localization;
@ -11,6 +18,7 @@ import com.rometools.rome.io.XmlReader;
import io.netty.buffer.ByteBufAllocator;
import io.netty.handler.codec.http.QueryStringDecoder;
import io.netty.util.AsciiString;
import me.kavin.piped.consts.Constants;
import me.kavin.piped.utils.DownloaderImpl;
import me.kavin.piped.utils.ResponseHelper;
@ -22,11 +30,6 @@ import reactor.netty.NettyOutbound;
import reactor.netty.http.server.HttpServer;
import reactor.netty.http.server.HttpServerResponse;
import java.nio.charset.StandardCharsets;
import static io.netty.handler.codec.http.HttpHeaderNames.*;
import static io.netty.handler.codec.http.HttpHeaderValues.*;
public class Main {
public static void main(String[] args) throws Exception {
@ -41,7 +44,8 @@ public class Main {
QueryStringDecoder query = new QueryStringDecoder(req.uri());
try {
return writeResponse(res, query.parameters().get("hub.challenge").get(0), TEXT_PLAIN, 200, "private", start);
return writeResponse(res, query.parameters().get("hub.challenge").get(0), TEXT_PLAIN, 200,
"private", start);
} catch (Exception e) {
e.printStackTrace();
@ -226,25 +230,31 @@ public class Main {
server.onDispose().block();
}
}).bindNow();
Thread.sleep(Long.MAX_VALUE);
}
public static NettyOutbound writeResponse(HttpServerResponse res, String resp, int code, String cache, long time) {
return writeResponse(res, resp, APPLICATION_JSON, code, cache, time);
}
public static NettyOutbound writeResponse(HttpServerResponse res, String resp, AsciiString mimeType, int code, String cache, long time) {
public static NettyOutbound writeResponse(HttpServerResponse res, String resp, AsciiString mimeType, int code,
String cache, long time) {
return writeResponse(res, resp, mimeType.toString(), code, cache, time);
}
public static NettyOutbound writeResponse(HttpServerResponse res, String resp, String mimeType, int code, String cache, long time) {
public static NettyOutbound writeResponse(HttpServerResponse res, String resp, String mimeType, int code,
String cache, long time) {
return writeResponse(res, resp.getBytes(StandardCharsets.UTF_8), mimeType, code, cache, time);
}
public static NettyOutbound writeResponse(HttpServerResponse res, byte[] resp, int code, String cache, long time) {
return writeResponse(res, resp, APPLICATION_JSON, code, cache, time);
}
public static NettyOutbound writeResponse(HttpServerResponse res, byte[] resp, AsciiString mimeType, int code, String cache, long time) {
public static NettyOutbound writeResponse(HttpServerResponse res, byte[] resp, AsciiString mimeType, int code,
String cache, long time) {
return writeResponse(res, resp, mimeType.toString(), code, cache, time);
}
@ -258,13 +268,12 @@ public class Main {
.sendByteArray(Flux.just(resp));
}
public static NettyOutbound writeResponse(HttpServerResponse res, Flux<String> resp, int code, String cache) {
return writeResponse(res, resp, APPLICATION_JSON, code, cache);
}
public static NettyOutbound writeResponse(HttpServerResponse res, Flux<String> resp, AsciiString mimeType, int code, String cache) {
public static NettyOutbound writeResponse(HttpServerResponse res, Flux<String> resp, AsciiString mimeType, int code,
String cache) {
return writeResponse(res, resp, mimeType.toString(), code, cache);
}

View File

@ -10,6 +10,8 @@ import org.schabi.newpipe.extractor.NewPipe;
import org.schabi.newpipe.extractor.StreamingService;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
public class Constants {
@ -27,6 +29,8 @@ public class Constants {
.version(Version.HTTP_2).build();
// public static final HttpClient h3client = Http3ClientBuilder.newBuilder().followRedirects(Redirect.NORMAL).build();
public static final MongoClient mongoClient;
public static final ObjectMapper mapper = new ObjectMapper();
static {
@ -37,6 +41,7 @@ public class Constants {
PORT = Integer.parseInt(prop.getProperty("PORT"));
PROXY_PART = prop.getProperty("PROXY_PART");
mongoClient = MongoClients.create(prop.getProperty("MONGO_URI"));
} catch (Exception e) {
throw new RuntimeException(e);
}

View File

@ -0,0 +1,20 @@
package me.kavin.piped.ipfs;
import me.kavin.piped.utils.Multithreading;
import me.kavin.piped.utils.obj.Channel;
public class IPFS {
// private static final io.ipfs.api.IPFS ipfs = new io.ipfs.api.IPFS(new MultiAddress("/ip4/127.0.0.1/tcp/5001"));
public static void publishData(final Channel channel) {
Multithreading.runAsync(() -> {
try {
// ipfs.pubsub.pub(URLUtils.silentEncode(channel.id),
// URLUtils.silentEncode(Constants.mapper.writeValueAsString(channel)));
} catch (Exception e) {
e.printStackTrace();
}
});
}
}

View File

@ -0,0 +1,19 @@
package me.kavin.piped.utils;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Multithreading {
private static final ExecutorService es = Executors.newFixedThreadPool(16);
public static void runAsync(final Runnable runnable) {
es.submit(runnable);
}
public static void runAsyncTimeout(final Runnable runnable) {
es.submit(runnable);
}
}

View File

@ -0,0 +1,7 @@
package me.kavin.piped.utils;
public class RegisterRequest {
public String username, password;
}

View File

@ -10,7 +10,6 @@ import java.net.http.HttpResponse.BodyHandlers;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.exception.ExceptionUtils;
@ -34,7 +33,9 @@ import com.github.benmanes.caffeine.cache.LoadingCache;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import me.kavin.piped.consts.Constants;
import me.kavin.piped.ipfs.IPFS;
import me.kavin.piped.utils.obj.Channel;
import me.kavin.piped.utils.obj.ChapterSegment;
import me.kavin.piped.utils.obj.PipedStream;
import me.kavin.piped.utils.obj.Playlist;
import me.kavin.piped.utils.obj.SearchResults;
@ -75,6 +76,7 @@ public class ResponseHelper {
final StreamInfo info = futureStream.get();
// System.out.println(Constants.mapper.writeValueAsString(info.getStreamSegments()));
info.getSubtitles().forEach(subtitle -> subtitles
.add(new Subtitle(rewriteURL(subtitle.getUrl()), subtitle.getFormat().getMimeType())));
@ -114,19 +116,20 @@ public class ResponseHelper {
}
if (!livestream) {
info.getVideoOnlyStreams()
.forEach(stream -> videoStreams
.add(new PipedStream(rewriteURL(stream.getUrl()), String.valueOf(stream.getFormat()),
stream.getResolution(), stream.getFormat().getMimeType(), true)));
info.getVideoOnlyStreams().forEach(stream -> videoStreams.add(new PipedStream(rewriteURL(stream.getUrl()),
String.valueOf(stream.getFormat()), stream.getResolution(), stream.getFormat().getMimeType(), true,
stream.getBitrate(), stream.getInitStart(), stream.getInitEnd(), stream.getIndexStart(),
stream.getIndexEnd(), stream.getCodec(), stream.getWidth(), stream.getHeight(), 30)));
info.getVideoStreams()
.forEach(stream -> videoStreams
.add(new PipedStream(rewriteURL(stream.getUrl()), String.valueOf(stream.getFormat()),
stream.getResolution(), stream.getFormat().getMimeType(), false)));
info.getAudioStreams()
.forEach(stream -> audioStreams
.add(new PipedStream(rewriteURL(stream.getUrl()), String.valueOf(stream.getFormat()),
stream.getAverageBitrate() + " kbps", stream.getFormat().getMimeType(), false)));
.forEach(stream -> audioStreams.add(new PipedStream(rewriteURL(stream.getUrl()),
String.valueOf(stream.getFormat()), stream.getAverageBitrate() + " kbps",
stream.getFormat().getMimeType(), false, stream.getBitrate(), stream.getInitStart(),
stream.getInitEnd(), stream.getIndexStart(), stream.getIndexEnd(), stream.getCodec())));
}
final List<StreamItem> relatedStreams = new ObjectArrayList<>();
@ -138,6 +141,11 @@ public class ResponseHelper {
item.getTextualUploadDate(), item.getDuration(), item.getViewCount()));
});
List<ChapterSegment> segments = new ObjectArrayList<>();
info.getStreamSegments().forEach(
segment -> segments.add(new ChapterSegment(segment.getTitle(), segment.getStartTimeSeconds())));
final Streams streams = new Streams(info.getName(), info.getDescription().getContent(),
info.getTextualUploadDate(), info.getUploaderName(), info.getUploaderUrl().substring(23),
rewriteURL(info.getUploaderAvatarUrl()), rewriteURL(info.getThumbnailUrl()), info.getDuration(),
@ -148,8 +156,7 @@ public class ResponseHelper {
}
public static final byte[] channelResponse(String channelId)
throws IOException, ExtractionException, InterruptedException {
public static final byte[] channelResponse(String channelId) throws Exception {
final ChannelInfo info = ChannelInfo.getInfo("https://youtube.com/channel/" + channelId);
@ -164,9 +171,11 @@ public class ResponseHelper {
String nextpage = info.hasNextPage() ? info.getNextPage().getUrl() : null;
final Channel channel = new Channel(info.getName(), rewriteURL(info.getAvatarUrl()),
final Channel channel = new Channel(info.getId(), info.getName(), rewriteURL(info.getAvatarUrl()),
rewriteURL(info.getBannerUrl()), info.getDescription(), nextpage, relatedStreams);
IPFS.publishData(channel);
return Constants.mapper.writeValueAsBytes(channel);
}

View File

@ -4,11 +4,12 @@ import java.util.List;
public class Channel {
public String name, avatarUrl, bannerUrl, description, nextpage;
public String id, name, avatarUrl, bannerUrl, description, nextpage;
public List<StreamItem> relatedStreams;
public Channel(String name, String avatarUrl, String bannerUrl, String description, String nextpage,
public Channel(String id, String name, String avatarUrl, String bannerUrl, String description, String nextpage,
List<StreamItem> relatedStreams) {
this.id = id;
this.name = name;
this.avatarUrl = avatarUrl;
this.bannerUrl = bannerUrl;

View File

@ -0,0 +1,12 @@
package me.kavin.piped.utils.obj;
public class ChapterSegment {
public String title;
public int start;
public ChapterSegment(String title, int start) {
this.title = title;
this.start = start;
}
}

View File

@ -2,9 +2,11 @@ package me.kavin.piped.utils.obj;
public class PipedStream {
public String url, format, quality, mimeType;
public String url, format, quality, mimeType, codec;
public boolean videoOnly;
public int bitrate, initStart, initEnd, indexStart, indexEnd, width, height, fps;
public PipedStream(String url, String format, String quality, String mimeType, boolean videoOnly) {
this.url = url;
this.format = format;
@ -12,4 +14,37 @@ public class PipedStream {
this.mimeType = mimeType;
this.videoOnly = videoOnly;
}
public PipedStream(String url, String format, String quality, String mimeType, boolean videoOnly, int bitrate,
int initStart, int initEnd, int indexStart, int indexEnd, String codec) {
this.url = url;
this.format = format;
this.quality = quality;
this.mimeType = mimeType;
this.videoOnly = videoOnly;
this.bitrate = bitrate;
this.initStart = initStart;
this.initEnd = initEnd;
this.indexStart = indexStart;
this.indexEnd = indexEnd;
this.codec = codec;
}
public PipedStream(String url, String format, String quality, String mimeType, boolean videoOnly, int bitrate,
int initStart, int initEnd, int indexStart, int indexEnd, String codec, int width, int height, int fps) {
this.url = url;
this.format = format;
this.quality = quality;
this.mimeType = mimeType;
this.videoOnly = videoOnly;
this.bitrate = bitrate;
this.initStart = initStart;
this.initEnd = initEnd;
this.indexStart = indexStart;
this.indexEnd = indexEnd;
this.codec = codec;
this.width = width;
this.height = height;
this.fps = fps;
}
}