/
discogs.cpp
2785 lines (2207 loc) · 78.7 KB
/
discogs.cpp
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
#include "stdafx.h"
#include "jansson/jansson.h"
#include "foo_discogs.h"
#include "utils.h"
#include "json_helpers.h"
#include "discogs.h"
#include "ol_cache.h"
using namespace Discogs;
const ExposedMap<ReleaseLabel> ExposedTags<ReleaseLabel>::exposed_tags = ReleaseLabel::create_tags_map();
const ExposedMap<ReleaseCompany> ExposedTags<ReleaseCompany>::exposed_tags = ReleaseCompany::create_tags_map();
const ExposedMap<ReleaseSeries> ExposedTags<ReleaseSeries>::exposed_tags = ReleaseSeries::create_tags_map();
const ExposedMap<ReleaseTrack> ExposedTags<ReleaseTrack>::exposed_tags = ReleaseTrack::create_tags_map();
const ExposedMap<ReleaseHeading> ExposedTags<ReleaseHeading>::exposed_tags = ReleaseHeading::create_tags_map();
const ExposedMap<ReleaseIndexes> ExposedTags<ReleaseIndexes>::exposed_tags = ReleaseIndexes::create_tags_map();
const ExposedMap<Release> ExposedTags<Release>::exposed_tags = Release::create_tags_map();
const ExposedMap<MasterRelease> ExposedTags<MasterRelease>::exposed_tags = MasterRelease::create_tags_map();
const ExposedMap<Artist> ExposedTags<Artist>::exposed_tags = Artist::create_tags_map();
const ExposedMap<ReleaseArtist> ExposedTags<ReleaseArtist>::exposed_tags = ReleaseArtist::create_tags_map();
const ExposedMap<ReleaseCredit> ExposedTags<ReleaseCredit>::exposed_tags = ReleaseCredit::create_tags_map();
const ExposedMap<ReleaseFormat> ExposedTags<ReleaseFormat>::exposed_tags = ReleaseFormat::create_tags_map();
const ExposedMap<ReleaseDisc> ExposedTags<ReleaseDisc>::exposed_tags = ReleaseDisc::create_tags_map();
const ExposedMap<ExpTagsImage> ExposedTags<ExpTagsImage>::exposed_tags = ExpTagsImage::create_tags_map();
string_encoded_array Discogs::ReleaseArtist::get_data(pfc::string8 &tag_name, threaded_process_status &p_status, abort_callback &p_abort) {
try {
return ExposedTags<ReleaseArtist>::get_data(tag_name, p_status, p_abort);
}
catch (missing_data_exception) {
return full_artist->get_data(tag_name, p_status, p_abort);
}
}
string_encoded_array Discogs::Artist::get_sub_data(pfc::string8 &tag_name, threaded_process_status &p_status, abort_callback &p_abort) {
pfc::string8 sub_tag_name;
string_encoded_array result;
if (STR_EQUALN(tag_name, "IMAGES_", 7)) {
result.force_array();
sub_tag_name = substr(tag_name, 7);
load(p_status, p_abort);
for (size_t i = 0; i < images.get_size(); i++) {
result.append_item_val(images[i]->get_data(sub_tag_name, p_status, p_abort));
}
}
else if (STR_EQUALN(tag_name, "RELEASES_", 9)) {
result.force_array();
sub_tag_name = substr(tag_name, 9);
load_releases(p_status, p_abort, false, nullptr);
for (size_t i = 0; i < releases.get_size(); i++) {
result.append_item_val(releases[i]->get_data(sub_tag_name, p_status, p_abort));
}
}
else {
return ExposedTags<Artist>::get_sub_data(tag_name, p_status, p_abort);
}
result.encode();
return result;
}
string_encoded_array Discogs::ReleaseCredit::get_sub_data(pfc::string8 &tag_name, threaded_process_status &p_status, abort_callback &p_abort) {
pfc::string8 sub_tag_name;
string_encoded_array result;
if (strncmp(tag_name.get_ptr(), "ARTISTS_", 8) == 0) {
result.force_array();
sub_tag_name = pfc::string8(tag_name.get_ptr() + 8);
for (size_t i = 0; i < artists.get_size(); i++) {
result.append_item_val(artists[i]->get_data(sub_tag_name, p_status, p_abort));
}
}
else {
return ExposedTags<ReleaseCredit>::get_sub_data(tag_name, p_status, p_abort);
}
result.encode();
return result;
}
string_encoded_array Discogs::ReleaseTrack::get_sub_data(pfc::string8 &tag_name, threaded_process_status &p_status, abort_callback &p_abort) {
pfc::string8 sub_tag_name;
string_encoded_array result;
if (strncmp(tag_name.get_ptr(), "ARTISTS_", 8) == 0) {
result.force_array();
sub_tag_name = pfc::string8(tag_name.get_ptr() + 8);
for (size_t i = 0; i < artists.get_size(); i++) {
result.append_item_val(artists[i]->get_data(sub_tag_name, p_status, p_abort));
}
}
else if (strncmp(tag_name.get_ptr(), "CREDITS_", 8) == 0) {
result.force_array();
sub_tag_name = pfc::string8(tag_name.get_ptr() + 8);
for (size_t i = 0; i < credits.get_size(); i++) {
result.append_item_val(credits[i]->get_data(sub_tag_name, p_status, p_abort));
}
}
else if (strncmp(tag_name.get_ptr(), "HIDDEN_TRACKS_", 14) == 0) {
result.force_array();
sub_tag_name = pfc::string8(tag_name.get_ptr() + 14);
for (size_t i = 0; i < hidden_tracks.get_size(); i++) {
result.append_item_val(hidden_tracks[i]->get_data(sub_tag_name, p_status, p_abort));
}
}
else {
return ExposedTags<ReleaseTrack>::get_sub_data(tag_name, p_status, p_abort);
}
result.encode();
return result;
}
string_encoded_array Discogs::ReleaseDisc::get_sub_data(pfc::string8 &tag_name, threaded_process_status &p_status, abort_callback &p_abort) {
pfc::string8 sub_tag_name;
string_encoded_array result;
if (STR_EQUALN(tag_name, "TRACKS_", 7)) {
result.force_array();
sub_tag_name = substr(tag_name, 7);
for (size_t i = 0; i < tracks.get_size(); i++) {
result.append_item_val(tracks[i]->get_data(sub_tag_name, p_status, p_abort));
}
}
else
{
return ExposedTags<ReleaseDisc>::get_sub_data(tag_name, p_status, p_abort);
}
result.encode();
return result;
}
string_encoded_array Discogs::Release::get_sub_data(pfc::string8 &tag_name, threaded_process_status &p_status, abort_callback &p_abort) {
pfc::string8 sub_tag_name;
string_encoded_array result;
if (STR_EQUALN(tag_name, "TRACKS_", 7)) {
result.force_array();
load(p_status, p_abort);
for (size_t i = 0; i < discs.get_size(); i++) {
if (i == 0) {
result = discs[i]->get_data(tag_name, p_status, p_abort);
}
else {
result.shallow_extend(discs[i]->get_data(tag_name, p_status, p_abort));
}
}
}
else if (STR_EQUALN(tag_name, "DISCS_", 6)) {
result.force_array();
sub_tag_name = substr(tag_name, 6);
load(p_status, p_abort);
for (size_t i = 0; i < discs.get_size(); i++) {
result.append_item_val(discs[i]->get_data(sub_tag_name, p_status, p_abort));
}
}
else if (STR_EQUALN(tag_name, "ARTISTS_", 8)) {
result.force_array();
sub_tag_name = substr(tag_name, 8);
load(p_status, p_abort);
for (size_t i = 0; i < artists.get_size(); i++) {
result.append_item_val(artists[i]->get_data(sub_tag_name, p_status, p_abort));
}
}
else if (STR_EQUALN(tag_name, "LABELS_", 7)) {
result.force_array();
sub_tag_name = substr(tag_name, 7);
load(p_status, p_abort);
for (size_t i = 0; i < labels.get_size(); i++) {
result.append_item_val(labels[i]->get_data(sub_tag_name, p_status, p_abort));
}
}
else if (STR_EQUALN(tag_name, "COMPANIES_", 10)) {
result.force_array();
sub_tag_name = substr(tag_name, 10);
load(p_status, p_abort);
for (size_t i = 0; i < companies.get_size(); i++) {
result.append_item_val(companies[i]->get_data(sub_tag_name, p_status, p_abort));
}
}
else if (STR_EQUALN(tag_name, "SERIES_", 7)) {
result.force_array();
sub_tag_name = substr(tag_name, 7);
load(p_status, p_abort);
for (size_t i = 0; i < series.get_size(); i++) {
result.append_item_val(series[i]->get_data(sub_tag_name, p_status, p_abort));
}
}
else if (STR_EQUALN(tag_name, "FORMATS_", 8)) {
result.force_array();
sub_tag_name = substr(tag_name, 8);
load(p_status, p_abort);
for (size_t i = 0; i < formats.get_size(); i++) {
result.append_item_val(formats[i]->get_data(sub_tag_name, p_status, p_abort));
}
}
else if (STR_EQUALN(tag_name, "CREDITS_", 8)) {
result.force_array();
sub_tag_name = substr(tag_name, 8);
load(p_status, p_abort);
for (size_t i = 0; i < credits.get_size(); i++) {
result.append_item_val(credits[i]->get_data(sub_tag_name, p_status, p_abort));
}
}
else if (STR_EQUALN(tag_name, "IMAGES_", 7)) {
result.force_array();
sub_tag_name = substr(tag_name, 7);
load(p_status, p_abort);
for (size_t i = 0; i < images.get_size(); i++) {
result.append_item_val(images[i]->get_data(sub_tag_name, p_status, p_abort));
}
}
else if (STR_EQUALN(tag_name, "MASTER_RELEASE_", 15)) {
sub_tag_name = substr(tag_name, 15);
load_preview(p_status, p_abort);
if (master_release) {
result = master_release->get_data(sub_tag_name, p_status, p_abort);
}
}
else {
return ExposedTags<Release>::get_sub_data(tag_name, p_status, p_abort);
}
result.encode();
return result;
}
string_encoded_array Discogs::MasterRelease::get_sub_data(pfc::string8 &tag_name, threaded_process_status &p_status, abort_callback &p_abort) {
pfc::string8 sub_tag_name;
string_encoded_array result;
if (STR_EQUALN(tag_name, "TRACKS_", 7)) {
result.force_array();
load(p_status, p_abort);
for (size_t i = 0; i < discs.get_size(); i++) {
if (i == 0) {
result = discs[i]->get_data(tag_name, p_status, p_abort);
}
else {
result.shallow_extend(discs[i]->get_data(tag_name, p_status, p_abort));
}
}
}
else if (STR_EQUALN(tag_name, "DISCS_", 6)) {
result.force_array();
sub_tag_name = substr(tag_name, 6);
load(p_status, p_abort);
for (size_t i = 0; i < discs.get_size(); i++) {
result.append_item_val(discs[i]->get_data(sub_tag_name, p_status, p_abort));
}
}
else if (STR_EQUALN(tag_name, "ARTISTS_", 8)) {
result.force_array();
sub_tag_name = substr(tag_name, 8);
load(p_status, p_abort);
for (size_t i = 0; i < artists.get_size(); i++) {
result.append_item_val(artists[i]->get_data(sub_tag_name, p_status, p_abort));
}
}
else if (STR_EQUALN(tag_name, "IMAGES_", 7)) {
result.force_array();
sub_tag_name = substr(tag_name, 7);
load(p_status, p_abort);
for (size_t i = 0; i < images.get_size(); i++) {
result.append_item_val(images[i]->get_data(sub_tag_name, p_status, p_abort));
}
}
else if (STR_EQUALN(tag_name, "MAIN_RELEASE_", 13)) {
sub_tag_name = substr(tag_name, 13);
load_preview(p_status, p_abort);
if (main_release) {
result = main_release->get_data(sub_tag_name, p_status, p_abort);
}
}
else if (STR_EQUALN(tag_name, "RELEASES_", 9)) {
result.force_array();
sub_tag_name = substr(tag_name, 9);
load_releases(p_status, p_abort);
for (size_t i = 0; i < sub_releases.get_size(); i++) {
result.append_item_val(sub_releases[i]->get_data(sub_tag_name, p_status, p_abort));
}
}
else {
return ExposedTags<MasterRelease>::get_sub_data(tag_name, p_status, p_abort);
}
result.encode();
return result;
}
inline void Discogs::MasterRelease::set_main_release(Release_ptr main) {
main_release = main;
if (main) {
main_release_id = main->id;
}
}
inline void Discogs::Release::set_master_release(MasterRelease_ptr master) {
master_release = master;
if (master) {
master_id = master->id;
}
}
pfc::string8 Discogs::remove_number_suffix(const pfc::string8& src) {
if (src.get_length() < 4) {
return src.c_str();
}
pfc::string8 dst = src;
unsigned src_size = src.get_length();
std::regex regex_v;
try {
regex_v = std::regex("\\(\\d+?\\)");
}
catch (std::regex_error e) {
dst.set_string(e.what());
return false;
}
try {
dst.set_string(std::regex_replace(dst.get_ptr(), regex_v, "").c_str());
while (dst.length() > 0 && dst.endsWith(' '))
dst.truncate(dst.length() - 1);
}
catch (std::regex_error e) {
dst.set_string(e.what());
return false;
}
return dst;
}
pfc::string8 Discogs::move_the_to_end(const pfc::string8 &src) {
if (src.get_length() < 5) {
return src;
}
pfc::string8 tmp = src;
pfc::string8 start = substr(tmp, 0, 4);
if (STR_EQUAL(start, "The ") || STR_EQUAL(start, "the ")) {
tmp = substr(tmp, 4, src.get_length());
tmp << ", The";
}
return tmp;
}
pfc::string8 Discogs::move_the_to_start(const pfc::string8 &src) {
if (src.get_length() < 5) {
return src;
}
pfc::string8 tmp = src;
pfc::string8 end = substr(tmp, src.get_length() - 5, 5);
if (STR_EQUAL(end, ", The") || STR_EQUAL(end, ", the")) {
pfc::string8 ret("The ");
ret << substr(tmp, 0, src.length() - 5);
return ret;
}
return src;
}
pfc::string8 Discogs::strip_artist_name(const pfc::string8 str) {
pfc::string8 result = lowercase(str);
result = rtrim(result);
if (result.length() > 0) {
if (result[result.length() - 1] == ')') {
result = substr(result, 0, result.find_last('('));
result = rtrim(result);
}
if (result.length() > 5 && STR_EQUAL(substr(result, result.length() - 5, 5), ", the")) {
result = substr(result, 0, result.length() - 5);
result = rtrim(result);
}
if (result.length() > 4 && STR_EQUAL(substr(result, result.length() - 4, 4), "the ")) {
result = substr(result, 4);
result = rtrim(result);
}
}
return result;
}
pfc::string8 Discogs::format_track_number(int tracknumber) {
char buf[4];
sprintf_s(buf, "%d", tracknumber);
return buf;
}
ReleaseArtist_ptr Discogs::parseReleaseArtist(json_t *element, bool preload) {
assert_is_object(element);
pfc::string8 artist_id = JSONAttributeString(element, "id");
pfc::string8 name = JSONAttributeString(element, "name");
pfc::string8 anv = JSONAttributeString(element, "anv");
auto full_artist = discogs_interface->get_artist(artist_id);
ReleaseArtist_ptr artist(new ReleaseArtist(artist_id, full_artist, preload));
artist->name = name;
artist->anv = anv;
artist->raw_roles = JSONAttributeString(element, "role");
tokenize_non_bracketed(artist->raw_roles, ",", artist->full_roles, true);
for (size_t i = 0; i < artist->full_roles.get_size(); i++) {
size_t pos = artist->full_roles[i].find_first('[');
if (pos != pfc::infinite_size) {
artist->roles.append_single(trim(substr(artist->full_roles[i], 0, pos)));
}
else {
artist->roles.append_single(artist->full_roles[i]);
}
}
artist->join = JSONAttributeString(element, "join");
return std::move(artist);
}
void Discogs::parseReleaseArtists(json_t *element, pfc::array_t<ReleaseArtist_ptr> &artists, bool preload) {
assert_is_array(element);
for (size_t i = 0; i < json_array_size(element); i++) {
json_t *artist = json_array_get(element, i);
artists.append_single(parseReleaseArtist(artist, preload));
}
}
pfc::array_t<pfc::string8> get_delims() {
pfc::array_t<pfc::string8> v;
v.append_single(pfc::string8(","));
v.append_single(pfc::string8(";"));
v.append_single(pfc::string8(" and "));
v.append_single(pfc::string8("&"));
//v.append_single(pfc::string8(" & "));
return v;
}
const pfc::array_t<pfc::string8> cCREDIT_DELIMS = get_delims();
void Discogs::parseReleaseCredits(json_t* element, pfc::array_t<ReleaseCredit_ptr> &credits, Release *release) {
assert_is_array(element);
pfc::string8 last_role;
for (size_t i = 0; i < json_array_size(element); i++) {
json_t *j = json_array_get(element, i);
ReleaseArtist_ptr artist = parseReleaseArtist(j);
const pfc::string8 &tracks = JSONAttributeString(j, "tracks");
if (tracks.get_length() && !tracks.equals("All")) {
ReleaseCredit_ptr credit(new ReleaseCredit());
credit->raw_roles = artist->raw_roles;
credit->roles = artist->roles;
credit->full_roles = artist->full_roles;
credit->artists.append_single(artist);
try {
pfc::array_t<pfc::string8> array_parts;
pfc::string8 sani_to_tracks = sanitize_track_to(tracks);
size_t count = tokenize_multi(sanitize_track_commas(sani_to_tracks), cCREDIT_DELIMS, array_parts, true, true);
DistReleaseTrackCredits(array_parts, credit, release);
}
catch (parser_exception& e) {
pfc::string8 error;
error << ("(skipped) ") << e.what() << "\n";
popup_message::g_show(error.get_ptr(), "Error(s)", popup_message::icon_error);
}
}
else {
if (!artist->raw_roles.get_length() || (credits.get_size() && STR_EQUAL(artist->raw_roles, credits[credits.get_size() - 1]->raw_roles))) {
credits[credits.get_size() - 1]->artists.append_single(artist);
}
else {
ReleaseCredit_ptr credit(new ReleaseCredit());
credit->raw_roles = artist->raw_roles;
credit->roles = artist->roles;
credit->full_roles = artist->full_roles;
credit->artists.append_single(artist);
credits.append_single(credit);
}
}
}
}
//todo: cleanup/fix alphanumeric notation
unsigned long long encode_track_position(const pfc::string8 &pos) {
// first 1-8 characters encoded
unsigned long long result = 0;
unsigned char temp = 0;
bool use_temp = false;
size_t count = 0;
const size_t length = pos.get_length();
for (size_t i = 0; count < 8 && i < length; i++) {
if (pos[i] >= 48 && pos[i] <= 57) {
temp = temp * 10 + (pos[i] - 48);
use_temp = true;
}
else {
if (use_temp) {
result |= temp;
use_temp = false;
temp = 0;
count++;
if (count < 7) {
result <<= 8;
}
}
result |= pos[i];
count++;
if (count < 7) {
result <<= 8;
}
}
}
if (use_temp) {
result |= temp;
count++;
if (count < 7) {
result <<= 8;
}
}
if (count < 7) {
result <<= (count * 8);
}
return result;
}
bool track_in_range(const pfc::string8 &pos, const pfc::string8 &start, const pfc::string8 &end) {
unsigned long long dpos = encode_track_position(pos);
unsigned long long dstart = encode_track_position(start);
unsigned long long dend = encode_track_position(end);
return dpos >= dstart && dpos <= dend;
}
void Discogs::DistReleaseTrackCredits(const pfc::array_t<pfc::string8>& arrTracks, ReleaseCredit_ptr& credit, Release* release) {
pfc::string8 alltracks_csv;
std::vector<pfc::string8> v_media_desc;
nota_info n_info;
size_t cTracks = arrTracks.get_count();
if (!cTracks) {
//nothing to do
return;
}
auto dc_credit_pos = arrTracks[0];
// check credit movement
pfc::string8 mov_credit_pos;
if (release->indexes.get_count() && dc_credit_pos.get_length() > 2) {
std::vector<pfc::string8> vindex_mov_nums;
for each (ReleaseIndexes_ptr var in release->indexes) {
auto bignum = extract_max_number(var->title, 'z', false);
if (bignum.get_length()) {
vindex_mov_nums.emplace_back(bignum);
}
}
if (std::find(std::begin(vindex_mov_nums), std::end(vindex_mov_nums), dc_credit_pos.c_str()) != std::end(vindex_mov_nums)) {
std::vector<pfc::string8> vsplit;
split(dc_credit_pos, "-", 0, vsplit);
for (auto wmovement : vsplit) {
if (!trim(wmovement).get_length()) {
continue;
}
auto relh = release->indexes;
for each (ReleaseIndexes_ptr var in relh) {
std::vector<std::string> mylist = { "Concerto","Sonata", "Suite" };
auto found_it = std::find_if(mylist.begin(), mylist.end(), [=](const std::string& w) {
return var->title.contains(w.c_str());
});
if (found_it != mylist.end()) {
if (var->title.contains(wmovement)) {
if (mov_credit_pos.get_length()) {
mov_credit_pos << ",";
}
mov_credit_pos = var->dc_track_first;
if (!var->dc_track_first.equals(var->dc_track_last)) {
mov_credit_pos << " to " << var->dc_track_last;
}
break;
}
}
}
}
}
}
dc_credit_pos = mov_credit_pos.get_length() ? mov_credit_pos : dc_credit_pos;
//.. end check credit movement
pfc::array_t<pfc::string8> inner_parts;
size_t inner_count = tokenize(dc_credit_pos, " to ", inner_parts, true);
if (!inner_count || inner_count > 2) {
parser_exception ex;
ex << "Error parsing release credits.";
throw ex;
}
else if (inner_count < 2) {
int tmp_disc, tmp_track;
replace_track_volume_desc(inner_parts[0], n_info, v_media_desc);
bool bres = release->find_std_disc_track(inner_parts[0], &tmp_disc, &tmp_track);
if (bres) {
if (tmp_disc < release->discs.get_size() && tmp_track < release->discs[tmp_disc]->tracks.get_size())
release->discs[tmp_disc]->tracks[tmp_track]->credits.append_single(credit);
}
}
else if (inner_count == 2) {
std::vector<std::pair<size_t, size_t>> inner_std_parts;
int tmp_disc, tmp_track;
// ->o to p
replace_track_volume_desc(inner_parts[0], n_info, v_media_desc);
{
bool bresleft = release->find_std_disc_track(inner_parts[0], &tmp_disc, &tmp_track);
if (bresleft) {
inner_std_parts.emplace_back(tmp_disc, tmp_track);
}
}
// o to ->p
replace_track_volume_desc(inner_parts[1], n_info, v_media_desc);
{
bool bresright = release->find_std_disc_track(inner_parts[1], &tmp_disc, &tmp_track);
if (bresright) {
inner_std_parts.emplace_back(tmp_disc, tmp_track);
}
}
if (inner_std_parts.size() == 2) {
pfc::string8 range_botton = PFC_string_formatter() << inner_std_parts[0].first << "." << inner_std_parts[0].second;
pfc::string8 range_top = PFC_string_formatter() << inner_std_parts[1].first << "." << inner_std_parts[1].second;
//check bounds
if (inner_std_parts[0].first < release->discs.get_size() &&
inner_std_parts[1].first < release->discs.get_size() &&
inner_std_parts[0].second < release->discs[inner_std_parts[0].first]->tracks.get_size() &&
inner_std_parts[1].second < release->discs[inner_std_parts[1].first]->tracks.get_size()) {
for (size_t d = inner_std_parts[0].first; d <= inner_std_parts[1].first; d++) {
for (size_t t = 0; t < release->discs[d]->tracks.get_size(); t++) {
pfc::string8 pos = release->discs[d]->tracks[t]->discogs_track_number;
if (release->find_std_disc_track(pos, &tmp_disc, &tmp_track)) {
pos = PFC_string_formatter() << tmp_disc << "." << tmp_track;
//todo: fix/cleanup to support release track notation
if (track_in_range(pos, range_botton, range_top)) {
release->discs[d]->tracks[t]->credits.append_single(credit);
}
}
}
}
}
}
}
if (cTracks > 1) {
//RECURSION (TAIL)
//todo: pass 'from' position instead
pfc::array_t<pfc::string8> arrTracksTail; arrTracksTail.resize(arrTracks.get_count() - 1);
int c = 1;
for (auto& ti : arrTracksTail) {
ti = arrTracks[c++];
}
DistReleaseTrackCredits(arrTracksTail, credit, release);
}
}
ReleaseLabel_ptr Discogs::parseReleaseLabel(json_t *element) {
ReleaseLabel_ptr label(new ReleaseLabel());
label->name = JSONAttributeString(element, "name");
label->id = JSONAttributeString(element, "id");
label->catalog = JSONAttributeString(element, "catno");
return std::move(label);
}
void Discogs::parseReleaseLabels(json_t *element, pfc::array_t<ReleaseLabel_ptr> &release_labels) {
if (json_is_array(element)) {
for (size_t i = 0; i < json_array_size(element); i++) {
release_labels.append_single(parseReleaseLabel(json_array_get(element, i)));
}
}
}
ReleaseCompany_ptr Discogs::parseReleaseCompany(json_t *element) {
ReleaseCompany_ptr company(new ReleaseCompany());
company->name = JSONAttributeString(element, "name");
company->id = JSONAttributeString(element, "id");
company->catalog = JSONAttributeString(element, "catno");
company->entity_type_name = JSONAttributeString(element, "entity_type_name");
return std::move(company);
}
void Discogs::parseReleaseCompanies(json_t *element, pfc::array_t<ReleaseCompany_ptr> &release_companies) {
if (json_is_array(element)) {
for (size_t i = 0; i < json_array_size(element); i++) {
release_companies.append_single(parseReleaseCompany(json_array_get(element, i)));
}
}
}
ReleaseSeries_ptr Discogs::parseReleaseSeries(json_t *element) {
ReleaseSeries_ptr series(new ReleaseSeries());
series->name = JSONAttributeString(element, "name");
series->id = JSONAttributeString(element, "id");
series->api_url = JSONAttributeString(element, "resource_url");
series->number = JSONAttributeString(element, "catno");
return std::move(series);
}
void Discogs::parseReleaseSeries(json_t *element, pfc::array_t<ReleaseSeries_ptr> &release_series) {
if (json_is_array(element)) {
for (size_t i = 0; i < json_array_size(element); i++) {
release_series.append_single(parseReleaseSeries(json_array_get(element, i)));
}
}
}
Image_ptr Discogs::parseImage(json_t *element) {
Image_ptr image(new ExpTagsImage());
// Should we use thumb instead of url150?
image->type = JSONAttributeString(element, "type");
image->url = JSONAttributeString(element, "resource_url");
image->url150 = JSONAttributeString(element, "uri150");
image->width = JSONAttributeString(element, "width");
image->height = JSONAttributeString(element, "height");
return std::move(image);
}
void Discogs::parseImages(json_t *array, pfc::array_t<Image_ptr> &images) {
pfc::array_t<Image_ptr> temp;
if (json_is_array(array)) {
for (size_t i = 0; i < json_array_size(array); i++) {
json_t *img = json_array_get(array, i);
if (json_is_object(img)) {
Image_ptr image = parseImage(img);
if (STR_EQUAL(image->type, "primary")) {
images.append_single(std::move(image));
}
else {
temp.append_single(std::move(image));
}
}
}
}
images.append(temp);
}
ReleaseFormat_ptr Discogs::parseReleaseFormat(json_t *element) {
ReleaseFormat_ptr format(new ReleaseFormat());
format->set_name(JSONAttributeString(element, "name"));
format->qty = JSONAttributeString(element, "qty");
format->descriptions = JSONAttributeStringArray(element, "descriptions");
format->text = JSONAttributeString(element, "text");
return format;
}
void Discogs::parseReleaseFormats(json_t *element, pfc::array_t<ReleaseFormat_ptr> &formats) {
if (json_is_array(element)) {
for (size_t i = 0; i < json_array_size(element); i++) {
json_t *format = json_array_get(element, i);
formats.append_single(parseReleaseFormat(format));
}
}
}
bool remove_chars(char c) {
return !((c >= '0' && c <= '9') || c == '-');
}
size_t get_last_num(pfc::string8 str, size_t minlength, size_t minpos, pfc::string8& lastnum) {
if (pfc::string_is_numeric(str)) return ~0;
if (str.get_length() >= minlength) {
std::regex regex_v("[\\d]+");
std::string str_reg(str.c_str());
std::sregex_iterator begin = std::sregex_iterator(str_reg.begin(), str_reg.end(), regex_v);
for (std::sregex_iterator i = begin; i != std::sregex_iterator(); i++) {
if (i->position() >= minpos) {
lastnum = i->str().c_str();
return i->position();
}
}
}
return ~0;
}
struct ptp_nfo {
pfc::string8 dctn;
pfc::string8 vol_preffix;
pfc::string8 trk_postfix;
pfc::string8 subtrk_postfix;
pfc::string8 hidden_postfix;
bool bvol_alpha;
bool btrk_alpha;
pfc::string8 format;
pfc::string8 altformat;
size_t format_qty;
size_t ql_format_qty;
bool bformat_incl_vol;
bool bformat_incl_sideb;
ReleaseFormat_ptr p_rf;
ptp_nfo() {
bvol_alpha = false;
btrk_alpha = false;
format_qty = 0;
ql_format_qty = ~0;
bformat_incl_vol = false;
bformat_incl_sideb = false;
}
};
struct trk_name_nfo {
const pfc::string8 dctn;
pfc::string8 tn_nosub;
pfc::string8 tn_nosub_chopped;
pfc::string8 tn_std;
bool isIndex;
trk_name_nfo(const pfc::string8 dctn, bool isIndex): dctn(dctn), isIndex(isIndex) {
//..
}
};
// 1..N in/out params
// second are start/end release format positions
std::pair<ReleaseFormat_ptr, LPARAM> get_format_name(pfc::array_t<ReleaseFormat_ptr>* formats, size_t clay_pos) {
std::pair<ReleaseFormat_ptr, size_t> no_res(NULL, NULL);
if (clay_pos == ~0) return no_res;
//todo: sec boxes
const ReleaseFormat* wf = (*formats)[0].get();
size_t boxset_offset = static_cast<size_t>(wf->is_box());
size_t cDisc = 0;
for (size_t w = 0; w < formats->get_count(); w++) {
const ReleaseFormat_ptr wf = (*formats)[w];
auto qty = atoi(wf->get_quantity().print().c_str());
cDisc += qty;
if (clay_pos + boxset_offset <= cDisc) {
cDisc -= boxset_offset;
LPARAM range = MAKELPARAM(cDisc - qty + 1, cDisc);
return std::pair((*formats)[w], range);
}
}
return no_res;
}
void get_format_info(const pfc::string8 dc_track, const std::pair<ReleaseFormat_ptr, size_t> format_nfo_curr, const std::pair<ReleaseFormat_ptr, size_t> format_nfo_next,
int curr_disc_number, ptp_nfo& ptpos, pfc::string8& chop, bool& bjump, size_t& jump_disc) {
bjump = false;
jump_disc = ~0;
if (format_nfo_next.first) {
pfc::string8 name = format_nfo_curr.first->get_name();
ptpos.altformat = name;
bool bname = dc_track.has_prefix(name);
if (bname) {
if (format_nfo_curr.first == ptpos.p_rf) {
if (ptpos.ql_format_qty != ~0) {
chop = name;
}
}
else {
chop = name;
ptpos.format = chop;
ptpos.p_rf = format_nfo_curr.first;
ptpos.ql_format_qty = atoi(ptpos.p_rf->qty);
ptpos.bformat_incl_vol = format_nfo_curr.first->format_incl_vol;
}
}
else {
for (size_t wdesc = 0; wdesc < format_nfo_curr.first->descriptions.get_count(); wdesc++) {
auto desc = format_nfo_curr.first->descriptions[wdesc];
if (dc_track.has_prefix(desc)) {
if (format_nfo_curr.first == ptpos.p_rf) {
if (ptpos.ql_format_qty != ~0) {
chop = desc;
break;
}
}
else {
chop = desc;
ptpos.format = chop;
ptpos.p_rf = format_nfo_curr.first;
ptpos.ql_format_qty = atoi(ptpos.p_rf->qty);
ptpos.bformat_incl_vol = format_nfo_curr.first->format_incl_vol;
break;
}
}
}
}
//todo: trace
jump_disc = curr_disc_number;
}
if (!chop.get_length()) {
//TRY NEXT FORMAT...
if (format_nfo_next.first) {
pfc::string8 name = format_nfo_next.first->get_name();
ptpos.altformat = name;
bool bname = dc_track.has_prefix(name);
if (bname) {
if (format_nfo_next.first == ptpos.p_rf) {
if (ptpos.ql_format_qty != ~0) {
chop = name;
}
}
else {
chop = name;
ptpos.format = chop;
ptpos.p_rf = format_nfo_next.first;
ptpos.ql_format_qty = atoi(ptpos.p_rf->qty);
ptpos.bformat_incl_vol = format_nfo_next.first->format_incl_vol;
}
}
else {
for (size_t wdesc = 0; wdesc < format_nfo_next.first->descriptions.get_count(); wdesc++) {
auto desc = format_nfo_next.first->descriptions[wdesc];
if (dc_track.has_prefix(desc)) {
if (format_nfo_next.first == ptpos.p_rf) {
if (ptpos.ql_format_qty != ~0) {
{
chop = desc;
break;
}
}
else {
chop = desc;
ptpos.format = chop;
ptpos.p_rf = format_nfo_next.first;
ptpos.ql_format_qty = atoi(ptpos.p_rf->qty);
ptpos.bformat_incl_vol = format_nfo_next.first->format_incl_vol;
break;
}
}
}
}
}
if (chop.get_length()) {
bjump = true;
jump_disc = LOWORD(format_nfo_next.second);
}
}
}
}
// PARSE TRACK POSITION
void parseTrackPosition(ReleaseTrack_ptr& track, const std::pair<ReleaseFormat_ptr, size_t> format_nfo_curr, const std::pair<ReleaseFormat_ptr, size_t> format_nfo_next,
ptp_nfo & ptpos, bool multi_format,int curr_vol_number, int next_track_number) {
if (!(bool)curr_vol_number) {
//first run
curr_vol_number = 1;
}
//rewind-hidden track 0 not supported
if (track->discogs_track_number.equals("-1")) {
track->discogs_track_number = "1a";
}