Public/Manga.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
function Add-MangaDexManga {
    [CmdletBinding(DefaultParameterSetName = 'Default')]
    Param
    (
        [Parameter(ParameterSetName = 'Default', Mandatory = $true, HelpMessage = 'Enter the ID of each manga to monitor.', ValueFromPipeline = $true)]
        [long]$MangaId

        # [Parameter(ParameterSetName = 'Import', Mandatory = $false, HelpMessage = 'Enter the ID of each manga to monitor.', ValueFromPipeline = $false)]
        # [Switch]$ImportFromMangaDex
    )

    Begin {
        $currentManga = Get-IniContent -FilePath $MDX_Manga
        $newManga = @{}

        # if($ImportFromMangaDex) {
        # $website = Invoke-WebRequest -Uri 'https://mangadex.org/login' -SessionVariable mangaDex
        # $loginForm = $website.Forms['login_form']

        # $loginForm.Fields['login_username'] = $MDX_Username
        # $loginForm.Fields['login_password'] = [PSCredential]::new('null', $MDX_Password).GetNetworkCredential().Password

        # Invoke-WebRequest -Uri ('https://mangadex.org' + $loginForm.Action) -WebSession $mangaDex -Method POST -Body $loginForm.Fields
        # $reading = Invoke-WebRequest -Uri ('https://mangadex.org/follows/manga/1') -WebSession $mangaDex -Method GET
        # $foundMangaIDs = ((($reading.Links | Where-Object {$_.href -match '/title/[0-9]+/.+' -and $_.innerText}).href | Select-String '/title/([0-9]+)/.+').Matches.Groups | Where-Object Name -eq 1).Value

        # $foundMangaIDs | Add-MangaDexManga
        # }
    }

    Process {
        if($PSCmdlet.ParameterSetName -eq 'Default' -and -not $currentManga["$MangaId"]) {
            $manga = Invoke-RestMethod -Uri ('https://mangadex.org/api/manga/{0}' -f $MangaId)
            $newManga["$MangaId"] = @{
                name = $manga.manga.title
                latest_chapter = ($manga.chapter.PSobject.Properties | Where-Object {$_.MemberType -eq 'NoteProperty'}).Value.Chapter | Select-Object -First 1
            }
        }
    }

    End {
        if($newManga.Count) {
            Out-IniFile -FilePath $MDX_Manga -InputObject $newManga
        }
    }
}

function Get-MangaDexManga {
    Param (
        [long]$MangaID
    )

    $currentManga = Get-IniContent -FilePath $MDX_Manga

    if($MangaID) {
        return [PSCustomObject]@{
            id = $MangaID
            'latest chapter' = $currentManga["$MangaID"].latest_chapter
            name = $currentManga["$MangaID"].Name
        }
    } else {
        $returnObject = [System.Collections.Generic.List[System.Object]]::new()
        foreach($key in $currentManga.Keys) {
            $returnObject.Add([PSCustomObject]@{
                id = $key
                'latest chapter' = $currentManga[$key].latest_chapter
                name = $currentManga[$key].Name
            })
        }
        return $returnObject
    }
}

function Remove-MangaDexManga {
    [CmdletBinding(DefaultParameterSetName = 'Id')]
    param (
        [Parameter(ParameterSetName = 'Id', Mandatory = $true, HelpMessage = 'Enter the ID of each manga to remove.', ValueFromPipeline = $true)]
        $id,
        [Parameter(ParameterSetName = 'Name', Mandatory = $true, HelpMessage = 'Enter the name of each manga to remove.', ValueFromPipeline = $true)]
        $Name
    )

    Begin {
        $update = $false
        $currentManga = Get-IniContent -FilePath $MDX_Manga
    }

    Process {
        if($PSCmdlet.ParameterSetName -eq 'Id') {
            $currentManga.Remove("$id")
            $update = $true
        } elseif($PSCmdlet.ParameterSetName -eq 'Name') {
            foreach($Key in ($currentManga.GetEnumerator() | Where-Object {$_.Value.Name -eq $Name})) {
                $currentManga.Remove('{0}' -f $Key.Name)
                $update = $true
            }
        }
    }

    End {
        if($update) {
            $null | Out-File -FilePath $MDX_Manga
            Out-IniFile -FilePath $MDX_Manga -InputObject $currentManga
        }
    }
}