Compare commits
9 Commits
Author | SHA1 | Date | |
---|---|---|---|
ca3c3423b1 | |||
0ce14bdc53 | |||
![]() |
676748f614 | ||
![]() |
b92a325b99 | ||
![]() |
7e12d9e939 | ||
![]() |
38449a7676 | ||
![]() |
7cf0cf5033 | ||
c95613e240 | |||
a7babdbba4 |
33
download.go
Normal file
33
download.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
|
||||
"github.com/lrstanley/go-ytdlp"
|
||||
)
|
||||
|
||||
const OUTPUT_DIR = "C:/Users/Administrator/ytdlpVideos"
|
||||
|
||||
var dl = ytdlp.New().
|
||||
// FormatSort("bestvideo[ext=mp4]+bestaudio[ext=m4a]").
|
||||
FormatSort("res,ext:mp4:m4a").
|
||||
Output("C:/Users/Administrator/ytdlpVideos/%(uploader)s/%(title)s.%(ext)s").
|
||||
LimitRate("50M").
|
||||
// HTTPChunkSize("20M").
|
||||
MarkWatched().
|
||||
SponsorblockMark("all").
|
||||
PrintJSON().
|
||||
RecodeVideo("mp4").
|
||||
ConcurrentFragments(4)
|
||||
|
||||
func Download(event PBEvent, status chan error) {
|
||||
_, err := dl.Run(context.TODO(), event.Record.Link)
|
||||
if err != nil {
|
||||
status <- err
|
||||
return
|
||||
}
|
||||
|
||||
log.Printf("Downloaded %s (%s)", event.Record.Id, event.Record.Link)
|
||||
SetDownloaded(event)
|
||||
}
|
@@ -21,28 +21,49 @@ func init() {
|
||||
|
||||
const DOWNLOAD_WORKERS = 10
|
||||
|
||||
var downloadQueue = make(chan *DownloadTask, 100)
|
||||
|
||||
func enableCORS(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Access-Control-Allow-Origin", "*")
|
||||
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, OPTIONS")
|
||||
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
|
||||
|
||||
if r.Method == http.MethodOptions {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
return
|
||||
}
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
|
||||
func handleDownload(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
http.Error(w, "Invalid request method", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
var req DownloadRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
log.Printf("Error parsing JSON: %v", err)
|
||||
http.Error(w, "Error parsing JSON", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
downloadQueue <- &DownloadTask{Url: req.Link}
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
|
||||
func main() {
|
||||
downloadQueue := make(chan *DownloadTask, 100)
|
||||
for i := 0; i < DOWNLOAD_WORKERS; i++ {
|
||||
worker := &DownloadWorker{id: i, input: downloadQueue}
|
||||
go worker.Run()
|
||||
}
|
||||
|
||||
http.HandleFunc("/download", func(responseWriter http.ResponseWriter, request *http.Request) {
|
||||
defer request.Body.Close()
|
||||
|
||||
req := DownloadRequest{}
|
||||
err := json.NewDecoder(request.Body).Decode(&req)
|
||||
if err != nil {
|
||||
log.Printf("Error parsing JSON: %v", err)
|
||||
http.Error(responseWriter, "Error parsing JSON", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
downloadQueue <- &DownloadTask{Url: req.Link}
|
||||
})
|
||||
mux := http.NewServeMux()
|
||||
mux.Handle("/download", enableCORS(http.HandlerFunc(handleDownload)))
|
||||
log.Println("Server starting on :5000")
|
||||
err := http.ListenAndServe(":5000", nil)
|
||||
err := http.ListenAndServe(":5000", mux)
|
||||
if err != nil {
|
||||
log.Println("Error starting server:", err)
|
||||
}
|
||||
|
10
extension/background.js
Normal file
10
extension/background.js
Normal file
@@ -0,0 +1,10 @@
|
||||
browser.menus.create({
|
||||
id: "youtube-download",
|
||||
title: "Download",
|
||||
contexts: ["all"],
|
||||
documentUrlPatterns: ["*://*.youtube.com/*"],
|
||||
});
|
||||
|
||||
browser.menus.onShown.addListener((info, tab) => {
|
||||
browser.tabs.sendMessage(tab.id, { action: "check-element", info: info });
|
||||
});
|
34
extension/content.js
Normal file
34
extension/content.js
Normal file
@@ -0,0 +1,34 @@
|
||||
const URL = `http://localhost:5000/download`;
|
||||
let lastRightClickCoords = { x: 0, y: 0 };
|
||||
|
||||
document.addEventListener("contextmenu", (event) => {
|
||||
lastRightClickCoords = { x: event.clientX, y: event.clientY };
|
||||
console.log("Right-click coordinates:", lastRightClickCoords);
|
||||
});
|
||||
|
||||
browser.runtime.onMessage.addListener((message, sender, sendResponse) => {
|
||||
const { x, y } = lastRightClickCoords;
|
||||
let element = document.elementFromPoint(x, y);
|
||||
|
||||
while (element && element.tagName != "YTD-RICH-ITEM-RENDERER") {
|
||||
element = element.parentElement;
|
||||
}
|
||||
if (!element.tagName == "YTD-RICH-ITEM-RENDERER") {
|
||||
console.error("No video element found.");
|
||||
return;
|
||||
}
|
||||
|
||||
const link = element.querySelector("a#video-title-link").href;
|
||||
fetch(URL, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify({
|
||||
link: link,
|
||||
}),
|
||||
}).then((res) => {
|
||||
console.log(res);
|
||||
res.json().then((data) => console.log(data));
|
||||
});
|
||||
});
|
20
extension/manifest.json
Normal file
20
extension/manifest.json
Normal file
@@ -0,0 +1,20 @@
|
||||
{
|
||||
"manifest_version": 2,
|
||||
"name": "Youtube Downloader",
|
||||
"version": "1.0",
|
||||
"permissions": [
|
||||
"menus",
|
||||
"activeTab",
|
||||
"tabs",
|
||||
"http://localhost:5000/*"
|
||||
],
|
||||
"background": {
|
||||
"scripts": ["background.js"]
|
||||
},
|
||||
"content_scripts": [
|
||||
{
|
||||
"matches": ["*://*.youtube.com/*"],
|
||||
"js": ["content.js"]
|
||||
}
|
||||
]
|
||||
}
|
41
hotkey.js
Normal file
41
hotkey.js
Normal file
@@ -0,0 +1,41 @@
|
||||
nodes = document.querySelectorAll(":hover");
|
||||
i = 1;
|
||||
console.log(nodes);
|
||||
titleNode = nodes[nodes.length - i];
|
||||
|
||||
selector = "a#video-title-link";
|
||||
invidious = false;
|
||||
if (window.location.href.includes("invidious.site")) {
|
||||
selector = "a";
|
||||
invidious = true;
|
||||
}
|
||||
|
||||
while (titleNode && !titleNode.matches(selector)) {
|
||||
titleNode = titleNode.parentElement;
|
||||
console.log(titleNode);
|
||||
}
|
||||
if (!(titleNode && titleNode.matches(selector))) {
|
||||
console.error("No video element found.");
|
||||
} else {
|
||||
link = titleNode.href;
|
||||
if (link.startsWith("/")) {
|
||||
link = window.location.origin + link;
|
||||
}
|
||||
console.log(link);
|
||||
fetch("http://localhost:5000/download", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify({
|
||||
link: link,
|
||||
}),
|
||||
}).then((res) => {
|
||||
textNode = titleNode.querySelector("yt-formatted-string");
|
||||
if (invidious) {
|
||||
textNode = titleNode.querySelector("p");
|
||||
}
|
||||
textNode.style.setProperty("color", "green", "important");
|
||||
res.json().then((data) => console.log(data));
|
||||
});
|
||||
}
|
Reference in New Issue
Block a user