-
Notifications
You must be signed in to change notification settings - Fork 0
/
scraper.mm
executable file
·1994 lines (1564 loc) · 62.2 KB
/
scraper.mm
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// SublerCLI.mm
// Subler
//
// Copyright 2009-2013 Damiano Galassi. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "Movie.h"
#import "XMLParser.h"
#import "iTunesAPI.h"
#import "MP42File.h"
#import "MP42FileImporter.h"
#import "RegexKitLite.h"
#import "JSONKit.h"
#define APPNAME "scraper"
#define APPVERSION 0.1
#define PROGRESSBAR_MAX_CALLS 1000
#define PROGRESSBAR_WIDTH 40
#define DEBUG 1
XMLParser *xmlParser;
BOOL verbose = false;
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Command Line Arguments
//
void print_help() {
printf("%s usage:\n", APPNAME);
printf("\n");
printf("\t -i, -input <file> URL of source file \n");
printf("\n");
printf("nfo Options:\n");
printf("\t -n, -nfo <nfo file> URL of nfo file to read \n");
printf("\t -w, -writenfo Create nfo file \n");
printf("\t -O, -overwritenfo Overwrite nfo file \n");
printf("\t -I, -ignorenfo Don't read nfo file \n");
printf("\n");
printf("Metadata Options:\n");
printf("\t -lm, -listmetadata List metadata \n");
printf("\t -it, -ignoretags Ignore tags \n");
printf("\t -t, -tag Tag file with data from nfo or scraper \n");
printf("\t -e, -extract <png file> Extract artwork to <png file> \n");
printf("\t -ed, -extractdefault Extract artwork to <movie>-poster.png \n");
printf("\n");
printf("Scraper Options:\n");
printf("\t -T, -title <title> Title for lookup \n");
printf("\t -c, -country <cc> Country Code for lookup e.g. de \n");
printf("\t -C, -listcc List available country codes \n");
printf("\t -L, -limit Limit search results (default: 1) \n");
printf("\t -N, -nolookup Don't lookup, just read nfo \n");
printf("\n");
printf("Mode Options:\n");
printf("\t -u, -unattended No user interactions, skip \n");
printf("\t -r, -remove Remove nfo and artwork from disk \n");
printf("\n");
printf("Misc Options:\n");
printf("\t -h, -help Show help \n");
printf("\t -V, -version Show version \n");
printf("\t -v, -verbose Verbose output \n");
printf("\t -W, -workflow Show workflow \n");
printf("\n");
printf("\n");
printf("Recommended Directory Structure:\n");
printf("\n");
printf("<All Movies>\n");
printf("[----<Matrix>\n");
printf(" [----<Matrix>.m4v\n");
printf("[----<Prestige>\n");
printf(" [----<foobar>.m4v\n");
printf("\n");
printf("The title for lookups is the name of parent directory of the m4v file or the title given via tag/nfo or via commandline argument. \n");
printf("\n");
printf("Examples: \n");
printf("Lookup for the title <Matrix> (show max 10 results) and tag the file but ignore embedded tags: \n");
printf("scraper -input \"/path/to/file.m4v\" -country de -limit 10 -tag -title \"Matrix\" \n");
printf("scraper -i \"/path/to/file.m4v\" -c de -L 10 -t -T \"Matrix\" \n");
printf("\n");
printf("Read tags from a file and create a nfo file for xbmc with embedded artwork as <movie>-poster.png \n");
printf("scraper -i \"/path/to/file.m4v\" -nolookup -writenfo -extractdefault \n");
printf("\n");
printf("Tag all m4v files from the infos of the nfo files \n");
printf("scraper -i \"/Users/username/iTunes/Movies/\" -it -t \n");
printf("\n");
printf("Lookup, create new nfo, tag, save Artwork for all movies in this directory without user interaction: \n");
printf("scraper -i \"/Users/username/iTunes/Movies/\" -it -t -c de -u -w -O -ed -I \n");
printf("\n");
printf("list metadata: \n");
printf("scraper -i \"/Users/username/iTunes/Movies/Matrix/Matrix.m4v\" -lm \n");
printf("\n");
printf("\n");
}
void print_version() {
printf("Version: %.1f \n", APPVERSION);
}
void print_cc() {
printf("All available country codes: \n");
iTunesAPI *itunesAPI = [[iTunesAPI alloc] init];
NSArray *cCodes = [[itunesAPI iTunesStoreCodes] allKeys];
for (NSString *cCode in cCodes) {
//NSLog(@"%@",cCode);
printf("%s ", [cCode UTF8String]);
}
printf("\n");
}
void print_workflow(){
printf("%s workflow: \n", APPNAME);
printf(" ___ ____ ____ ___ ____ \n");
printf(" |8| |8| | | / \\ |8| |8| | | \n");
printf(" |8|m4v|8| + | nfo| + |iTunes| >>> |8|m4v|8| + | nfo| \n");
printf(" |8|___|8| |____| \\____/ |8|___|8| |____| \n");
printf(" \n");
printf(" Get info from tags \n");
printf(" + nfo and overwrite info \n");
printf(" + iTunes and overwrite info \n");
printf(" >>> Write tags \n");
printf(" + new nfo \n");
printf(" \n");
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Helper Functions
//
// Process has done x out of n rounds,
// and we want a bar of width w and resolution r.
static void progressBar(int x, int n, int r, int w, NSString* aString, NSString* aDesc)
{
// Only update r times.
if ( x % (n/r +1) != 0 ) return;
// Calculuate the ratio of complete-to-incomplete.
float ratio = x/(float)n;
int c = ratio * w;
// Show the percentage complete.
printf("%s %3d%% [", [aString UTF8String], (int)(ratio*100) );
// Show the load bar.
for (int x=0; x<c; x++)
printf("=");
for (int x=c; x<w; x++)
printf(" ");
// ANSI Control codes to go back to the
// previous line and clear it.
if (x != 0) {
printf("] %s \n\033[F\033[J", [aDesc UTF8String]);
}
if ((ratio*100) == 100) {
printf("%s OK\n",[aString UTF8String]);
}
}
NSInteger indexOfITunesCodes(NSString *aRatingString, NSArray *iTunesCodes) {
NSString *splitElements = @"\\|";
NSArray *ratingItems = [aRatingString componentsSeparatedByRegex:splitElements];
NSInteger indexForItunesCode = 0;
if ([ratingItems count] > 2){
NSInteger counter = 0;
for (NSString *curiTunesCode in iTunesCodes) {
if ([curiTunesCode isEqualToString:aRatingString]) {
indexForItunesCode = counter;
return indexForItunesCode;
}
counter++;
}
} else {
//ratingiTunesCode = nil;
NSInteger counter = 0;
for (NSString *curiTunesCode in iTunesCodes) {
NSString *splitElements = @"\\|";
NSArray *ratingItems = [curiTunesCode componentsSeparatedByRegex:splitElements];
//de-movie|Ab 12 Jahren|200|
if ([[ratingItems objectAtIndex:1] isEqualToString:aRatingString]) {
indexForItunesCode = counter;
return indexForItunesCode;
}
counter++;
}
}
return indexForItunesCode;
}
NSArray* getAllFiles(NSString *path, NSString *aExt){
NSString* file;
NSDirectoryEnumerator* enumerator = [[NSFileManager defaultManager] enumeratorAtPath:path];
NSMutableArray* allFiles = [[NSMutableArray alloc] init];
while (file = [enumerator nextObject])
{
// check if it's a directory
BOOL isDirectory = NO;
[[NSFileManager defaultManager] fileExistsAtPath: [NSString stringWithFormat:@"%@/%@",path,file]
isDirectory: &isDirectory];
if (!isDirectory) {
// open your file …
NSString *tempExt = [NSString stringWithFormat:@".%@", aExt];
if (![file hasPrefix:@"."]) {
if ([file hasSuffix:tempExt]) {
[allFiles addObject:[NSString stringWithFormat:@"%@%@",path,file]];
}
}
} else {
if (![file hasPrefix:@"."]) {
[allFiles addObjectsFromArray:getAllFiles(file, aExt)];
}
}
}
return allFiles;
}
void removeData(NSString *inputFilename){
NSMutableArray *nfoFiles = [[NSMutableArray alloc] init];
NSMutableArray *jpgFiles = [[NSMutableArray alloc] init];
NSMutableArray *pngFiles = [[NSMutableArray alloc] init];
BOOL isDir = NO;
NSError *error = nil;
printf("Cleaning Metadata from Directory...\n");
if (inputFilename == nil) {
printf("Error: Please specify input file or directory!\n");
}
if([[NSFileManager defaultManager] fileExistsAtPath:inputFilename isDirectory:&isDir] && isDir){
//NSLog(@"Is directory");
nfoFiles = [NSMutableArray arrayWithArray:getAllFiles(inputFilename, @"nfo")];
} else {
NSString *extension = [[inputFilename pathExtension] lowercaseString];
//NSString *directory = [inputFilename stringByDeletingLastPathComponent];
if ([extension isEqualToString:@"nfo"]) {
[nfoFiles addObject:[@"" stringByAppendingPathComponent:inputFilename]];
// NSLog(@"m4vfile: %@", m4vFiles);
}
}
// jpg
if([[NSFileManager defaultManager] fileExistsAtPath:inputFilename isDirectory:&isDir] && isDir){
//NSLog(@"Is directory");
jpgFiles = [NSMutableArray arrayWithArray:getAllFiles(inputFilename, @"jpg")];
} else {
NSString *extension = [[inputFilename pathExtension] lowercaseString];
//NSString *directory = [inputFilename stringByDeletingLastPathComponent];
if ([extension isEqualToString:@"jpg"]) {
[jpgFiles addObject:[@"" stringByAppendingPathComponent:inputFilename]];
// NSLog(@"m4vfile: %@", m4vFiles);
}
}
// png
if([[NSFileManager defaultManager] fileExistsAtPath:inputFilename isDirectory:&isDir] && isDir){
//NSLog(@"Is directory");
pngFiles = [NSMutableArray arrayWithArray:getAllFiles(inputFilename, @"png")];
} else {
NSString *extension = [[inputFilename pathExtension] lowercaseString];
//NSString *directory = [inputFilename stringByDeletingLastPathComponent];
if ([extension isEqualToString:@"png"]) {
[pngFiles addObject:[@"" stringByAppendingPathComponent:inputFilename]];
// NSLog(@"m4vfile: %@", m4vFiles);
}
}
int noOfFiles = (int)[nfoFiles count] + (int)[jpgFiles count] + (int)[pngFiles count];
int fileCounter = 0;
// Delete all files ...
for (NSString *filePath in nfoFiles) {
[[NSFileManager defaultManager] removeItemAtPath:filePath error:&error];
if (error != nil) {
NSLog(@"Error: %@", error);
NSLog(@"Path to file: %@", filePath);
}
//////////////////////
// Display non verbose
//
if (!verbose) {
NSString *displayString = [NSString stringWithFormat:@" %3i / %3lu", fileCounter, (unsigned long)noOfFiles];
if (noOfFiles > 1) {
progressBar(fileCounter, (int)noOfFiles, PROGRESSBAR_MAX_CALLS, PROGRESSBAR_WIDTH, displayString, @"");
} else {
progressBar(100, 100, PROGRESSBAR_MAX_CALLS, PROGRESSBAR_WIDTH, displayString, @"");
}
}
fileCounter++;
}
for (NSString *filePath in jpgFiles) {
[[NSFileManager defaultManager] removeItemAtPath:filePath error:&error];
if (error != nil) {
NSLog(@"Error: %@", error);
NSLog(@"Path to file: %@", filePath);
}
//////////////////////
// Display non verbose
//
if (!verbose) {
NSString *displayString = [NSString stringWithFormat:@" %3i / %3lu", fileCounter, (unsigned long)noOfFiles];
if (noOfFiles > 1) {
progressBar(fileCounter, (int)noOfFiles, PROGRESSBAR_MAX_CALLS, PROGRESSBAR_WIDTH, displayString, @"");
} else {
progressBar(100, 100, PROGRESSBAR_MAX_CALLS, PROGRESSBAR_WIDTH, displayString, @"");
}
}
fileCounter++;
}
for (NSString *filePath in pngFiles) {
[[NSFileManager defaultManager] removeItemAtPath:filePath error:&error];
if (error != nil) {
NSLog(@"Error: %@", error);
NSLog(@"Path to file: %@", filePath);
}
//////////////////////
// Display non verbose
//
if (!verbose) {
NSString *displayString = [NSString stringWithFormat:@" %3i / %3lu", fileCounter, (unsigned long)noOfFiles];
if (noOfFiles > 1) {
progressBar(fileCounter, (int)noOfFiles, PROGRESSBAR_MAX_CALLS, PROGRESSBAR_WIDTH, displayString, @"");
} else {
progressBar(100, 100, PROGRESSBAR_MAX_CALLS, PROGRESSBAR_WIDTH, displayString, @"");
}
}
fileCounter++;
}
}
void writeXML(Movie *currentMovie, NSString *filePath)
{
NSXMLElement *root = [[NSXMLElement alloc] initWithName:@"movie"];
NSXMLDocument *xmlDoc = [NSXMLDocument documentWithRootElement:root];
[xmlDoc setVersion:@"1.0"];
[xmlDoc setCharacterEncoding:@"UTF-8"];
[xmlDoc setStandalone:YES];
// [root addAttribute:[NSXMLNode attributeWithName:@"Attribute1" stringValue:@"Value1"]];
// [root addAttribute:[NSXMLNode attributeWithName:@"Attribute2" stringValue:@"Value2"]];
// [root addAttribute:[NSXMLNode attributeWithName:@"Attribute3" stringValue:@"Value3"]];
// NSXMLDocument *xmlDoc = [[NSXMLDocument alloc] initWithRootElement:root];
// [root addChild:[NSXMLNode commentWithStringValue:@"Hello world!"]];
NSXMLElement *childElement = [[NSXMLElement alloc] initWithName:@"title" stringValue:currentMovie.title];
[root addChild:childElement];
childElement = [[NSXMLElement alloc] initWithName:@"originaltitle" stringValue:currentMovie.title];
[root addChild:childElement];
// childElement = [[NSXMLElement alloc] initWithName:@"outline" stringValue:currentMovie.shortDescription];
// [root addChild:childElement];
childElement = [[NSXMLElement alloc] initWithName:@"plot" stringValue:currentMovie.plot];
[root addChild:childElement];
childElement = [[NSXMLElement alloc] initWithName:@"mpaa" stringValue:currentMovie.mpaa];
[root addChild:childElement];
childElement = [[NSXMLElement alloc] initWithName:@"id" stringValue:currentMovie.imdbid];
[root addChild:childElement];
childElement = [[NSXMLElement alloc] initWithName:@"itunesid" stringValue:currentMovie.itunesid];
[root addChild:childElement];
childElement = [[NSXMLElement alloc] initWithName:@"storeid" stringValue:currentMovie.storeID];
[root addChild:childElement];
childElement = [[NSXMLElement alloc] initWithName:@"contentid" stringValue: [NSString stringWithFormat:@"%li", (long)currentMovie.contentID]];
[root addChild:childElement];
childElement = [[NSXMLElement alloc] initWithName:@"rating" stringValue:currentMovie.rating];
[root addChild:childElement];
childElement = [[NSXMLElement alloc] initWithName:@"year" stringValue:currentMovie.year];
[root addChild:childElement];
childElement = [[NSXMLElement alloc] initWithName:@"top250" stringValue:currentMovie.top250];
[root addChild:childElement];
childElement = [[NSXMLElement alloc] initWithName:@"votes" stringValue:currentMovie.votes];
[root addChild:childElement];
childElement = [[NSXMLElement alloc] initWithName:@"outline" stringValue:currentMovie.outline];
[root addChild:childElement];
childElement = [[NSXMLElement alloc] initWithName:@"tagline" stringValue:currentMovie.tagline];
[root addChild:childElement];
childElement = [[NSXMLElement alloc] initWithName:@"runtime" stringValue:currentMovie.runtime];
[root addChild:childElement];
childElement = [[NSXMLElement alloc] initWithName:@"playcount" stringValue:currentMovie.playcount];
[root addChild:childElement];
childElement = [[NSXMLElement alloc] initWithName:@"lastplayed" stringValue:currentMovie.lastplayed];
[root addChild:childElement];
childElement = [[NSXMLElement alloc] initWithName:@"set" stringValue:currentMovie.set];
[root addChild:childElement];
childElement = [[NSXMLElement alloc] initWithName:@"premiered" stringValue:currentMovie.premiered];
[root addChild:childElement];
childElement = [[NSXMLElement alloc] initWithName:@"status" stringValue:currentMovie.status];
[root addChild:childElement];
childElement = [[NSXMLElement alloc] initWithName:@"code" stringValue:currentMovie.code];
[root addChild:childElement];
childElement = [[NSXMLElement alloc] initWithName:@"aired" stringValue:currentMovie.aired];
[root addChild:childElement];
childElement = [[NSXMLElement alloc] initWithName:@"dateadded" stringValue:currentMovie.dateadded];
[root addChild:childElement];
childElement = [[NSXMLElement alloc] initWithName:@"copyright" stringValue:currentMovie.copyright];
[root addChild:childElement];
childElement = [[NSXMLElement alloc] initWithName:@"trailer" stringValue:currentMovie.trailer];
[root addChild:childElement];
if (currentMovie.kind == MP4MediaTypeMovie) {
childElement = [[NSXMLElement alloc] initWithName:@"kind" stringValue:@"feature-movie"];
[root addChild:childElement];
} else if (currentMovie.kind == MP4MediaTypeTvShow) {
childElement = [[NSXMLElement alloc] initWithName:@"kind" stringValue:@"feature-???"];
[root addChild:childElement];
}
childElement = [[NSXMLElement alloc] initWithName:@"artistname" stringValue:currentMovie.artistname];
[root addChild:childElement];
childElement = [[NSXMLElement alloc] initWithName:@"releasedate" stringValue:currentMovie.releasedate];
[root addChild:childElement];
childElement = [[NSXMLElement alloc] initWithName:@"studio" stringValue:currentMovie.studio];
[root addChild:childElement];
childElement = [[NSXMLElement alloc] initWithName:@"mpaaitunes" stringValue:currentMovie.mpaaiTunes];
[root addChild:childElement];
childElement = [[NSXMLElement alloc] initWithName:@"contentadvisoryrating" stringValue:currentMovie.contentAdvisoryRating];
[root addChild:childElement];
// Genres
for (NSString *genre in currentMovie.genres) {
childElement = [[NSXMLElement alloc] initWithName:@"genre" stringValue:genre];
[root addChild:childElement];
}
// country
for (NSString *country in currentMovie.countries) {
childElement = [[NSXMLElement alloc] initWithName:@"country" stringValue:country];
[root addChild:childElement];
}
// Credits
for (Actor *producer in currentMovie.producers) {
childElement = [[NSXMLElement alloc] initWithName:@"credits" stringValue:producer.name];
[root addChild:childElement];
}
// Directors
for (Actor *director in currentMovie.directors) {
childElement = [[NSXMLElement alloc] initWithName:@"director" stringValue:director.name];
[root addChild:childElement];
}
// Thumbs
for (Thumb *thumb in currentMovie.thumbs) {
childElement = [[NSXMLElement alloc] initWithName:@"thumb" stringValue:thumb.value];
if (![thumb.aspect isEqualToString:@""]) {
[childElement addAttribute:[NSXMLNode attributeWithName:@"aspect" stringValue:thumb.aspect]];
}
if (![thumb.preview isEqualToString:@""]) {
[childElement addAttribute:[NSXMLNode attributeWithName:@"preview" stringValue:thumb.preview]];
}
[root addChild:childElement];
}
// Fanarts
if ([currentMovie.fanarts count] > 0) {
childElement = [[NSXMLElement alloc] initWithName:@"fanart"];
for (Thumb *thumb in currentMovie.fanarts) {
NSXMLElement *nameElement = [[NSXMLElement alloc] initWithName:@"thumb" stringValue:thumb.value];
if (![thumb.aspect isEqualToString:@""]) {
[nameElement addAttribute:[NSXMLNode attributeWithName:@"aspect" stringValue:thumb.aspect]];
}
if (![thumb.preview isEqualToString:@""]) {
[nameElement addAttribute:[NSXMLNode attributeWithName:@"preview" stringValue:thumb.preview]];
}
[childElement addChild:nameElement];
}
[root addChild:childElement];
}
// Actors
for (Actor *actor in currentMovie.actors) {
childElement = [[NSXMLElement alloc] initWithName:@"actor"];
NSXMLElement *nameElement = [[NSXMLElement alloc] initWithName:@"name" stringValue:actor.name];
[childElement addChild:nameElement];
NSXMLElement *roleElement = [[NSXMLElement alloc] initWithName:@"role" stringValue:actor.role];
[childElement addChild:roleElement];
NSXMLElement *orderElement = [[NSXMLElement alloc] initWithName:@"order" stringValue:actor.order];
[childElement addChild:orderElement];
NSXMLElement *thumbElement = [[NSXMLElement alloc] initWithName:@"thumb" stringValue:actor.thumb];
[childElement addChild:thumbElement];
[root addChild:childElement];
}
// NSXMLElement *childElement2 = [[NSXMLElement alloc] initWithName:@"ChildElement2"];
// [childElement2 addAttribute:[NSXMLNode attributeWithName:@"ChildAttribute2.1" stringValue:@"Value2.1"]];
// [childElement2 setStringValue:@"ChildValue2.1"];
// [root addChild:childElement2];
// NSLog(@"XML Document\n%@", xmlDoc);//till this art code runs fine.
NSData *xmlData = [xmlDoc XMLDataWithOptions:NSXMLNodePrettyPrint];
[[NSFileManager defaultManager] createFileAtPath:filePath contents:xmlData attributes:nil];
// file = [NSFileHandle fileHandleForUpdatingAtPath: filePath];
// //set writing path to file
// if (file == nil) //check file present or not in file
// NSLog(@"Failed to open file");
// //[file seekToFileOffset: 6];
// //object pointer initialy points the offset as 6 position in file
// [file writeData: xmlData];
// //writing data to new file
// [file closeFile];
// [xmlData writeToFile:@"/Users/halen/Documents/project3/xmlsample.xml" atomically:YES];
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Main
//
int main(int argc, const char * argv[]) {
@autoreleasepool {
// insert code here...
//printf("scraper v %.1f\n", APPVERSION);
NSString *inputFilename = NULL;
NSString *nfoFilename = NULL;
NSString *pngFilename = NULL;
NSString *inputFullname = NULL;
NSString *inputDir = NULL;
NSString *inputParentDir = NULL;
NSString *inputTitle = NULL;
NSString *inputCC = @"de";
NSString *artworkFilename = NULL;
NSString *fanartFilename = NULL;
NSNumber *inputLimit = @1;
int fileCounter = 1;
NSMutableArray *m4vFiles = [[NSMutableArray alloc] init];
BOOL ignoreNFO = false;
BOOL writenfo = false;
BOOL overwritenfo = false;
BOOL nolookup = false;
BOOL unattended = false;
BOOL skip = false;
BOOL listmetadata = false;
BOOL ignoreTags = false;
BOOL tag = false;
BOOL hasArtwork = false;
BOOL extract = false;
NSUInteger noOfFiles = 1;
NSInteger curRating = 0;
NSMutableArray *skippedMovies = [[NSMutableArray alloc] init];
NSMutableArray *processedMovies = [[NSMutableArray alloc] init];
NSFileManager *fileManager = [NSFileManager defaultManager];
xmlParser = [[XMLParser alloc] init];
printf(" \n");
printf(" _______ _______ ______ _______ _____ _______ ______ \n");
printf(" |______ | |_____/ |_____| |_____] |______ |_____/ \n");
printf(" ______| |_____ | \\_ | | | |______ | \\_ \n");
printf("\n");
printf("This Software is currently in Beta status. Backup your files! \n");
printf("\n");
printf("\n");
if (argc == 1) {
print_help();
exit(-1);
}
argv += 1;
argc--;
#ifdef DEBUG
printf("DEBUG MODE enabled!\n");
printf("DEBUG - Parameter: \n");
#endif
// Handle args
while (argc > 0 && **argv == '-') {
const char *args = &(*argv)[1];
argc--;
argv++;
#ifdef DEBUG
printf("-%s ",args);
fflush(stdout);
#endif
if (( ! strcmp ( args, "input" )) || ( ! strcmp ( args, "i" )) ) {
inputFilename = @(*argv++);
argc--;
#ifdef DEBUG
printf("%s ",[inputFilename UTF8String]);
fflush(stdout);
#endif
BOOL isDir = NO;
if([[NSFileManager defaultManager] fileExistsAtPath:inputFilename isDirectory:&isDir] && isDir){
//NSLog(@"Is directory");
//inputDir = [inputFilename stringByDeletingLastPathComponent];
inputDir = inputFilename;
// if inputDir does NOT end inputFilename with "/" add it
if (![inputDir hasSuffix:@"/"]){
inputDir = [NSString stringWithFormat:@"%@/", inputDir];
}
m4vFiles = [NSMutableArray arrayWithArray:getAllFiles(inputDir, @"m4v")];
} else {
inputDir = [inputFilename stringByDeletingLastPathComponent];
NSString *extension = [[inputFilename pathExtension] lowercaseString];
//NSString *directory = [inputFilename stringByDeletingLastPathComponent];
if ([extension isEqualToString:@"m4v"]) {
[m4vFiles addObject:[@"" stringByAppendingPathComponent:inputFilename]];
// NSLog(@"m4vfile: %@", m4vFiles);
}
}
noOfFiles = [m4vFiles count];
}
else if (( ! strcmp ( args, "verbose" )) || ( ! strcmp ( args, "v" )) ) {
verbose = YES;
}
else if (( ! strcmp ( args, "nfo" )) || ( ! strcmp ( args, "n" )) ) {
nfoFilename = @(*argv++);
argc--;
#ifdef DEBUG
printf("%s ",[nfoFilename UTF8String]);
fflush(stdout);
#endif
if (verbose) {
}
}
else if (( ! strcmp ( args, "title" )) || ( ! strcmp ( args, "T" )) ) {
inputTitle = @(*argv++);
#ifdef DEBUG
printf("%s ",[inputTitle UTF8String]);
fflush(stdout);
#endif
argc--;
}
else if (( ! strcmp ( args, "limit" )) || ( ! strcmp ( args, "L" )) ) {
NSNumberFormatter * f = [[NSNumberFormatter alloc] init];
[f setNumberStyle:NSNumberFormatterDecimalStyle];
inputLimit = [f numberFromString:@(*argv++)];
argc--;
#ifdef DEBUG
printf("%ld ",[inputLimit longValue]);
fflush(stdout);
#endif
}
else if (( ! strcmp ( args, "country" )) || ( ! strcmp ( args, "c" )) ) {
inputCC = @(*argv++);
argc--;
#ifdef DEBUG
printf("%s ",[inputCC UTF8String]);
fflush(stdout);
#endif
}
else if (( ! strcmp ( args, "extract" )) || ( ! strcmp ( args, "e" )) ) {
pngFilename = @(*argv++);
argc--;
#ifdef DEBUG
printf("%s ",[pngFilename UTF8String]);
fflush(stdout);
#endif
}
else if (( ! strcmp ( args, "extractdefault" )) || ( ! strcmp ( args, "ed" )) ) {
extract = YES;
}
else if (( ! strcmp ( args, "listcc" )) || ( ! strcmp ( args, "C" )) ) {
print_cc();
return 0;
}
else if (( ! strcmp ( args, "remove" )) || ( ! strcmp ( args, "r" )) ) {
removeData(inputFilename);
return 0;
}
else if (( ! strcmp ( args, "listmetadata" )) || ( ! strcmp ( args, "lm" )) ) {
listmetadata = YES;
}
else if (( ! strcmp ( args, "ignoretags" )) || ( ! strcmp ( args, "it" )) ) {
ignoreTags = YES;
}
else if (( ! strcmp ( args, "nolookup" )) || ( ! strcmp ( args, "N" )) ) {
nolookup = YES;
}
else if (( ! strcmp ( args, "unattended" )) || ( ! strcmp ( args, "u" )) ) {
unattended = YES;
}
else if (( ! strcmp ( args, "tag" )) || ( ! strcmp ( args, "t" )) ) {
tag = YES;
}
else if (( ! strcmp ( args, "ignorenfo" )) || ( ! strcmp ( args, "I" )) ) {
ignoreNFO = YES;
}
else if (( ! strcmp ( args, "writenfo" )) || ( ! strcmp ( args, "w" )) ) {
writenfo = YES;
}
else if (( ! strcmp ( args, "overwritenfo" )) || ( ! strcmp ( args, "O" )) ) {
overwritenfo = YES;
}
else if (( ! strcmp ( args, "version" )) || ( ! strcmp ( args, "V" )) ) {
print_version();
return 0;
}
else if (( ! strcmp ( args, "workflow" )) || ( ! strcmp ( args, "W" )) ) {
print_workflow();
return 0;
}
else if (( ! strcmp ( args, "help" )) || ( ! strcmp ( args, "h" )) ) {
print_help();
return 0;
} else {
printf("Invalid input parameter: %s\n", args );
print_help();
return 1;
}
}
#ifdef DEBUG
printf("\n");
#endif
// printf("Searching Movie for movie: ");
// fflush(stdout);
//
// for (int i=1; i<=100; i++) {
// progressBar(i, 100, PROGRESSBAR_MAX_CALLS, PROGRESSBAR_WIDTH, @"Searching Movie for movie ");
// usleep(10000);
// }
// Override Limit when unattended
if (unattended) {
inputLimit = @1;
}
if (verbose) {
printf("Parameter: \n");
printf("\t verbose: \t %hhd\n", verbose);
printf("\t inputCC: \t %s\n", [inputCC UTF8String]);
if (ignoreNFO) {
printf("\t ignorenfo: \t %hhd\n", ignoreNFO);
}
if (nfoFilename != NULL) {
printf("\t nfo: \t %s\n", [nfoFilename UTF8String]);
}
if (inputTitle != NULL) {
printf("\t title: \t %s\n", [inputTitle UTF8String]);
}
if (writenfo) {
printf("\t writenfo: \t %hhd\n", writenfo);
}
if (overwritenfo) {
printf("\t overwritenfo: \t %hhd\n", overwritenfo);
}
printf("\t No Files: \t %lu\n", (unsigned long)noOfFiles);
for (int i=0; i<noOfFiles; i++) {
printf("\t input: \t %s\n", [[m4vFiles objectAtIndex:i] UTF8String]);
// NSLog(@"%@",[m4vFiles objectAtIndex:i]);
}
}
//NSLog(@"m4vFiles: %@",m4vFiles);
Movie *currentMovie;
MP42File *mp4File = nil;
for (NSString *m4vFile in m4vFiles) {
currentMovie = [[Movie alloc] init];
inputFullname = [NSString stringWithFormat:@"%s",[m4vFile UTF8String]];
// Trim inputFullname
inputFullname = [inputFullname stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
inputDir = [inputFullname stringByDeletingLastPathComponent];
// documentDirectory = @"~me/Public/Demo"
//NSString *documentFilename = [fileURL lastPathComponent];
// documentFilename = @"readme.txt"
//NSString *fileExtension = [fileURL pathExtension];
// documentExtension = @"txt"
NSArray *components = [inputFullname pathComponents];
inputParentDir = [NSString pathWithComponents:[components subarrayWithRange:(NSRange){ [components count] - 2 , 1}]];
NSString* fileName = [[inputFullname lastPathComponent] stringByDeletingPathExtension];
if(!unattended) {
printf("================== %s ==================\n", [fileName UTF8String]);
}
nfoFilename = [NSString stringWithFormat:@"%@/%@.nfo", inputDir, fileName];
NSString *nfoFilename2 = [NSString stringWithFormat:@"%@/movie.nfo", inputDir];
NSString *nfoTarget = nfoFilename;
// if (verbose) {
// printf("\t input: \t %s\n", [inputFullname UTF8String]);
// printf("\t Dir name: \t %s\n", [inputParentDir UTF8String]);
// printf("\t File name: \t %s\n", [fileName UTF8String]);
// printf("\t nfo file: \t %s\n", [nfoFilename UTF8String]);
// }
//////////////////////
// Read Tags
//
//mp4File = [[MP42File alloc] initWithExistingFile:[NSURL fileURLWithPath:inputFullname] andDelegate:nil];
if ([[NSFileManager defaultManager] fileExistsAtPath:inputFullname]) {
mp4File = [[MP42File alloc] initWithExistingFile:[NSURL fileURLWithPath:inputFullname]
andDelegate:nil];
}
if (!mp4File) {
if (!unattended) {
printf("Error: %s\n", "the mp4 file couln't be opened.");
return -1;
}
currentMovie.title = inputFullname;
[skippedMovies addObject:currentMovie];
skip=YES;
}
if ((!ignoreTags) || (listmetadata)) {
if (!unattended) {
printf("Reading tags...");
}
NSArray * availableMetadata = [[mp4File metadata] availableMetadata];
NSDictionary * tagsDict = [[mp4File metadata] tagsDict];
if (!unattended) {
printf(" OK\n");
}
for (NSString* key in availableMetadata) {
NSString* tag = [tagsDict valueForKey:key];
if (tag) {
if ([tag isKindOfClass:[NSString class]]) {
if ((verbose) || (listmetadata)) {
printf("%s: %s\n", [key UTF8String], [tag UTF8String]);
}
}
if ([tag isKindOfClass:[NSNumber class]]) {
if ((verbose) || (listmetadata)) {
printf("%s: %ld\n", [key UTF8String], (long)[tag integerValue]);
}
}
if ([key isEqualToString:@"Name"])
currentMovie.title = tag;
if ([key isEqualToString:@"Artist"])
currentMovie.artistname = tag;
if ([key isEqualToString:@"Rating"])
curRating = [tag integerValue];
if ([key isEqualToString:@"Genre"]) {
NSMutableArray *curGenres = [[NSMutableArray alloc] init];
[curGenres addObject:tag];
currentMovie.genres = curGenres;
}
if ([key isEqualToString:@"Release Date"]) {
currentMovie.releasedate = tag;
currentMovie.year = [currentMovie.releasedate substringWithRange: NSMakeRange (0, 4)];
}
if ([key isEqualToString:@"iTunes Country"])
currentMovie.storeID = tag;
if ([key isEqualToString:@"Description"])
currentMovie.plot = tag;
if ([key isEqualToString:@"Copyright"])
currentMovie.copyright = tag;
if ([key isEqualToString:@"contentID"])
currentMovie.contentID = (long)[tag integerValue];
if ([key isEqualToString:@"Cast"]) {
//NSLog(@"%@",tag);
NSArray *tempArray = [[NSArray alloc]init];
NSMutableArray *actors = [[NSMutableArray alloc] init];
tempArray = [tag componentsSeparatedByString:@","];
for (NSString *curActor in tempArray) {
Actor *newActor = [[Actor alloc] initWithName:curActor];
[actors addObject:newActor];
}
currentMovie.actors = actors;
}
if ([key isEqualToString:@"Director"]) {
//NSLog(@"%@",tag);
NSArray *tempArray = [[NSArray alloc]init];
NSMutableArray *actors = [[NSMutableArray alloc] init];
tempArray = [tag componentsSeparatedByString:@","];
for (NSString *curActor in tempArray) {
Actor *newActor = [[Actor alloc] initWithName:curActor];
[actors addObject:newActor];
}
currentMovie.directors = actors;
}
if ([key isEqualToString:@"Producers"]) {
//NSLog(@"%@",tag);
NSArray *tempArray = [[NSArray alloc]init];
NSMutableArray *actors = [[NSMutableArray alloc] init];
tempArray = [tag componentsSeparatedByString:@","];
for (NSString *curActor in tempArray) {
Actor *newActor = [[Actor alloc] initWithName:curActor];
[actors addObject:newActor];