9 Commits

Author SHA1 Message Date
ca3c3423b1 Discover go install 2024-10-03 10:53:35 +02:00
0ce14bdc53 Update hotkey 2024-08-17 15:12:01 +02:00
PhatPhuckDave
676748f614 Make text green when download started
Finally, feedback
2024-07-22 15:45:53 +02:00
PhatPhuckDave
b92a325b99 Update hotkey to work with any youtube page (hopefully) 2024-07-21 23:29:24 +02:00
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
9 changed files with 175 additions and 17 deletions

View File

@@ -1,3 +1,3 @@
module main module dl
go 1.22.4 go 1.22.4

View File

@@ -1 +0,0 @@
main.exe,"C:\Program Files\Git\usr\bin\dl.exe",t

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 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() { func main() {
downloadQueue := make(chan *DownloadTask, 100)
for i := 0; i < DOWNLOAD_WORKERS; i++ { for i := 0; i < DOWNLOAD_WORKERS; i++ {
worker := &DownloadWorker{id: i, input: downloadQueue} worker := &DownloadWorker{id: i, input: downloadQueue}
go worker.Run() go worker.Run()
} }
http.HandleFunc("/download", func(responseWriter http.ResponseWriter, request *http.Request) { mux := http.NewServeMux()
defer request.Body.Close() mux.Handle("/download", enableCORS(http.HandlerFunc(handleDownload)))
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}
})
log.Println("Server starting on :5000") log.Println("Server starting on :5000")
err := http.ListenAndServe(":5000", nil) err := http.ListenAndServe(":5000", mux)
if err != nil { if err != nil {
log.Println("Error starting server:", err) 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"]
}
]
}

41
hotkey.js Normal file
View 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));
});
}