add image exception
This commit is contained in:
		@@ -197,43 +197,46 @@ class UpstreamHandler():
 | 
			
		||||
        try:
 | 
			
		||||
            embed_url = f'https://noembed.com/embed?url={self.upstream_safe}'
 | 
			
		||||
            async with self.proxy.session() as session:
 | 
			
		||||
                resp_embed_future = session.get(embed_url)
 | 
			
		||||
                resp_upstream_future = session.get(self.upstream)
 | 
			
		||||
                resp_embed = await resp_embed_future
 | 
			
		||||
                resp_upstream = await resp_upstream_future
 | 
			
		||||
                text_embed_future = resp_embed.text()
 | 
			
		||||
                text_upstream_future = resp_upstream.text()
 | 
			
		||||
                text_embed = await text_embed_future
 | 
			
		||||
                text_upstream = await text_upstream_future
 | 
			
		||||
                parser = MetaParser()
 | 
			
		||||
                parser.feed(text_upstream)
 | 
			
		||||
                data_raw = json.loads(text_embed)
 | 
			
		||||
                if isinstance(data_raw, dict):
 | 
			
		||||
                    data_new = {}
 | 
			
		||||
                    data_valid = True
 | 
			
		||||
                    data_new["og:title"] = data_raw.get("title")
 | 
			
		||||
                    data_new["og:description"] = data_raw.get("author_name")
 | 
			
		||||
                    data_new["og:image"] = data_raw.get("thumbnail_url")
 | 
			
		||||
                    data_filtered = {}
 | 
			
		||||
                    for key in data_new:
 | 
			
		||||
                        value = data_new.get(key)
 | 
			
		||||
                        if isinstance(value, str):
 | 
			
		||||
                            data_filtered[key] = value
 | 
			
		||||
                    data_filtered.update(parser.meta_data)
 | 
			
		||||
                    data = data_filtered
 | 
			
		||||
                    image = data.get("og:image")
 | 
			
		||||
                    if isinstance(image, str):
 | 
			
		||||
                        if self.provider == "youtube":
 | 
			
		||||
                            full_image = re.sub(r'\/[a-zA-Z0-9]+\.([a-zA-Z0-9]+)$', r'/maxresdefault.\1', image)
 | 
			
		||||
                            standard_image = re.sub(r'\/[a-zA-Z0-9]+\.([a-zA-Z0-9]+)$', r'/sddefault.\1', image)
 | 
			
		||||
                            image_status_full_future = session.head(full_image)
 | 
			
		||||
                            image_status_standard_future = session.head(standard_image)
 | 
			
		||||
                            image_status_full = await image_status_full_future
 | 
			
		||||
                            image_status_standard = await image_status_standard_future
 | 
			
		||||
                            if hasattr(image_status_full, "status") and (image_status_full.status < 400):
 | 
			
		||||
                                data["og:image"] = full_image
 | 
			
		||||
                            elif hasattr(image_status_standard, "status") and (image_status_standard.status < 400):
 | 
			
		||||
                                data["og:image"] = standard_image
 | 
			
		||||
                try:
 | 
			
		||||
                    resp_embed_future = session.get(embed_url)
 | 
			
		||||
                    resp_upstream_future = session.get(self.upstream)
 | 
			
		||||
                    resp_embed = await resp_embed_future
 | 
			
		||||
                    resp_upstream = await resp_upstream_future
 | 
			
		||||
                    text_embed_future = resp_embed.text()
 | 
			
		||||
                    text_upstream_future = resp_upstream.text()
 | 
			
		||||
                    text_embed = await text_embed_future
 | 
			
		||||
                    text_upstream = await text_upstream_future
 | 
			
		||||
                    parser = MetaParser()
 | 
			
		||||
                    parser.feed(text_upstream)
 | 
			
		||||
                    data_raw = json.loads(text_embed)
 | 
			
		||||
                    if isinstance(data_raw, dict):
 | 
			
		||||
                        data_new = {}
 | 
			
		||||
                        data_valid = True
 | 
			
		||||
                        data_new["og:title"] = data_raw.get("title")
 | 
			
		||||
                        data_new["og:description"] = data_raw.get("author_name")
 | 
			
		||||
                        data_new["og:image"] = data_raw.get("thumbnail_url")
 | 
			
		||||
                        data_filtered = {}
 | 
			
		||||
                        for key in data_new:
 | 
			
		||||
                            value = data_new.get(key)
 | 
			
		||||
                            if isinstance(value, str):
 | 
			
		||||
                                data_filtered[key] = value
 | 
			
		||||
                        data_filtered.update(parser.meta_data)
 | 
			
		||||
                        data = data_filtered
 | 
			
		||||
                        image = data.get("og:image")
 | 
			
		||||
                        if isinstance(image, str):
 | 
			
		||||
                            if self.provider == "youtube":
 | 
			
		||||
                                full_image = re.sub(r'\/[a-zA-Z0-9]+\.([a-zA-Z0-9]+)$', r'/maxresdefault.\1', image)
 | 
			
		||||
                                standard_image = re.sub(r'\/[a-zA-Z0-9]+\.([a-zA-Z0-9]+)$', r'/sddefault.\1', image)
 | 
			
		||||
                                image_status_full_future = session.head(full_image)
 | 
			
		||||
                                image_status_standard_future = session.head(standard_image)
 | 
			
		||||
                                image_status_full = await image_status_full_future
 | 
			
		||||
                                image_status_standard = await image_status_standard_future
 | 
			
		||||
                                if hasattr(image_status_full, "status") and (image_status_full.status < 400):
 | 
			
		||||
                                    data["og:image"] = full_image
 | 
			
		||||
                                elif hasattr(image_status_standard, "status") and (image_status_standard.status < 400):
 | 
			
		||||
                                    data["og:image"] = standard_image
 | 
			
		||||
                except Exception as e:
 | 
			
		||||
                    logger.info(e)
 | 
			
		||||
        except Exception as e:
 | 
			
		||||
            logger.info(e)
 | 
			
		||||
        return data
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user