Add method to parse meta from the provided URL
This commit is contained in:
parent
01e3516fa8
commit
2c839c4161
3 changed files with 46 additions and 12 deletions
|
|
@ -128,20 +128,21 @@ export default {
|
|||
frappe.db.insert(comment);
|
||||
},
|
||||
generate_preview(link_element) {
|
||||
// TODO: create meta parse and move the code to separate component
|
||||
fetch(`https://opengraph.io/api/1.1/site/${link_element.href}?app_id=5bc7fee2a2677843003b8bba`).then(res => {
|
||||
res.json().then(data => {
|
||||
const og = data['openGraph']
|
||||
// TODO: move the code to separate component
|
||||
frappe.xcall('frappe.social.doctype.post.post.get_link_info', {
|
||||
'url': link_element.href
|
||||
}).then(info => {
|
||||
if (info['og:title'] || info['title']) {
|
||||
link_element.insertAdjacentHTML('afterend', `
|
||||
<div class="preview-card" class="flex">
|
||||
<img class="user-avatar" src="${og.image.url}"/>
|
||||
<a href="${info['og:url']}" target="blank" class="preview-card" class="flex">
|
||||
<img src="${info['og:image']}"/>
|
||||
<div class="flex-column">
|
||||
<h5>${og.title}</h5>
|
||||
<p>${og.description}</p>
|
||||
<h5>${info['og:title'] || info['title']}</h5>
|
||||
<p class="text-muted">${info['og:description'] || info['description']}</p>
|
||||
</div>
|
||||
</div>
|
||||
</a>
|
||||
` );
|
||||
});
|
||||
}
|
||||
})
|
||||
.catch(console.error)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -74,6 +74,7 @@ body[data-route*="social"] {
|
|||
|
||||
.post-card {
|
||||
.generic-card();
|
||||
max-width: 600px;
|
||||
.post-body {
|
||||
padding: 15px;
|
||||
.user-name {
|
||||
|
|
@ -114,16 +115,21 @@ body[data-route*="social"] {
|
|||
border-top: 1px solid @border-color;
|
||||
}
|
||||
.preview-card {
|
||||
text-decoration: none;
|
||||
height: 150px;
|
||||
border: 1px solid @light-border-color;
|
||||
border-radius: 5px;
|
||||
margin-top: 15px;
|
||||
padding-right: 10px;
|
||||
background: white;
|
||||
display: flex;
|
||||
img {
|
||||
height: 100%;
|
||||
border: none;
|
||||
margin-left: 10px;
|
||||
margin: 0 10px;
|
||||
max-width: 150px;
|
||||
height: auto;
|
||||
max-height: 90%;
|
||||
align-self: center;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -34,3 +34,30 @@ def frequently_visited_links():
|
|||
return frappe.get_all('Route History', fields=['route', 'count(name) as count'], filters={
|
||||
'user': frappe.session.user
|
||||
}, group_by="route", order_by="count desc", limit=10)
|
||||
|
||||
@frappe.whitelist()
|
||||
def get_link_info(url):
|
||||
cached_link_info = frappe.cache().hget("link_info", url)
|
||||
if cached_link_info:
|
||||
return cached_link_info
|
||||
|
||||
from bs4 import BeautifulSoup
|
||||
import requests
|
||||
try:
|
||||
page = requests.get(url)
|
||||
except:
|
||||
frappe.cache().hset("link_info", url, {})
|
||||
return {}
|
||||
|
||||
soup = BeautifulSoup(page.text)
|
||||
|
||||
meta_obj = {}
|
||||
for meta in soup.findAll('meta'):
|
||||
meta_name = meta.get('property') or meta.get('name', '').lower()
|
||||
if meta_name:
|
||||
meta_obj[meta_name] = meta.get('content')
|
||||
|
||||
frappe.cache().hset("link_info", url, meta_obj)
|
||||
|
||||
return meta_obj
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue