Compare commits

...

5 Commits

Author SHA1 Message Date
PhatPhuckDave
7e12d9e939 Enable CORS 2024-07-21 18:59:57 +02:00
PhatPhuckDave
38449a7676 Add hotkey for download 2024-07-21 18:56:11 +02:00
PhatPhuckDave
7cf0cf5033 Add no workey extension 2024-07-21 18:56:08 +02:00
c95613e240 Merge branch 'rework' 2024-07-16 17:53:20 +02:00
a7babdbba4 Fix issues with slow 2024-06-17 16:14:46 +02:00
7 changed files with 159 additions and 15 deletions

33
download.go Normal file
View 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)
}

View File

@@ -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
View 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
View 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
View 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"]
}
]
}

26
hotkey.js Normal file
View File

@@ -0,0 +1,26 @@
nodes = document.querySelectorAll(":hover");
i = 1;
console.log(nodes);
videoNode = nodes[nodes.length - i];
while (videoNode && videoNode.tagName != "YTD-RICH-ITEM-RENDERER") {
videoNode = videoNode.parentElement;
console.log(videoNode);
}
if (!(videoNode && videoNode.tagName == "YTD-RICH-ITEM-RENDERER")) {
console.error("No video element found.");
} else {
link = videoNode.querySelector("a#video-title-link").href;
fetch("http://localhost:5000/download", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
link: link,
}),
}).then((res) => {
console.log(res);
res.json().then((data) => console.log(data));
});
}