Skip to content

Instantly share code, notes, and snippets.

@tigerwang202
Created April 30, 2014 19:50
Show Gist options
  • Save tigerwang202/9939571001d551ab3281 to your computer and use it in GitHub Desktop.
Save tigerwang202/9939571001d551ab3281 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
# 导入未被iTurns收录的MP3歌曲
# wmy 2014-4-30
import os
import re
import shutil
iturns_library_path = '/Users/wang/Music/iTunes/iTunes Media/Music'
target_library_path = '/Volumes/共享/我喜欢的音乐' # 需导入歌曲目录
# 匹配文件名开头的音轨编号,例如“01 ”、“01. ”、“01- ”
songPerfix = re.compile(r'\A[0-9\-\.\s]*')
# 按‘-’、‘.’、空格、‘_’ 分割文件名
songSplit = re.compile(ur'\s*[-\[\]\._[]-]+\s*', re.UNICODE)
songs = [] # 保存iturns library中歌曲名
def main():
# 扫描iturns library,获取所有歌曲名
for root, dirs, files in os.walk(iturns_library_path, True):
for name in files:
if name == '.DS_Store':
continue
songs.append(songPerfix.sub('', os.path.splitext(name)[0]))
print 'iTurns library has ', len(songs), 'songs\r\n'
#print "\n".join(songs)
# 创建目录用于保存新增歌曲
songDir = os.path.join(target_library_path, 'new song')
if not os.path.exists(songDir):
os.makedirs(songDir)
print 'Make new song dir.'
# 搜索iturns library中未包含歌曲
for root, dirs, files in os.walk(target_library_path, True):
for name in files:
if not re.search(r'(\.mp3|\.MP3)\Z', name):
print 'Skip invalid file ', name
continue
songTitle = songSplit.split(
unicode(os.path.splitext(name)[0], 'utf-8'))
isNewSong = True
for s in songs:
if unicode(s, 'utf-8') in songTitle:
isNewSong = False
print 'Exclude ', name, ' {', s, '}'
break
if isNewSong is True:
# skip duplicate file
if not os.path.isfile(os.path.join(songDir, name)):
print 'Copy ', name
shutil.copy(os.path.join(root, name), songDir)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment