Skip to content

Instantly share code, notes, and snippets.

@Fabaderheld
Last active July 30, 2023 04:36
Show Gist options
  • Select an option

  • Save Fabaderheld/4dc9bdbdf2f5d74e30cd1dfe8c634b0c to your computer and use it in GitHub Desktop.

Select an option

Save Fabaderheld/4dc9bdbdf2f5d74e30cd1dfe8c634b0c to your computer and use it in GitHub Desktop.
Open Metadata from an CBZ file in Powershell

function Get-ComicInfo { [CmdletBinding()] param ( [Parameter()] [String] [Alias("Name", "ComicFile")] $Path )

# If the path starts with "./", replace it with the current working directory ($PWD)
if ($Path -like "./*") {
    $Path = $Path.Replace("./", "$PWD/")
}

# Add the necessary assembly to work with zip files
Add-Type -assembly "system.io.compression.filesystem"

# Open the zip file for reading
$zip = [io.compression.zipfile]::OpenRead($Path)

# Find the ComicInfo.xml file within the zip archive
$file = $zip.Entries | Where-Object { $_.Name -eq "ComicInfo.xml" }

if ($file -eq $null) {
    Write-Warning "No ComicInfo.xml file found in $Path"
}
else {
    # Open a stream to read the contents of the ComicInfo.xml file
    $stream = $file.Open()

    # Create a reader to read the contents of the stream
    $reader = New-Object IO.StreamReader($stream)

    # Read the entire contents of the stream
    $text = $reader.ReadToEnd()

    # Create an XML document object and load the XML content
    $xmlDocument = New-Object -TypeName System.Xml.XmlDocument
    $xmlDocument.LoadXml($text)

    # Get the ComicInfo node from the XML document
    $ComicInfo = $xmlDocument.ComicInfo

    # Close the reader, stream, and dispose of the zip object
    $reader.Close()
    $stream.Close()
    $zip.Dispose()

    # Return the ComicInfo node
    $ComicInfo

}

}

function Get-ComicFromComicvine { [CmdletBinding()] param ( [Parameter()] [String] $APIKey, [Parameter()] [String] [Alias("ID")] $IssueID )

# Check if APIKey parameter is provided
if ($APIKey -eq $null) {
    Write-Error "No APIKey provided"
    exit 1
}
else {
    $APIKey = $APIKey
}

# Check if ComicVineAPIKey is available in environment variables
if ($Null -eq $env:ComicVineAPIKey) {
    Write-Error "No ComicVineAPIKey found in environment variables"
    exit 1
}
else {
    $APIKey = $env:ComicVineAPIKey
}

# Check if IssueID parameter is provided
if ($IssueID -eq $null) {
    Write-Error "No IssueID provided"
    exit 1
}
else {
    $IssueID = $IssueID
}

## Extract ID from ComicInfo.xml
# $ComicInfo.Web.split("/")[-2]

# Invoke the Comic Vine API to retrieve information about the issue using the provided APIKey and IssueID
((Invoke-RestMethod -Uri "https://comicvine.gamespot.com/api/issue/$IssueID/?api_key=$APIKey&format=json").results)

}

function Get-VolumeFromComicvine { [CmdletBinding()] param ( [Parameter()] [String] $APIKey, [Parameter()] [String] [Alias("ID")] $VolumeID, [Parameter()] [String] [Alias("SeriesInfoFile")] $Path )

# If the path starts with "./", replace it with the current working directory ($PWD)
if ($Path -like "./*") {
    $Path = $Path.Replace("./", "$PWD/")
}

# Check if APIKey parameter is provided
if ($APIKey -eq $null) {
    Write-Error "No APIKey provided"
    exit 1
}
else {
    $APIKey = $APIKey
}

# Check if ComicVineAPIKey is available in environment variables
if ($Null -eq $env:ComicVineAPIKey) {
    Write-Error "No ComicVineAPIKey found in environment variables"
    exit 1
}
else {
    $APIKey = $env:ComicVineAPIKey
}

# Check if VolumeID and SeriesInfoFile parameters are provided
if ($Null -eq $VolumeID -and $Null -ne $Path) {
    Write-Error "No VolumeID provided and no SeriesInfoFile provided"
    exit 1
}
elseif ($Null -eq $VolumeID) {
    # Read the SeriesInfoFile and extract the VolumeID
    $SeriesInfo = (Get-Content $Path | ConvertFrom-Json).Metadata
    $VolumeID = $SeriesInfo.Comicid
}
elseif ($Null -ne $Path) {
    Write-Error "No SeriesInfoFile provided"
}

# Invoke the Comic Vine API to retrieve information about the volume using the provided APIKey and VolumeID
((Invoke-RestMethod -Uri "https://comicvine.gamespot.com/api/issue/4050-$VolumeID/?api_key=$APIKey&format=json").results)

}

function Get-ComicvineInfo { param ( $File )

comictagger -s -t cr -o -f $File

}

$Comics = Get-ChildItem ...

foreach ($Comic in $Comics) {

$ComicInfo = Get-ComicInfo $Comic.Fullname -WarningAction SilentlyContinue

if ($Null -eq $Comicinfo) {

    Get-ComicvineInfo -File $Comic.Fullname

}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment