-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPushFilesToPlex7.0.ps1
165 lines (150 loc) · 6.95 KB
/
PushFilesToPlex7.0.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
$Logfile = "C:\Logs\$($MyInvocation.MyCommand.Name).log"
$stopwatch = [system.diagnostics.stopwatch]::StartNew()
$count = 0
$totalSize = 0
$url = "http://www.omdbapi.com/?apikey=$($env:OMDBAPITOKEN)&t="
function LogWrite {
[CmdletBinding()]
param (
[Parameter(Mandatory)]
[string]$logString
)
Add-content $Logfile -value $logString
Write-Host $logString
}
LogWrite("`
#######################################################################`
###################### Start ######################`
#######################################################################`
")
LogWrite((Get-Date).toString("yyyy/MM/dd HH:mm:ss") + ": Script started")
function checkIfFileAlreadyOnFTP {
[CmdletBinding()]
param (
[Parameter(Mandatory)]
[string]$folder
)
LogWrite((Get-Date).toString("yyyy/MM/dd HH:mm:ss") + ": CHECKING IF FILE IS ON FTP: '<$folder>'")
$ftpCheckFiles = [System.Net.FtpWebRequest]::Create("ftp://###")
$ftpCheckFiles.Credentials = New-Object System.Net.NetworkCredential("###","###")
$ftpCheckFiles.Method = [System.Net.WebRequestMethods+Ftp]::ListDirectory
$ftpCheckFiles.GetResponse()
$ftpGR = $ftpCheckFiles.GetResponse()
$rs = $ftpGR.GetResponseStream()
$StreamReader = New-Object System.IO.Streamreader $rs
$filesOnFtp = @($StreamReader.ReadToEnd() -split [Environment]::NewLine) #this puts all the files in an array
LogWrite((Get-Date).toString("yyyy/MM/dd HH:mm:ss") + ": CLOSING FTP CONNECTION")
$ftpGR.Close()
if($filesOnFtp -contains $folder){
return $true
}else {
return $false
}
LogWrite((Get-Date).toString("yyyy/MM/dd HH:mm:ss") + ": FINISHED CHECKING IF ON FTP")
}
function movieNameAndYearSplit {
[CmdletBinding()]
param (
[Parameter(Mandatory)]
[string]$movieName
)
$nameAndYear = @()
$nameAndYear += ("$([regex]::Match("$movieName","(.*)(?=(\s\(\d{4}\)))").Value)") #matching name only
$nameAndYear += ("$([regex]::Match("$movieName","(?<=\()\d{4}(?=\))").Value)") #matching year only
LogWrite((Get-Date).toString("yyyy/MM/dd HH:mm:ss") + ": Split Name and Year: '<$nameAndYear>'")
return $nameAndYear
}
function callAPIReturnImdb {
[CmdletBinding()]
param (
[Parameter(Mandatory)]
[string]$name
)
$val = movieNameAndYearSplit $name
$uri = $url + "$($val[0])&y=$($val[1])"
$result = Invoke-RestMethod -Method Get -Uri $uri
LogWrite((Get-Date).toString("yyyy/MM/dd HH:mm:ss") + ": Made API call and return imdbID: '<$($result.imdbID)>'")
#return "https://www.imdb.com/title/" + $result.imdbID + "/"
return $result.imdbID
}
function sendEmail {
[CmdletBinding()]
param (
[Parameter(Mandatory)]
[string[]]$movieList
)
$body = "<ul> </br>"
foreach ($movie in $movieList) {
$movieImdbID = callAPIReturnImdb $movie
$body += "<li> <a href='https://www.imdb.com/title/" + $movieImdbID + "' target = _blank>" + $movie + "</a> </li> </br>"
}
$body += "</ul>"
Send-MailMessage -SmtpServer "ESXi-WinMail" -Bcc ("###") -To ("###") -From ("###") -Subject "New Movie(s) Uploaded!" -Body $body -BodyAsHtml
LogWrite((Get-Date).toString("yyyy/MM/dd HH:mm:ss") + ": EMAIL SENT")
}
#renaming all files that have a "[" or "]" in the name as powershell is stupid and doesn't like it when uploading files
$dir = @(Get-ChildItem "F:\FrostWire\FrostWire 6\Data" -Directory)
Try{
foreach($item in $dir){
Rename-Item -LiteralPath $item.FullName -NewName ($item.name -replace "\.",' '`
-replace "(?<=\(\d{4}\))(.*)",''`
-replace "[\[\]]",'' `
-replace " H264 AAC-RARBG",''`
) -ErrorAction Continue
LogWrite((Get-Date).toString("yyyy/MM/dd HH:mm:ss") + ": RENAMED ITEM: '<$item>'")
}
}
Catch{
LogWrite((Get-Date).toString("yyyy/MM/dd HH:mm:ss") + ": ERROR OCCURRED: $_.Exception.Message")
}
#script that uploads entire folders and its sub-files
$FromDir_SubDir = @(Get-ChildItem "F:\FrostWire\FrostWire 6\Data" -Directory)
$ftp = "ftp://###"
Try{
$movieEmailList = @()
foreach ($folder in $FromDir_SubDir) {
$ftp2 = $ftp + "/$folder"
$files = @(Get-ChildItem $folder.FullName -Recurse -File)
if ((checkIfFileAlreadyOnFTP $folder) -eq $true){
LogWrite((Get-Date).toString("yyyy/MM/dd HH:mm:ss") + ": FOLDER ALREADY ON FTP...SKIPPING AND REMOVING '<$folder>'")
rmdir $folder.FullName -Force -Recurse
continue
}else{
LogWrite((Get-Date).toString("yyyy/MM/dd HH:mm:ss") + ": FOLDER NOT ON FTP...UPLOADING '<$folder>'")
$makeDir = [System.Net.WebRequest]::Create($ftp2)
#$makeDir.Credentials = New-Object System.Net.NetworkCredential($user,$pass)
$makeDir.Method = [System.Net.WebRequestMethods+FTP]::MakeDirectory
$makeDir.GetResponse()
LogWrite((Get-Date).toString("yyyy/MM/dd HH:mm:ss") + ": CREATED FOLDER ON FTP: <$folder>")
$movieEmailList += $folder.Name
Copy-Item $folder.FullName -Destination "T:\Movies\" -Recurse -Force
LogWrite((Get-Date).toString("yyyy/MM/dd HH:mm:ss") + ": COPIED FOLDER to BACKUP DRIVE T:\Movies: '<$($folder.Name)>'")
foreach($file in $files){
$webclient = New-Object -TypeName System.Net.WebClient
$uri = New-Object -TypeName System.Uri -ArgumentList "$ftp2/$($file.Name)"
$webclient.UploadFile("$uri", $file.FullName)
LogWrite((Get-Date).toString("yyyy/MM/dd HH:mm:ss") + ": SUCCESSFULLY UPLOADED FILE to FTP: '<$($file)>' to folder: '<$folder>'")
$count += 1
$totalSize += $file.Length/1MB
Remove-Item $file.FullName -Recurse -Force
}
rmdir $folder.FullName -Force -Recurse
$res = $makeDir.GetResponse()
LogWrite((Get-Date).toString("yyyy/MM/dd HH:mm:ss") + ": CLOSING FTP CONNECTION")
$res.Close()
}
}
if ($movieEmailList.Length -ne 0){
sendEmail $movieEmailList
}
}
Catch{
LogWrite((Get-Date).toString("yyyy/MM/dd HH:mm:ss") + ": ERROR OCCURRED: $_.Exception.Message")
}
$stopwatch.Stop()
LogWrite((Get-Date).toString("yyyy/MM/dd HH:mm:ss") + ": Uploaded <$($count)> items in <$($stopwatch.Elapsed.TotalSeconds)> seconds of size <$($totalSize)> MB at avg speed of <$($totalSize/$stopwatch.Elapsed.TotalSeconds)> MB/s")
LogWrite((Get-Date).toString("yyyy/MM/dd HH:mm:ss") + ": Script ended")
LogWrite("`
#######################################################################`
####################### End #######################`
#######################################################################")