Add Try/Except block

This commit is contained in:
administrator 2023-12-11 15:53:11 +01:00
parent 630f756fa6
commit 7b2bb3dca6
1 changed files with 18 additions and 5 deletions

View File

@ -491,12 +491,25 @@ class Source(models.Model):
@property @property
def get_thumbnail_url(self): def get_thumbnail_url(self):
if self.source_type==Source.SOURCE_TYPE_YOUTUBE_PLAYLIST: if self.source_type == self.SOURCE_TYPE_YOUTUBE_PLAYLIST:
raise Exception('This source is a playlist so it doesn\'t have thumbnail.') raise Exception('This source is a playlist so it doesn\'t have thumbnail.')
soup = BeautifulSoup(requests.get(self.url, cookies={'CONSENT': 'YES+1'}).text, "html.parser")
try:
response = requests.get(self.url, cookies={'CONSENT': 'YES+1'})
response.raise_for_status()
except RequestException as e:
print(f"Error occurred while making a request to YouTube: {e}")
return None
soup = BeautifulSoup(response.text, "html.parser")
try:
data = re.search(r"var ytInitialData = ({.*});", str(soup.prettify())).group(1) data = re.search(r"var ytInitialData = ({.*});", str(soup.prettify())).group(1)
json_data = json.loads(data) json_data = json.loads(data)
return json_data["header"]["c4TabbedHeaderRenderer"]["avatar"]["thumbnails"][2]["url"] return json_data["header"]["c4TabbedHeaderRenderer"]["avatar"]["thumbnails"][2]["url"]
except (KeyError, ValueError, TypeError) as e:
print(f"Error occurred while parsing YouTube JSON: {e}")
return None
def directory_exists(self): def directory_exists(self):
return (os.path.isdir(self.directory_path) and return (os.path.isdir(self.directory_path) and