Skip to content

Commit 7d30764

Browse files
committed
✨ feat(theBrain): new script for formatting YouTube thumbnails in TheBrain Notes
This commit introduces a new PowerShell script that scans for YouTube thumbnail URLs in the `Notes.md` files located under the Brain data folder. The script then backs up and modifies the `Notes.md` files to retrieve the YouTube thumbnails from the YouTube API instead of using local storage. This ensures that the YouTube thumbnails are always up-to-date and accessible. The script utilizes the `Get-ChildItem` and `Select-String` cmdlets to search for `Notes.md` files and match the YouTube thumbnail URL pattern. It performs various actions for each matching result, including creating backups, moving local image files, modifying the YouTube thumbnail URLs, and saving the modified `Notes.md` files. This new script enhances the functionality of the project by automating the process of updating YouTube thumbnails in TheBrain Notes.
1 parent 1fbe72e commit 7d30764

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<#
2+
.SYNOPSIS
3+
Format YouTube thumbnails in TheBrain Notes by replacing local image URLs
4+
withto web URLs.
5+
6+
.DESCRIPTION
7+
Scan for YouTube thumbnail URLs in the Notes.md files located under the Brain
8+
data folder, then back up and modify the Notes.md files to retrieve the
9+
YouTube thumbnails from the YouTube API instead of using local storage.
10+
11+
.PARAMETER None
12+
13+
.INPUTS
14+
None.
15+
16+
.OUTPUTS
17+
None
18+
19+
.EXAMPLE
20+
PS C:\> .\Format-TheBrainNotesYouTubeThumbnail.ps1
21+
22+
.NOTES
23+
Version: 1.0.0
24+
Author: chriskyfung
25+
License: GNU GPLv3 license
26+
#>
27+
28+
#Requires -Version 2.0
29+
30+
# Enable Verbose output
31+
[CmdletBinding()]
32+
33+
$ErrorActionPreference = "Stop"
34+
35+
# Look up the Notes.md files that locate under the Brain data folder and contain the YouTube thumbnail URLs.
36+
$BrainFolder = . "$PSScriptRoot\Get-TheBrainDataDirectory.ps1"
37+
$SubFolders = Get-ChildItem -Directory -Path $BrainFolder -Exclude 'Backup'
38+
$BackupFolder = Join-Path $BrainFolder 'Backup'
39+
40+
$Filename = 'Notes.md'
41+
$FilenameWithoutExtension = [System.IO.Path]::GetFileNameWithoutExtension($Filename)
42+
$FileExtension = [System.IO.Path]::GetExtension($Filename)
43+
44+
Write-Host 'Scanning YouTube thumbnail URLs in Brain Notes...'
45+
46+
$MatchInfo = Get-ChildItem -Path $SubFolders -Filter $Filename -Recurse | Select-String '\[\!\[(?<ALT>.*)\]\(\.data\/md-images\/(?<IMAGEFILE>.+)\.jpg(?<width>.*)\)\]\(https:\/\/youtu.be\/(?<VIDEOID>.+?)\)' -List
47+
48+
# For each matching result
49+
Write-Host 'Backing up and modifying Brain Notes...'
50+
ForEach ($Match in $MatchInfo) {
51+
$FilePath = $Match.Path | Convert-Path # FilePath of the Notes.md file
52+
$ParentFolder = Split-Path -Path $FilePath -Parent # Path of the parent folder
53+
$BackupLocation = $ParentFolder.Replace($BrainFolder, $BackupFolder)
54+
# Backup the Notes.md file
55+
$Timestamp = (Get-Item $FilePath).LastWriteTime.ToString('yyyyMMdd_HHmmss')
56+
$BackupFilename = "$FilenameWithoutExtension-$Timestamp$FileExtension~"
57+
$BackupPath = Join-Path $BackupLocation $BackupFilename
58+
Copy-Item -Path $FilePath -Destination (New-Item -ItemType File -Force -Path $BackupPath) -Force
59+
# Backup the md-image file
60+
$LocalImageFile = $Match.Matches[0].Groups['IMAGEFILE'].Value
61+
$LocalImagePath = Convert-Path "$ParentFolder/.data/md-images/$LocalImageFile.jpg"
62+
$BackupImagePath = $LocalImagePath.Replace($BrainFolder, $BackupFolder)
63+
Move-Item -Path $LocalImagePath -Destination (New-Item -ItemType File -Force -Path $BackupImagePath) -Force
64+
Write-Verbose "Created --> '$BackupPath'"
65+
# Amend the link of the YouTube thumbnail with UTF8 encoding
66+
$Pattern = $Match.Matches.Value
67+
$AltText = $Match.Matches[0].Groups['ALT'].Value
68+
$VideoId = $Match.Matches[0].Groups['VIDEOID'].Value
69+
$NewString = '[![{0}](https://img.youtube.com/vi/{1}/maxresdefault.jpg)](https://www.youtube.com/watch?v={1})' -f $AltText, $VideoId
70+
(Get-Content $FilePath -Encoding UTF8).Replace($Pattern, $NewString) | Set-Content $FilePath -Encoding UTF8
71+
Write-Verbose "Modified --> '$FilePath'"
72+
}
73+
74+
Write-Host ('Finished: {0} file(s) found' -f $MatchInfo.Length) # Output the number of files found
75+
76+
$MatchInfo | Format-Table Path | Out-Host # Output the path of the files found
77+
78+
Return

0 commit comments

Comments
 (0)