Public/Updates.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
using namespace System.Collections.Generic

function Get-MangaDexUpdates {
    [CmdletBinding(DefaultParameterSetName = 'Default')]
    Param(
        [Parameter(ParameterSetName = 'id', Mandatory = $true, HelpMessage = 'Enter the ID of each manga check updates for.', ValueFromPipeline = $true)]
        [long]$MangaId,

        [Parameter(ParameterSetName = 'id', Mandatory = $false, HelpMessage = 'Sends notice to pushbullet if present.')]
        [Parameter(ParameterSetName = 'Default', Mandatory = $false, HelpMessage = 'Enter the ID of each manga check updates for.')]
        [Switch]$PushBullet,

        [Parameter(ParameterSetName = 'id', Mandatory = $false, HelpMessage = 'Supress the output to PowerShell.')]
        [Parameter(ParameterSetName = 'Default', Mandatory = $false, HelpMessage = 'Supress the output to PowerShell.')]
        [Switch]$Quiet
    )

    Begin {
        if($PSCmdlet.ParameterSetName -eq 'Default') {
            (Get-IniContent -file $MDX_Manga).Keys | Get-MangaDexUpdates -Quiet:($Quiet.IsPresent) -PushBullet:($PushBullet.IsPresent)
        } else {
            $newChapters = [List[Object]]::New()
        }
    }

    Process {
        if($PSCmdlet.ParameterSetName -eq 'id') {
            $lastSeen = (Get-MangaDexManga -MangaID $MangaId).'latest chapter'
            $manga = Invoke-RestMethod -Uri ('https://mangadex.org/api/manga/{0}' -f $MangaId)
            $lastUploaded = $manga.chapter.PSobject.Properties | Where-Object {$_.MemberType -eq 'NoteProperty'} | Select-Object -First 1

            if($lastSeen -lt $lastUploaded.Value.chapter) {
                $newChapters.Add(
                    @(
                        $MangaId
                        $lastUploaded.Value.chapter
                    )
                )

                if(-not $Quiet) {
                    '{0} has been updated! New chapter: {1}' -f $manga.manga.title, $lastUploaded.Value.chapter
                }
                if($PushBullet) {
                    Send-MangaDexPushBullet -Title $manga.manga.title -Message ('Chapter {0} has been released!' -f $lastUploaded.Value.chapter)
                }
            }
        }
    }

    End {
        if($PSCmdlet.ParameterSetName -eq 'id') {
            $newContent = Get-Content -Path $MDX_Manga

            foreach ($chapter in $newChapters) {
                $oldLine = $newContent | Where-Object {$_ -eq ('[{0}]' -f $chapter[0])}
                $newContent[$newContent.IndexOf($oldLine) + 1] = 'latest_chapter={0}' -f $chapter[1]
            }

            Set-Content -Path $MDX_Manga -Value $newContent
        }
    }
}