-
Notifications
You must be signed in to change notification settings - Fork 2
/
doc-downloader.ps1
114 lines (95 loc) · 4.73 KB
/
doc-downloader.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
$Config = Get-Content "configurations.json" | ConvertFrom-Json
### Configuration variables found in configurations.json
$API_KEY = $Config.api_key
$BASE_URL = $Config.base_url
$DRIVE_AXLE = $Config.drive_axle_customer
# Eleos Document Downloader Script
# This script fetches queued documents from the Eleos API and downloads them into a folder on local machine
# Import functions from functions File
$here = (Split-Path -Parent $MyInvocation.MyCommand.Path)
. $here\doc-downloader.functions.ps1
#----------------------------------------------------------------------------------------------------------
# Constants
#----------------------------------------------------------------------------------------------------------
$DATED_FOLDERS = $true #if set to true, files will be downloaded into dated folders
$DESTINATION_PATH = "C:\Eleos\" # Desired destination folder for the downloaded files
$LOG_DIR = "C:\Eleos\Logs\" #location for the dated log files
$date = (Get-Date -Format "MM-dd").ToString()
If($DATED_FOLDERS) {
$FILE_DIR = $DESTINATION_PATH + $date + "\"
}
Else {
$FILE_DIR = $DESTINATION_PATH}
$DRIVE_AXLE_HEADERS = @{ Authorization = ("DriveAxleKey key=" + $API_KEY)
Accept = 'application/json'
ContentType = 'application/json'}
$ELEOS_HEADERS = @{ Authorization = ("Key key=" + $API_KEY)
Accept = 'application/json'
ContentType = 'application/json'}
$HEADERS = If ($DRIVE_AXLE) { $DRIVE_AXLE_HEADERS } Else { $ELEOS_HEADERS }
#----------------------------------------------------------------------------------------------------------
# Functions are defined in doc-downloader.functions.ps1
#----------------------------------------------------------------------------------------------------------
#----------------------------------------------------------------------------------------------------------
# Main Script
#----------------------------------------------------------------------------------------------------------
# Checks and Creates Directory if it does not exist
CheckDirectory $DESTINATION_PATH
CheckDirectory $LOG_DIR
CheckDirectory $FILE_DIR
# Creates LOG File
$log_file = (CreateLogFile $LOG_DIR)
# Creates Timestamp for LOG file
$Timestamp = Get-Date -format "dd-MM-yyyy HH:mm:s"
WriteToLog ("Script Executed at: " + $Timestamp + "`r`n") $log_file
# Starts Timer for LOG file
$Timer = [System.Diagnostics.Stopwatch]::StartNew()
$file_count = 0
do{
$URI = $BASE_URL + "/api/v1/documents/queued/next"
WriteToLog ("Calling " + $URI + "`r`n") $log_file
try{
WriteToLog "Getting Next Document in Queue..." $log_file
$response = GetNextDoc $URI $HEADERS $log_file
If ($response.StatusCode -eq 302){
WriteToLog "Found Document in Queue..." $log_file
$redirect = $BASE_URL + $response.Headers["Location"]
WriteToLog ("Redirecting to URL " + $redirect) $log_file
$queuedDoc = GetDocFromQueue $redirect $HEADERS $log_file
$queuedDoc = $queuedDoc | ConvertFrom-Json
$downloadURI = $queuedDoc.download_url
WriteToLog ("Downloading Document from " + $downloadURI) $log_file
$file_count++
try{
$filename = GetFilename $downloadURI $file_count $log_file
WriteToLog ("Downloading file " + $filename + " ...." + "`r`n") $log_file
DownloadFile $downloadURI $filename $FILE_DIR $log_file
}
catch{
WriteToLog ($_.Exception.Message + "`r`n" + "Error Occured at: " + (Get-Date -format "MM-dd-yyyy HH:mm:s").ToString() + "`r`n") $log_file
break
}
WriteToLog ("Attempting to delete document " + $redirect + " from the Queue`r`n") $log_file
$removeDoc = RemoveDocFromQueue $redirect $HEADERS $log_file
If ($removeDoc){
WriteToLog ("Document Removed from Queue with Status Code: " + $removeDoc.StatusCode + "`r`n") $log_file
}
Else{
WriteToLog ("Error Removing Document from the Queue after several attempts`r`n") $log_file
}
}
Else{
WriteToLog ("No Documents in Queue: Response returned " + $response.StatusCode + "`r`n") $log_file
}
}
catch [System.Net.WebException]{
WriteToLog ("An exception has occured: " + $_.Exception.Message + "`r`n") $log_file
}
WriteToLog("------------------------") $log_file
}
while($response.StatusCode -eq 302)
# Ends Timer for LOG file
$Timer.Stop()
WriteToLog("------------------------") $log_file
WriteToLog ("Script total run time: " + $Timer.Elapsed.ToString()) $log_file
WriteToLog ($file_count.ToString() + " documents downloaded." + "`r`n") $log_file