Guest User

python path

a guest
Aug 27th, 2020
337
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 9.35 KB | None | 0 0
  1.  
  2. paths:
  3.     #regex to match all items. (avoid having to separately define compilations and singletons.)
  4.     title::.*: "$alb_main$i_cust_catalog$alb_main_suffix/$i_main"
  5.    
  6. album_fields:
  7.     alb_main: |
  8.         from titlecase import titlecase
  9.         import beets.plugins
  10.         pthe = beets.plugins.template_funcs()['the']
  11.         def alb_promo_boot(albumstatus):
  12.             # official, promo, bootleg, and pseudo-release. we only note the middle two. https://musicbrainz.org/doc/Release#Status
  13.             if 'Promo' in albumstatus:
  14.                 return 'Promo'
  15.             elif 'Bootleg' in albumstatus:
  16.                 return 'Bootleg'
  17.             else:
  18.                 return ''
  19.         def alb_exotic_type(albumtype):
  20.             # no special noting of albums, EPs, and soundtracks
  21.             if albumtype in {'album', 'ep', 'soundtrack'}:
  22.                 return ''
  23.             # special case for proper spacing and caps
  24.             elif 'spokenword' in albumtype:
  25.                 return 'Spoken Word'
  26.             else:
  27.                 return str.title(albumtype)
  28.         def alb_title(album, mb_albumartistid, allowed_length=3):
  29.             '''convert long allcaps titles to title case, while leaving other titles alone'''
  30.             # maybe this should be refactored into a per-country blacklist?
  31.             # Japan loves allcaps but other countries are more selective.
  32.             allowed_allcaps_artists = ['573fd3e2-3f61-4329-a6c1-89e20620b0b9']
  33.             if album.isupper and len(album) > allowed_length:
  34.                 if mb_albumartistid not in allowed_allcaps_artists:
  35.                     return titlecase(album)
  36.             if len(album) < 1:
  37.                 return "[untitled]"
  38.             return album
  39.         def alb_artist(albumartist, items):
  40.             # get this from beets config itself?
  41.             va_str = "Various Artists"
  42.             if albumartist:
  43.                 return albumartist
  44.             else:
  45.                 # the album probably has no MBid
  46.                 # return va_str if multiple artists are found on the album
  47.                 my_artists = set()
  48.                 for item in items:
  49.                     my_artists.add(item.artist)
  50.                     if len(my_artists) > 1:
  51.                         return va_str
  52.                 # every artist is the same
  53.                 return my_artists.pop()
  54.         def alb_orig_year(item):
  55.             if item.original_year > 0:
  56.                 return str(item.original_year)
  57.             if item.year > 0:
  58.                 return str(item.year)
  59.             else:
  60.                 return ''
  61.         def alb_main():
  62.             # the output string
  63.             out = ''
  64.             myartist = alb_artist(albumartist, items)
  65.             if myartist:
  66.                 out += pthe(myartist)
  67.             else:
  68.                 out += "No Artist"
  69.             out += " - "
  70.             if alb_promo_boot(albumstatus):
  71.                 out += alb_promo_boot(albumstatus)
  72.                 if alb_exotic_type(albumtype):
  73.                     # example: Promo, Spoken Word
  74.                     out += ", "
  75.                 else:
  76.                     out += " - "
  77.             if alb_exotic_type(albumtype):
  78.                 out += alb_exotic_type(albumtype) + " - "
  79.             # just grab the first item since original_year is
  80.             # apparently an item level field???? need to verify
  81.             out += alb_orig_year(items[0]) + " - "
  82.             out += alb_title(album, mb_albumartistid)
  83.             # % aunique{
  84.             #     album alb_orig_year_mm_dd albumartist albumstatus albumtype,
  85.             #     albumdisambig country label catalognum,
  86.             #     ()
  87.             # }
  88.             return out
  89.         return alb_main()
  90.     alb_main_suffix: |
  91.         from random import seed, sample
  92.         def alb_sample_tracks(tracks):
  93.             # reproducible randomness for debugging. should be excluded for optimal randomness from album to album.
  94.             # otherwise, any album with same number of tracks will get same sample of track numbers, I think
  95.             seed(25)
  96.             num_items = len(tracks)
  97.             tracks_to_check = 10
  98.             sample_size = min(tracks_to_check, num_items)
  99.             test_tracks = sample(tracks, sample_size)
  100.             return(test_tracks)
  101.         def alb_subset_bitrate(items, min_kbps=150):
  102.             '''get bitrate information for albums <150kbps.
  103.            do nothing for higher-bitrate albums, because
  104.            they are assumed to be perceptually lossless.'''
  105.             # Should probably have some logic for surround sound (>2 channels),
  106.             # but if you're dealing with audibly lossy surround tracks,
  107.             # something has gone wrong in your life.
  108.             test_tracks = alb_sample_tracks(items)
  109.             sample_size = len(test_tracks)
  110.             total_bitrate = 0
  111.             for item in test_tracks:
  112.                 total_bitrate += item.bitrate
  113.             album_br_bits_ps = total_bitrate / sample_size
  114.             # music bitrates are base 10:
  115.             # https://hydrogenaud.io/index.php/topic,12633.0.html
  116.             album_br_kbps = album_br_bits_ps / 1000
  117.             if album_br_kbps >= min_kbps:
  118.                 out_text = ''
  119.             else:
  120.                 # return Opus, MP3, "Mixed", etc.
  121.                 # uses same test_tracks from earlier sample
  122.                 alb_format = set()
  123.                 for item in test_tracks[0:sample_size]:
  124.                     alb_format.add(item.format)
  125.                 if len(alb_format) != 1:
  126.                     alb_format = 'Mixed'
  127.                 else:
  128.                     alb_format = alb_format.pop()
  129.                 out_text = str(int(album_br_kbps)) + 'kbps ' + str(alb_format)
  130.             return out_text
  131.         def alb_main_suffix():
  132.             out = ''
  133.             mybitrate = alb_subset_bitrate(items)
  134.             if mybitrate:
  135.                 out += "[" + alb_subset_bitrate + "]"
  136.             return out
  137.         return alb_main_suffix()
  138.  
  139.  
  140. item_fields:
  141.     i_cust_catalog: |
  142.         def i_reissue_year(year, original_year):
  143.             if year > original_year:
  144.                 if original_year > 0:
  145.                     return str(year)
  146.             return ''
  147.         def i_cust_media(media):
  148.             # see https://musicbrainz.org/doc/Release/Format
  149.             # omit uninteresting formats
  150.             # these formats are bit-identical to CD
  151.             snake_oil_formats = ['Blu-spec CD', 'SHM-CD', 'HQCD']
  152.             media_types_to_omit = snake_oil_formats + \
  153.                 ['CD', 'CD-R', 'Enhanced CD', 'CDDA', 'Digital Media', '']
  154.             if media in media_types_to_omit:
  155.                 return ''
  156.             # combine hybrid SACD with SACD, see https://en.wikipedia.org/wiki/Super_Audio_CD#Technology
  157.             elif 'SACD' in media:
  158.                 return 'SACD'
  159.             # combine all vinyl types into "Vinyl"
  160.             elif 'Vinyl' in media:
  161.                 # https://en.wikipedia.org/wiki/VinylDisc
  162.                 if media == 'VinylDisc':
  163.                     # assume it's a CD
  164.                     return ''
  165.                 return 'Vinyl'
  166.             elif "USB" in media:
  167.                 return 'USB'
  168.             elif media == 'HD-DVD':
  169.                 return 'HD-DVD'
  170.             elif 'DVD' in media or media == "DualDisc":
  171.                 return 'DVD'
  172.             else:
  173.                 return media
  174.         parsed_media = i_cust_media(media)
  175.         result = i_reissue_year(year, original_year)
  176.         # condition = custom attribute to note vinyl skips
  177.         # and other noticable artifacts.
  178.         result += ' ' + parsed_media  # + i_condition()
  179.         result = result.strip(' ')
  180.         if result != '':
  181.             return ' (' + result + ')'
  182.         return result
  183.     i_main: |
  184.         def i_track(track):
  185.             # untested
  186.             if not track:
  187.                 return None
  188.             # hotfix for beets' hardcoded 2-digit track numbers
  189.             # https://github.com/beetbox/beets/issues/3352
  190.             str_track = str(track)
  191.             # pad based on length of highest track number (per disc).
  192.             # may interact with per_disc_numbering, not sure
  193.             length = len(str(tracktotal))
  194.             if length < 2:
  195.                 length = 2
  196.             return str_track.zfill(length)
  197.         def i_disc_layer(disc, disctotal):
  198.             # do nothing for single-disc releases
  199.             if disctotal > 1:
  200.                 # todo should this be 'is not None'?
  201.                 if disc != '':
  202.                     str_disc = str(disc)
  203.                     # pad based on length of highest disc number.
  204.                     # ex. if total discs = 2 digits, pad 1 zero for discs 1-9 (01-09)
  205.                     legth_to_pad_to = len(str(disctotal))
  206.                     return str_disc.zfill(legth_to_pad_to)
  207.             return ''
  208.         def i_main():
  209.             out = ''
  210.             disc_layer = i_disc_layer(disc, disctotal)
  211.             if disc_layer:
  212.                 # no space
  213.                 out += disc_layer + "-"
  214.             mytrack = i_track(track)
  215.             if mytrack:
  216.                 out += mytrack
  217.             else:
  218.                 out += "[no track number]"
  219.             out += ". "
  220.             if title:
  221.                 out += str(title)
  222.             else:
  223.                 out += '[unknown]'
  224.             return out
  225.         return i_main()
Advertisement
Add Comment
Please, Sign In to add comment