-
Notifications
You must be signed in to change notification settings - Fork 12
/
Merge-InputFiles.ps1
83 lines (66 loc) · 2.13 KB
/
Merge-InputFiles.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
$sourceFolder = "C:\Users\kristol\Desktop\movies\"
$targetFolder = "C:\Users\kristol\Desktop\movies\output\" #REMEMBER TO INCLUDE TRAILING BACK SLASH \ IN PATH
$number_of_files_to_distribute_across = 3
$count = 1
$files = Get-ChildItem ($sourceFolder + "*.json")
foreach ($file in $files)
{
$file.Name | Out-Default
$movie = $file | Get-Content | ConvertFrom-Json
# ID
$id = $movie.id
# TITLE
if ([string]::IsNullOrWhiteSpace($movie.title))
{
"Ignoring file " + $file.Name + " due to no title" | Out-Default
continue
}
$title = $movie.title
# YEAR
if ([string]::IsNullOrWhiteSpace($movie.release_date))
{
"Ignoring file " + $file.Name + " due to no release_date" | Out-Default
continue
}
$year = ($movie.release_date | Get-Date).Year
if ($movie.spoken_languages.Count -eq 0)
{
$spoken_languages = "unspecified"
}
else
{
$spoken_languages_array = @()
foreach ($language in $movie.spoken_languages)
{
$spoken_languages_array += $language.iso_639_1
}
$spoken_languages = $spoken_languages_array -join ','
}
# VOTE AVERAGE
$vote_average = $movie.vote_average.ToString([CultureInfo]::InvariantCulture);
# VOTE COUNT
$vote_count = $movie.vote_count
# GENRES
if ($movie.genres.Count -eq 0)
{
"Ignoring file " + $file.Name + " due to no genres" | Out-Default
continue
}
# Flatten structure and duplicate each movie per genre. I.e. a movie can be present several times in the output
# files if it is tagged with more than one genre
foreach ($genre in $movie.genres)
{
$row = @()
$row += $id
$row += $title
$row += $year
$row += $genre.name
$row += $spoken_languages
$row += $vote_average
$row += $vote_count
$outputfilename = "movies" + $count % $number_of_files_to_distribute_across + ".csv"
$targetFile = $targetFolder + $outputfilename
$row -join '|' | Out-File $targetFile -Append -Encoding ascii
$count = $count + 1
}
}