Fixed the incomplete file issue due to not understanding Mutagens
tagging process.

Also added in user_agent and requests session, the user-agent because
its proper and session because it increases performance slightly by
using keep-alive by default.
master
Anthony Forsberg 2017-01-13 14:15:42 -05:00
parent c74e82ba96
commit feb6d93bb9
3 changed files with 18 additions and 13 deletions

View File

@ -51,7 +51,7 @@ from .bandcampdownloader import BandcampDownloader
def main(): def main():
arguments = docopt(__doc__, version='bandcamp-dl 0.0.7-01') arguments = docopt(__doc__, version='bandcamp-dl 0.0.7-02')
bandcamp = Bandcamp() bandcamp = Bandcamp()
basedir = arguments['--base-dir'] or os.getcwd() basedir = arguments['--base-dir'] or os.getcwd()

View File

@ -16,6 +16,9 @@ class BandcampDownloader:
:param directory: download location :param directory: download location
:param overwrite: if True overwrite existing files :param overwrite: if True overwrite existing files
""" """
self.headers = {'user_agent': 'bandcamp-dl/0.0.7-02 (https://github.com/iheanyi/bandcamp-dl)'}
self.session = requests.Session()
if type(urls) is str: if type(urls) is str:
self.urls = [urls] self.urls = [urls]
@ -95,7 +98,7 @@ class BandcampDownloader:
while True: while True:
try: try:
r = requests.get(track['url'], stream=True) r = self.session.get(track['url'], headers=self.headers, stream=True)
file_length = int(r.headers['content-length']) file_length = int(r.headers['content-length'])
total = int(file_length/100) total = int(file_length/100)
# If file exists and is still a tmp file skip downloading and encode # If file exists and is still a tmp file skip downloading and encode
@ -110,13 +113,16 @@ class BandcampDownloader:
skip = True skip = True
break break
with open(filepath, "wb") as f: with open(filepath, "wb") as f:
dl = 0 if file_length is None:
for data in r.iter_content(chunk_size=total): f.write(r.content)
dl += len(data) else:
f.write(data) dl = 0
done = int(50 * dl / file_length) for data in r.iter_content(chunk_size=total):
sys.stdout.write("\r({}/{}) [{}{}] :: Downloading: {}".format(self.track_num, self.num_tracks, "=" * done, " " * (50 - done), filename[:-8])) dl += len(data)
sys.stdout.flush() f.write(data)
done = int(50 * dl / file_length)
sys.stdout.write("\r({}/{}) [{}{}] :: Downloading: {}".format(self.track_num, self.num_tracks, "=" * done, " " * (50 - done), filename[:-8]))
sys.stdout.flush()
local_size = os.path.getsize(filepath) local_size = os.path.getsize(filepath)
# if the local filesize before encoding doesn't match the remote filesize redownload # if the local filesize before encoding doesn't match the remote filesize redownload
if local_size != file_length and attempts != 3: if local_size != file_length and attempts != 3:
@ -140,7 +146,7 @@ class BandcampDownloader:
if album['art']: if album['art']:
try: try:
with open(dirname + "/cover.jpg", "wb") as f: with open(dirname + "/cover.jpg", "wb") as f:
r = requests.get(album['art'], stream=True) r = self.session.get(album['art'])
f.write(r.content) f.write(r.content)
except Exception as e: except Exception as e:
print(e) print(e)
@ -173,7 +179,6 @@ class BandcampDownloader:
audio["date"] = meta['date'] audio["date"] = meta['date']
audio.save() audio.save()
audio.save(filepath[:-4]) os.rename(filepath, filepath[:-4])
os.remove(filepath)
sys.stdout.write("\r({}/{}) [{}] :: Finished: {}".format(self.track_num, self.num_tracks, "=" * 50, filename)) sys.stdout.write("\r({}/{}) [{}] :: Finished: {}".format(self.track_num, self.num_tracks, "=" * 50, filename))

View File

@ -6,7 +6,7 @@ here = path.abspath(path.dirname(__file__))
setup( setup(
name='bandcamp-downloader', name='bandcamp-downloader',
version='0.0.7-01', version='0.0.7-02',
description='bandcamp-dl downloads albums and tracks from Bandcamp for you', description='bandcamp-dl downloads albums and tracks from Bandcamp for you',
long_description=open('README.rst').read(), long_description=open('README.rst').read(),
url='https://github.com/iheanyi/bandcamp-dl', url='https://github.com/iheanyi/bandcamp-dl',