forked from jeffa/Media-Browser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmongo-to-mysql.pl
executable file
·223 lines (196 loc) · 5.88 KB
/
mongo-to-mysql.pl
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#!/usr/bin/env perl
use strict;
use warnings;
use Data::Dumper;
use open ':std', ':encoding(UTF-8)';
use lib 'lib';
use MovieUtil qw( get_raw_titles get_dbh );
my $dbh = get_dbh();
my $titles = get_raw_titles();
my $iter = $titles->find();
while (my $movie = $iter->next) {
my $title_id;
my $meta = $movie->{meta};
unless ($movie->{imdb_id} =~ /^tt\d{7}$/) {
warn "skipping $movie->{filename}: no IMDB ID\n";
next;
}
if (my ($sth) = $dbh->selectall_array( 'SELECT title_id from titles WHERE imdb_id = ?', undef, $movie->{imdb_id} )) {
$title_id = $sth->[0];
warn "$title_id: IMDB ID already processed\n";
if (my ($sth) = $dbh->selectall_array( 'SELECT file_id from files WHERE file_name = ?', undef, $movie->{filename} )) {
warn "$title_id: Filename already processed: $movie->{filename}\n";
next;
}
$dbh->do( 'INSERT INTO files(
file_name,
title_id
) VALUES (?, ? )', undef,
$movie->{filename},
$title_id
);
warn "$title_id: added file $movie->{filename}\n";
next;
}
# create sort
my $sort = $meta->{title};
$sort =~ s/^(?:A|An|The|La|Le)\b\s+//;
$sort =~ s/^\W+\b\s*//;
# insert title info
$dbh->do( 'INSERT INTO titles(
imdb_id,
title,
sort,
year,
kind,
ratio,
rating,
cover,
story,
tagline
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)', undef,
$movie->{imdb_id},
$meta->{title},
$sort,
$meta->{year},
$meta->{kind},
$meta->{ratio},
$meta->{rating},
$meta->{cover},
$meta->{story},
$meta->{tagline},
);
# obtain new title_id
$title_id = $dbh->{mysql_insertid};
warn "inserted $movie->{imdb_id}: new title ID $title_id\n";
# insert file using title_id
$dbh->do( 'INSERT INTO files(
file_name,
title_id
) VALUES (?, ?)', undef,
$movie->{filename},
$title_id
);
warn "$title_id: added file $movie->{filename}\n";
# insert duration(s) using title_id
my %unique_duration;
for my $duration (@{ $meta->{duration} }) {
if ($unique_duration{$duration}++) {
warn "$title_id: repeated duration: $duration\n";
} else {
$dbh->do( 'INSERT INTO durations(
duration,
title_id
) VALUES (?, ?)', undef,
$duration,
$title_id
);
}
}
# check genres
for my $genre (map lc($_), @{ $meta->{genres} }) {
my $genre_id;
my ($sth) = $dbh->selectall_array(
'SELECT genre_id from genres WHERE genre_name = ?',
undef, $genre
);
if ($sth->[0]) {
$genre_id = $sth->[0];
} else{
# insert any new genre
$dbh->do( 'INSERT INTO genres(genre_name)
VALUES (?)', undef,
$genre,
);
$genre_id = $dbh->{mysql_insertid};
warn "$title_id: added new genre $genre\n";
}
# insert genre_xref using title and genre ids
$dbh->do( 'INSERT INTO genre_xref(
genre_id,
title_id
) VALUES (?, ?)', undef,
$genre_id,
$title_id
);
warn "$title_id: added genre_xref $genre_id\n";
}
# check director names
for my $director (@{ $meta->{directors} }) {
my $name = $director->{name};
my $imdb_id = $director->{id};
my $person_id;
my ($sth) = $dbh->selectall_array(
'SELECT person_id from people WHERE imdb_id = ?',
undef, $imdb_id
);
if ($sth->[0]) {
$person_id = $sth->[0];
} else{
# insert any new names
$dbh->do( 'INSERT INTO people(imdb_id, person_name)
VALUES (?, ?)', undef,
$imdb_id,
$name,
);
$person_id = $dbh->{mysql_insertid};
warn "$title_id: added new person $name\n";
}
# insert roles_xref for director using title and name ids
eval {
$dbh->do( 'INSERT INTO role_xref(
role_id,
person_id,
title_id
) VALUES (?, ?, ?)', undef,
1,
$person_id,
$title_id
);
};
if ($@) {
warn "$@\n";
} else {
warn "$title_id: added role_xref $person_id\n";
}
}
# check writer names
for my $writer (@{ $meta->{writers} }) {
my $name = $writer->{name};
my $imdb_id = $writer->{id};
my $person_id;
my ($sth) = $dbh->selectall_array(
'SELECT person_id from people WHERE imdb_id = ?',
undef, $imdb_id
);
if ($sth->[0]) {
$person_id = $sth->[0];
} else{
# insert any new names
$dbh->do( 'INSERT INTO people(imdb_id, person_name)
VALUES (?, ?)', undef,
$imdb_id,
$name,
);
$person_id = $dbh->{mysql_insertid};
warn "$title_id: added new person $name\n";
}
# insert roles_xref for writer using title and name ids
eval {
$dbh->do( 'INSERT INTO role_xref(
role_id,
person_id,
title_id
) VALUES (?, ?, ?)', undef,
2,
$person_id,
$title_id
);
};
if ($@) {
warn "$@\n";
} else {
warn "$title_id: added role_xref $person_id\n";
}
}
}