Skip to content

✨Feature: Add script for formatting YouTube thumbnails in TheBrain Notes #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions theBrain/Format-TheBrainNotesYouTubeThumbnail.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<#
.SYNOPSIS
Format YouTube thumbnails in TheBrain Notes by replacing local image URLs
withto web URLs.

.DESCRIPTION
Scan for YouTube thumbnail URLs in the Notes.md files located under the Brain
data folder, then back up and modify the Notes.md files to retrieve the
YouTube thumbnails from the YouTube API instead of using local storage.

.PARAMETER None

.INPUTS
None.

.OUTPUTS
None

.EXAMPLE
PS C:\> .\Format-TheBrainNotesYouTubeThumbnail.ps1

.NOTES
Version: 1.0.0
Author: chriskyfung
License: GNU GPLv3 license
#>

#Requires -Version 2.0

# Enable Verbose output
[CmdletBinding()]

$ErrorActionPreference = "Stop"

# Look up the Notes.md files that locate under the Brain data folder and contain the YouTube thumbnail URLs.
$BrainFolder = . "$PSScriptRoot\Get-TheBrainDataDirectory.ps1"
$SubFolders = Get-ChildItem -Directory -Path $BrainFolder -Exclude 'Backup'
$BackupFolder = Join-Path $BrainFolder 'Backup'

$Filename = 'Notes.md'
$FilenameWithoutExtension = [System.IO.Path]::GetFileNameWithoutExtension($Filename)
$FileExtension = [System.IO.Path]::GetExtension($Filename)

Write-Host 'Scanning YouTube thumbnail URLs in Brain Notes...'

$MatchInfo = Get-ChildItem -Path $SubFolders -Filter $Filename -Recurse | Select-String '\[\!\[(?<ALT>.*)\]\(\.data\/md-images\/(?<IMAGEFILE>.+)\.jpg(?<width>.*)\)\]\(https:\/\/youtu.be\/(?<VIDEOID>.+?)\)' -List

# For each matching result
Write-Host 'Backing up and modifying Brain Notes...'
ForEach ($Match in $MatchInfo) {
$FilePath = $Match.Path | Convert-Path # FilePath of the Notes.md file
$ParentFolder = Split-Path -Path $FilePath -Parent # Path of the parent folder
$BackupLocation = $ParentFolder.Replace($BrainFolder, $BackupFolder)
# Backup the Notes.md file
$Timestamp = (Get-Item $FilePath).LastWriteTime.ToString('yyyyMMdd_HHmmss')
$BackupFilename = "$FilenameWithoutExtension-$Timestamp$FileExtension~"
$BackupPath = Join-Path $BackupLocation $BackupFilename
Copy-Item -Path $FilePath -Destination (New-Item -ItemType File -Force -Path $BackupPath) -Force
# Backup the md-image file
$LocalImageFile = $Match.Matches[0].Groups['IMAGEFILE'].Value
$LocalImagePath = Convert-Path "$ParentFolder/.data/md-images/$LocalImageFile.jpg"
$BackupImagePath = $LocalImagePath.Replace($BrainFolder, $BackupFolder)
Move-Item -Path $LocalImagePath -Destination (New-Item -ItemType File -Force -Path $BackupImagePath) -Force
Write-Verbose "Created --> '$BackupPath'"
# Amend the link of the YouTube thumbnail with UTF8 encoding
$Pattern = $Match.Matches.Value
$AltText = $Match.Matches[0].Groups['ALT'].Value
$VideoId = $Match.Matches[0].Groups['VIDEOID'].Value
$NewString = '[![{0}](https://img.youtube.com/vi/{1}/maxresdefault.jpg)](https://www.youtube.com/watch?v={1})' -f $AltText, $VideoId
(Get-Content $FilePath -Encoding UTF8).Replace($Pattern, $NewString) | Set-Content $FilePath -Encoding UTF8
Write-Verbose "Modified --> '$FilePath'"
}

Write-Host ('Finished: {0} file(s) found' -f $MatchInfo.Length) # Output the number of files found

$MatchInfo | Format-Table Path | Out-Host # Output the path of the files found

Return
33 changes: 26 additions & 7 deletions theBrain/Resize-TheBrainNotesYouTubeThumbnail.ps1
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<#
.SYNOPSIS
Resize YouTube thumbnails down to 30% in theBrain Notes
Resize YouTube thumbnails in TheBrain Notes.

.DESCRIPTION
Scan all Markdown files in the user's Brain data directory, and apends `#$width=30p$`
Scan all Markdown files in the user's Brain data directory, and apends `#$width=50p$`
to the image URL of embedded YouTube thumbnails within the Markdown files, and backs
up the original notes to the Backup folder before changing the Markdown file content.

Expand All @@ -19,7 +19,7 @@
PS C:\> .\Resize-TheBrainNotesYouTubeThumbnail.ps1

.NOTES
Version: 2.0.0
Version: 2.1.0
Author: chriskyfung
License: GNU GPLv3 license
Original from: https://gist.github.com/chriskyfung/ff65df9a60a7a544ff12aa8f810d728a/
Expand All @@ -32,6 +32,16 @@

$ErrorActionPreference = "Stop"

<#
.PARAMETERS
Depending on the $ImageType parameter, it either finds the default YouTube
thumbnails or the resized ones. If the $ImageType is 'resized', it also takes
into account the $CurrentWidth parameter to find thumbnails of a specific width.
#>
$ImageType = 'default' # [ValidateSet('default', 'resized')]
$CurrentWidth = 30 # [ValidateRange(1,100)]
$NewWidth = 50 # [ValidateRange(1,100)]

# Look up the Notes.md files that locate under the Brain data folder and contain the YouTube thumbnail URLs.
$BrainFolder = . "$PSScriptRoot\Get-TheBrainDataDirectory.ps1"
$SubFolders = Get-ChildItem -Directory -Path $BrainFolder -Exclude 'Backup'
Expand All @@ -42,10 +52,15 @@ $FilenameWithoutExtension = [System.IO.Path]::GetFileNameWithoutExtension($Filen
$FileExtension = [System.IO.Path]::GetExtension($Filename)

Write-Host 'Scanning YouTube thumbnail URLs in Brain Notes...'
$MatchInfo = Get-ChildItem -Path $SubFolders -Filter $Filename -Recurse | Select-String '\/(hq|maxres)default.jpg\)' -List

if ($ImageType -eq 'default') {
$MatchInfo = Get-ChildItem -Path $SubFolders -Filter $Filename -Recurse | Select-String '\/(hq|maxres)default.jpg\)' -List
} else {
$MatchInfo = Get-ChildItem -Path $SubFolders -Filter $Filename -Recurse | Select-String ('\/(hq|maxres)default.jpg#\$width={0}p\$\)' -f $CurrentWidth) -List
}

# For each matching result
Write-Information 'Backing up and modifying Brain Notes...'
Write-Host 'Backing up and modifying Brain Notes...'
ForEach ($Match in $MatchInfo) {
$FilePath = $Match.Path | Convert-Path # FilePath of the Notes.md file
$ParentFolder = Split-Path -Path $FilePath -Parent # Path of the parent folder
Expand All @@ -57,12 +72,16 @@ ForEach ($Match in $MatchInfo) {
Write-Verbose "Created --> '$BackupPath'"
# Amend the link of the YouTube thumbnail with UTF8 encoding
$Pattern = $Match.Matches.Value
$NewString = $Pattern.Replace(')', '#$width=30p$)')
if ($ImageType -eq 'default') {
$NewString = $Pattern.Replace(')', '#$width={0}p$)' -f $NewWidth)
} else {
$NewString = $Pattern.Replace('#$width=30p$)', '#$width={0}p$)' -f $NewWidth)
}
(Get-Content $FilePath -Encoding UTF8).Replace($Pattern, $NewString) | Set-Content $FilePath -Encoding UTF8
Write-Verbose "Modified --> '$FilePath'"
}

Write-Host 'Finished: ' $MatchInfo.Length 'file(s) found' # Output the number of files found
Write-Host ('Finished: {0} file(s) found' -f $MatchInfo.Length) # Output the number of files found

$MatchInfo | Format-Table Path | Out-Host # Output the path of the files found

Expand Down