Skip to content

Instantly share code, notes, and snippets.

@grahams
Created April 21, 2021 17:30
Show Gist options
  • Save grahams/eb23cf7b35398fb680f6ff59971d4d44 to your computer and use it in GitHub Desktop.
Save grahams/eb23cf7b35398fb680f6ff59971d4d44 to your computer and use it in GitHub Desktop.
import requests
import asyncio
from dataclasses import dataclass
from pathlib import Path
from os.path import expanduser
@dataclass
class Track:
title: str
artist: str
album: str
length: str
artworkURL: str
uniqueId: str
ignore: bool
def fetchArtwork(self, coverImagePath):
p = Path(f'{expanduser(coverImagePath)}/{self.uniqueId}.jpg')
asyncio.run(fetch(p, self.artworkURL))
return p
async def fetch(p, artworkURL):
if(p.is_file() == False):
with p.open(mode='wb') as handle:
response = requests.get(artworkURL, stream=True)
if not response.ok:
return False
for block in response.iter_content(1024):
if not block:
return False
handle.write(block)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment