Implement copy function for files
This commit is contained in:
25
scraper.py
25
scraper.py
@@ -2,6 +2,8 @@
|
|||||||
# ex: set filetype=python
|
# ex: set filetype=python
|
||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
|
import os
|
||||||
|
from shutil importy copyfile
|
||||||
import xml.etree.cElementTree as xml
|
import xml.etree.cElementTree as xml
|
||||||
|
|
||||||
class Track():
|
class Track():
|
||||||
@@ -13,7 +15,7 @@ class Track():
|
|||||||
def __init__(self, xml_doc):
|
def __init__(self, xml_doc):
|
||||||
it = iter(xml_doc)
|
it = iter(xml_doc)
|
||||||
for k, v in zip(it, it):
|
for k, v in zip(it, it):
|
||||||
setattr(self, k.text.replace(' ', ''), v)
|
setattr(self, k.text.replace(' ', ''), v.text)
|
||||||
|
|
||||||
def parseXml(xml_file):
|
def parseXml(xml_file):
|
||||||
"""
|
"""
|
||||||
@@ -26,6 +28,20 @@ def parseXml(xml_file):
|
|||||||
tracks.append(Track(t))
|
tracks.append(Track(t))
|
||||||
return tracks
|
return tracks
|
||||||
|
|
||||||
|
def copy_file(track, targetFolder):
|
||||||
|
"""
|
||||||
|
Copies a track from its source to the destination folder
|
||||||
|
"""
|
||||||
|
path = track.Location[7:]
|
||||||
|
dst = os.path.join(targetFolder, os.path.basename(path))
|
||||||
|
if (os.path.exists(path)):
|
||||||
|
print('Copying file {0}', path)
|
||||||
|
copyfile(path, dst)
|
||||||
|
else:
|
||||||
|
print('File {0} not found.')
|
||||||
|
return dst
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
"""
|
"""
|
||||||
Setup our main program
|
Setup our main program
|
||||||
@@ -38,8 +54,15 @@ def main():
|
|||||||
parser.add_argument('libraryFile', help='The iTunes library file in XML format.')
|
parser.add_argument('libraryFile', help='The iTunes library file in XML format.')
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
print('Parsing library file...')
|
||||||
tracks = parseXml(args.libraryFile)
|
tracks = parseXml(args.libraryFile)
|
||||||
|
print('{0} tracks found.'.format(len(tracks)))
|
||||||
|
|
||||||
|
if not os.path.isdir(args.target):
|
||||||
|
os.mkdir(args.target)
|
||||||
|
|
||||||
|
for t in tracks:
|
||||||
|
new_file = copy_file(t, args.target)
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
|||||||
Reference in New Issue
Block a user