Advertisement
TankorSmash

Solved id3 regex

Jul 3rd, 2012
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.55 KB | None | 0 0
  1. import re
  2.  
  3. #v3 tags: http://pastebin.com/VJEBGauL
  4. #v2 tags: http://pastebin.com/z9mz71yd
  5.  
  6. #----------------------------------------------------------------------
  7. def parseID3v3():
  8.     """saves the tags as a dict"""
  9.     tags = {}
  10.     #ID3 tags http://stackoverflow.com/questions/11319123/grab-two-parts-of-a-single-short-string
  11.     with open(r'..\Actual\ID3v3 tags.txt')as f:
  12.         for line in f.readlines():
  13.             full_match = re.search(r'^\S+\s+(\S+)\s+\[#\S+ (.*?)\]', line)
  14.             dict_key = full_match.group(1)   # 'AENC'
  15.             id3v3_tag = full_match.group(2)  # 'Audio encryption'
  16.             print dict_key, ':', id3v3_tag
  17.             tags[dict_key] = id3v3_tag
  18.            
  19.     return tags
  20.  
  21. #----------------------------------------------------------------------
  22. def parseID3v2():
  23.     """saves the tags as a dict"""
  24.     tags = {}
  25.     #ID3 tags http://stackoverflow.com/questions/11319123/grab-two-parts-of-a-single-short-string
  26.     with open(r'..\Actual\ID3v2 tags.txt')as f:
  27.         for line in f.readlines():
  28.             try:
  29.                    
  30.                 line = line.strip()
  31.                 full_match = re.search(r'^\S+\s+(\S+)+\s+(\S+)', line)
  32.                 dict_key = full_match.group(1)   # 'AENC'
  33.                 id3v2_tag = full_match.group(2)  # 'Audio encryption'
  34.                 print dict_key, ':', id3v2_tag
  35.                 tags[dict_key] = id3v2_tag
  36.             except AttributeError as e:
  37.                 print e, 'with line: >>', line, '<<'
  38.            
  39.     return tags
  40.  
  41. print parseID3v3().keys()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement