ozdemir08 / youtube-video-summarizer

MIT License
25 stars 4 forks source link

Additional Settings for date, frontmatter, title, thumbnail, description, and summary #4

Open rshmhrj opened 4 months ago

rshmhrj commented 4 months ago

I have scrambled together a youtube template which uses Templater. QuickAdd allows me to create a new note with the video on my clipboard. I wanted to use the summarizer plugin immediately afterward to add the summary (as the URL is still on my clipboard).

The problem is that now, I end up having duplicate metadata.

It would be great to have settings for:

For reference, here's the content of my template:

<%*  
let url = await tp.system.clipboard();
let page = await tp.obsidian.request({url});
let p = new DOMParser();
let doc = p.parseFromString(page, "text/html");
let $ = s => doc.querySelector(s);

let qcFileName1 = $("meta[property='og:title']").content;
let qcFileName = qcFileName1.replace(/:/g, "_");
let title = "🎬 " + qcFileName;

await tp.file.move("/3_resources 📚/videos 🎥/" + title);
let duration2 = $("meta[itemprop='duration']").content.slice(2);
let duration1 = duration2.replace(/M/gi, " min ");
let duration = duration1.replace(/S/gi, " sec ");

const html = await page.toString();
const matches = html.match(/ytInitialPlayerResponse\s*=\s*({.+?})\s*;\s*(?:var\s+meta|<\/script|\n)/);
const json = JSON.parse(matches[1]);
const videoDetails = json.videoDetails;
const keywords = videoDetails.keywords;
const description = videoDetails.shortDescription;
const author = videoDetails.author;
const channelId = videoDetails.channelId;
const channelUrl = `https://www.youtube.com/channel/${channelId}`
const viewCount = videoDetails.viewCount;
const views = parseInt(viewCount).toLocaleString('en-US');
const thumbs = videoDetails.thumbnail.thumbnails
const thumbnail_large = thumbs.pop().url;
const thumbnail_small = thumbs.pop().url;

let tags = ["videos"]
if (keywords != null) {
    keywords.forEach( (tag) => {
        let t = tag.split(' ').join('_');
        let firstChar = t.charAt(0);
        if (firstChar <= '9' && firstChar >= '0') {
            t = "_" + t;
        }
    tags.push(t);
    });
}

setTimeout(() => {
  app.fileManager.processFrontMatter(tp.config.target_file, frontmatter => {
      frontmatter["title"] = qcFileName;
      frontmatter["url"] = url;
      frontmatter["date"] = tp.file.creation_date();
      frontmatter["tags"] = tags;
  })
}, 200)
%>  
--- 

![thumbnail](<% thumbnail_large %>)

- `channel:` [<% author %>](<% channelUrl %>)
- `ref:` <% $("meta[itemprop='description']").content %> 
- `publish date:` <% 
$("meta[itemprop='uploadDate']").content.slice(0, 10) %> 
- `len`: <% duration %> 
- `views`: <% views %>
- `status`: pending

### Description
<% description %>

### Notes
<% tp.file.cursor() %>

And here is the output after running both the template and the summarizer back-to-back:

image image image

Here is the youtube link I used for testing: https://www.youtube.com/watch?v=jzZsG8n2R9A

mariusursache commented 3 months ago

Very good suggestions, but it’s a huge feature. Maybe split this in smaller issues?

rshmhrj commented 3 months ago

Hi @ozdemir08 and @mariusursache

Created PR #9 with a few additional settings.

I realized the toggling of all the various flags would be unnecessary if I could just update the template directly, so the main addition was a template formatting text area in settings.

Also took the opportunity (based on the references of a few plugins I use with nice settings panels), to consolidate some constants as well as the ability to reset the settings back to default.

Lastly, added some minor logic to accept both fully formed URLs as well as IDs only (e.g. https://www.youtube.com/watch?v=nC7rpDXa4B8 and nC7rpDXa4B8 should both be accepted in the popup modal).

Please review and let me know if there is anything that should be fixed or updated. This is my first time at trying anything to do with Obsidian plugins, so hope there aren't many mistakes!

ozdemir08 commented 3 months ago

Thanks @rshmhrj for the suggestions and the PR. I will review it by tomorrow!