Simplified embedding, added support for height/width and better site name selection
# Conflicts: # src/snek/system/template.py
This commit is contained in:
		
							parent
							
								
									27de7a8b4e
								
							
						
					
					
						commit
						cc9fc0eeaf
					
				| @ -333,142 +333,185 @@ def get_url_content(url): | |||||||
|         return None |         return None | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|  | def get_element_options(head_info, elem=None, meta=None, ograph=None, twitter=None): | ||||||
|  |     if twitter: | ||||||
|  |         tw_tag = head_info.find( | ||||||
|  |             "meta", attrs={"name": "twitter:" + twitter} | ||||||
|  |         ) or head_info.find("meta", attrs={"property": "twitter:" + twitter}) | ||||||
|  |         if tw_tag: | ||||||
|  |             return tw_tag.get("content", tw_tag.get("value", None)) | ||||||
|  | 
 | ||||||
|  |     if ograph: | ||||||
|  |         og_tag = head_info.find( | ||||||
|  |             "meta", attrs={"property": "og:" + ograph} | ||||||
|  |         ) or head_info.find("meta", attrs={"name": "og:" + ograph}) | ||||||
|  |         if og_tag: | ||||||
|  |             return og_tag.get("content", og_tag.get("value", None)) | ||||||
|  | 
 | ||||||
|  |     if meta: | ||||||
|  |         meta_tag = head_info.find("meta", attrs={"name": meta}) or head_info.find( | ||||||
|  |             "meta", attrs={"property": meta} | ||||||
|  |         ) | ||||||
|  |         if meta_tag: | ||||||
|  |             return meta_tag.get("content", meta_tag.get("value", None)) | ||||||
|  | 
 | ||||||
|  |     if elem: | ||||||
|  |         elem_tag = head_info.find(elem) | ||||||
|  |         if elem_tag: | ||||||
|  |             return elem_tag.text | ||||||
|  | 
 | ||||||
|  |     return None | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
| def embed_url(text): | def embed_url(text): | ||||||
|     soup = BeautifulSoup(text, "html.parser") |     soup = BeautifulSoup(text, "html.parser") | ||||||
| 
 | 
 | ||||||
|     attachments = {} |     attachments = {} | ||||||
| 
 | 
 | ||||||
|     for element in soup.find_all("a"): |     for element in soup.find_all("a"): | ||||||
|         if "href" in element.attrs and element.attrs["href"].startswith("http") and ("data-noembed" not in element.attrs): |         if ( | ||||||
|  |             "href" in element.attrs | ||||||
|  |             and element.attrs["href"].startswith("http") | ||||||
|  |             and element.attrs["href"] not in attachments | ||||||
|  |             and ("data-noembed" not in element.attrs) | ||||||
|  |         ): | ||||||
|  |             original_link_name = element.attrs["href"] | ||||||
|  | 
 | ||||||
|             page_url = urlparse(element.attrs["href"]) |             page_url = urlparse(element.attrs["href"]) | ||||||
|             page = get_url_content(element.attrs["href"]) |             page = get_url_content(element.attrs["href"]) | ||||||
|             if page: |             if not page: | ||||||
|                 parsed_page = BeautifulSoup(page, "html.parser") |                 continue | ||||||
|                 head_info = parsed_page.find("head") |  | ||||||
|                 if head_info: |  | ||||||
| 
 | 
 | ||||||
|                     def get_element_options( |             parsed_page = BeautifulSoup(page, "html.parser") | ||||||
|                         elem=None, meta=None, ograph=None, twitter=None |             head_info = parsed_page.find("head") | ||||||
|                     ): |  | ||||||
|                         if twitter: |  | ||||||
|                             tw_tag = head_info.find( |  | ||||||
|                                 "meta", attrs={"name": "twitter:" + twitter} |  | ||||||
|                             ) or head_info.find( |  | ||||||
|                                 "meta", attrs={"property": "twitter:" + twitter} |  | ||||||
|                             ) |  | ||||||
|                             if tw_tag: |  | ||||||
|                                 return tw_tag.get("content", tw_tag.get("value", None)) |  | ||||||
| 
 | 
 | ||||||
|                         if ograph: |             if not head_info: | ||||||
|                             og_tag = head_info.find( |                 continue | ||||||
|                                 "meta", attrs={"property": "og:" + ograph} |  | ||||||
|                             ) or head_info.find("meta", attrs={"name": "og:" + ograph}) |  | ||||||
|                             if og_tag: |  | ||||||
|                                 return og_tag.get("content", og_tag.get("value", None)) |  | ||||||
| 
 | 
 | ||||||
|                         if meta: |             page_name = ( | ||||||
|                             meta_tag = head_info.find( |                 get_element_options(head_info, "title", "title", "title", "title") | ||||||
|                                 "meta", attrs={"name": meta} |                 or page_url.netloc | ||||||
|                             ) or head_info.find("meta", attrs={"property": meta}) |             ) | ||||||
|                             if meta_tag: |             page_site = ( | ||||||
|                                 return meta_tag.get( |                 get_element_options(head_info, None, "site", "site", "site") | ||||||
|                                     "content", meta_tag.get("value", None) |                 or get_element_options(head_info, ograph="site_name") | ||||||
|                                 ) |                 or page_url.netloc | ||||||
|  |             ) | ||||||
|  |             page_description = get_element_options( | ||||||
|  |                 head_info, None, "description", "description", "description" | ||||||
|  |             ) | ||||||
| 
 | 
 | ||||||
|                         if elem: |             page_image = ( | ||||||
|                             elem_tag = head_info.find(elem) |                 get_element_options(head_info, None, "image", "image", "image") | ||||||
|                             if elem_tag: |                 or get_element_options( | ||||||
|                                 return elem_tag.text |                     head_info, None, "image:url", "image:url", "image:url" | ||||||
|  |                 ) | ||||||
|  |                 or get_element_options( | ||||||
|  |                     head_info, | ||||||
|  |                     None, | ||||||
|  |                     "image:secure_url", | ||||||
|  |                     "image:secure_url", | ||||||
|  |                     "image:secure_url", | ||||||
|  |                 ) | ||||||
|  |             ) | ||||||
|  |             page_image_height = get_element_options( | ||||||
|  |                 head_info, None, "image:height", "image:height", "image:height" | ||||||
|  |             ) | ||||||
|  |             page_image_width = get_element_options( | ||||||
|  |                 head_info, None, "image:width", "image:width", "image:width" | ||||||
|  |             ) | ||||||
|  |             page_image_alt = get_element_options( | ||||||
|  |                 head_info, None, "image:alt", "image:alt", "image:alt" | ||||||
|  |             ) | ||||||
| 
 | 
 | ||||||
|                         return None |             page_video = ( | ||||||
|  |                 get_element_options(head_info, None, "video", "video", "video") | ||||||
|  |                 or get_element_options( | ||||||
|  |                     head_info, None, "video:url", "video:url", "video:url" | ||||||
|  |                 ) | ||||||
|  |                 or get_element_options( | ||||||
|  |                     head_info, | ||||||
|  |                     None, | ||||||
|  |                     "video:secure_url", | ||||||
|  |                     "video:secure_url", | ||||||
|  |                     "video:secure_url", | ||||||
|  |                 ) | ||||||
|  |             ) | ||||||
|  |             page_video_height = get_element_options( | ||||||
|  |                 head_info, None, "video:height", "video:height", "video:height" | ||||||
|  |             ) | ||||||
|  |             page_video_width = get_element_options( | ||||||
|  |                 head_info, None, "video:width", "video:width", "video:width" | ||||||
|  |             ) | ||||||
| 
 | 
 | ||||||
|                     original_link_name = element.attrs["href"] |             page_audio = ( | ||||||
|  |                 get_element_options(head_info, None, "audio", "audio", "audio") | ||||||
|  |                 or get_element_options( | ||||||
|  |                     head_info, None, "audio:url", "audio:url", "audio:url" | ||||||
|  |                 ) | ||||||
|  |                 or get_element_options( | ||||||
|  |                     head_info, | ||||||
|  |                     None, | ||||||
|  |                     "audio:secure_url", | ||||||
|  |                     "audio:secure_url", | ||||||
|  |                     "audio:secure_url", | ||||||
|  |                 ) | ||||||
|  |             ) | ||||||
| 
 | 
 | ||||||
|                     if original_link_name in attachments: |             (get_element_options(head_info, twitter="card") or "summary_large_image") | ||||||
|                         continue |  | ||||||
| 
 | 
 | ||||||
|                     page_name = ( |             attachment_base = BeautifulSoup(str(element), "html.parser") | ||||||
|                         get_element_options("title", "title", "title", "title") |             attachments[original_link_name] = attachment_base | ||||||
|                         or page_url.netloc |  | ||||||
|                     ) |  | ||||||
|                     page_site = ( |  | ||||||
|                         get_element_options(None, "site", "site", "site") |  | ||||||
|                         or page_url.netloc |  | ||||||
|                     ) |  | ||||||
|                     page_description = get_element_options( |  | ||||||
|                         None, "description", "description", "description" |  | ||||||
|                     ) |  | ||||||
|                     page_image = get_element_options(None, "image", "image", "image") |  | ||||||
|                     page_image_alt = get_element_options( |  | ||||||
|                         None, "image:alt", "image:alt", "image:alt" |  | ||||||
|                     ) |  | ||||||
|                     page_video = get_element_options(None, "video", "video", "video") |  | ||||||
|                     page_audio = get_element_options(None, "audio", "audio", "audio") |  | ||||||
| 
 | 
 | ||||||
|                     ( |             attachment = next(attachment_base.children) | ||||||
|                         get_element_options(None, None, None, "card") |  | ||||||
|                         or "summary_large_image" |  | ||||||
|                     ) |  | ||||||
| 
 | 
 | ||||||
|                     attachment_base = BeautifulSoup(str(element), "html.parser") |             attachment.clear() | ||||||
|                     attachments[original_link_name] = attachment_base |             attachment.attrs["class"] = "embed-url-link" | ||||||
| 
 | 
 | ||||||
|                     attachment = next(attachment_base.children) |             render_element = attachment | ||||||
| 
 | 
 | ||||||
|                     attachment.clear() |             if page_image: | ||||||
|                     attachment.attrs["class"] = "embed-url-link" |                 style = { | ||||||
|  |                     "width": page_image_width + "px" if page_image_width else None, | ||||||
|  |                     "height": page_image_height + "px" if page_image_height else None, | ||||||
|  |                 } | ||||||
| 
 | 
 | ||||||
|                     render_element = attachment |                 style_string = "; ".join( | ||||||
|  |                     f"{key}: {value}" for key, value in style.items() if value | ||||||
|  |                 ) | ||||||
| 
 | 
 | ||||||
|                     if page_image: |                 image_template = f'<span><img src="{page_image}" alt="{page_image_alt or page_name}" title="{page_name}" width="1" height="1" style="{style_string}" /></span>' | ||||||
|                         image_template = f'<span><img src="{page_image}" alt="{page_image_alt or page_name}" title="{page_name}" width="420" height="240" /></span>' |                 render_element.append(BeautifulSoup(image_template, "html.parser")) | ||||||
|                         render_element.append( |  | ||||||
|                             BeautifulSoup(image_template, "html.parser") |  | ||||||
|                         ) |  | ||||||
|                     if page_video: |  | ||||||
|                         video_template = f'<video controls><source src="{page_video}">Your browser does not support the video tag.</video>' |  | ||||||
|                         render_element.append( |  | ||||||
|                             BeautifulSoup(video_template, "html.parser") |  | ||||||
|                         ) |  | ||||||
|                     if page_audio: |  | ||||||
|                         audio_template = f'<audio controls><source src="{page_audio}">Your browser does not support the audio tag.</audio>' |  | ||||||
|                         render_element.append( |  | ||||||
|                             BeautifulSoup(audio_template, "html.parser") |  | ||||||
|                         ) |  | ||||||
| 
 | 
 | ||||||
|                     description_element_base = BeautifulSoup( |             if page_video: | ||||||
|                         "<span class='description'></span>", "html.parser" |                 style = { | ||||||
|                     ) |                     "width": page_video_width + "px" if page_video_width else None, | ||||||
|                     description_element = next(description_element_base.children) |                     "height": page_video_height + "px" if page_video_height else None, | ||||||
|                     description_element.append( |                 } | ||||||
|                         BeautifulSoup( |  | ||||||
|                             f'<p class="page-site">{page_site}</p>', |  | ||||||
|                             "html.parser", |  | ||||||
|                         ) |  | ||||||
|                     ) |  | ||||||
| 
 | 
 | ||||||
|                     description_element.append( |                 style_string = "; ".join( | ||||||
|                         BeautifulSoup( |                     f"{key}: {value}" for key, value in style.items() if value | ||||||
|                             f'<strong class="page-name">{page_name}</strong>', |                 ) | ||||||
|                             "html.parser", |                 video_template = f'<video controls style="{style_string}"><source src="{page_video}">Your browser does not support the video tag.</video>' | ||||||
|                         ) |                 render_element.append(BeautifulSoup(video_template, "html.parser")) | ||||||
|                     ) |  | ||||||
| 
 | 
 | ||||||
|                     description_element.append( |             if page_audio: | ||||||
|                         BeautifulSoup( |                 audio_template = f'<audio controls><source src="{page_audio}">Your browser does not support the audio tag.</audio>' | ||||||
|                             f"<p class='page-description'>{page_description or 'No description available.'}</p>", |                 render_element.append(BeautifulSoup(audio_template, "html.parser")) | ||||||
|                             "html.parser", |  | ||||||
|                         ) |  | ||||||
|                     ) |  | ||||||
| 
 | 
 | ||||||
|                     description_element.append( |             description_element = BeautifulSoup( | ||||||
|                         BeautifulSoup( |                 f""" | ||||||
|                             f"<p class='page-original-link'>{original_link_name}</p>", |             <span class='description'> | ||||||
|                             "html.parser", |                 <p class="page-site">{page_site}</p> | ||||||
|                         ) |                 <strong class="page-name">{page_name}</strong> | ||||||
|                     ) |                 <p class='page-description'>{page_description or "No description available."}</p> | ||||||
|  |                 <p class='page-original-link'>{original_link_name}</p> | ||||||
|  |             </span> | ||||||
|  |             """, | ||||||
|  |                 "html.parser", | ||||||
|  |             ) | ||||||
| 
 | 
 | ||||||
|                     render_element.append(description_element_base) |             render_element.append(description_element) | ||||||
| 
 | 
 | ||||||
|     for attachment in attachments.values(): |     for attachment in attachments.values(): | ||||||
|         soup.append(attachment) |         soup.append(attachment) | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user
	 BordedDev
						BordedDev