fetch and display u-syndication in case of webmention.io and brid.gy publish can be asked to provide the endpoint

This commit is contained in:
Peter Molnar 2019-05-05 13:04:36 +01:00
parent 72582d0a60
commit 32bf4994e7
2 changed files with 92 additions and 20 deletions

88
nasg.py
View file

@ -229,6 +229,58 @@ class Webmention(object):
) )
) )
@property
def syndication_fpath(self):
return self.fpath.replace('.ping', '.copy')
def check_syndication(self):
""" this is very specific to webmention.io and brid.gy publish """
if os.path.isfile(self.syndication_fpath):
logger.debug("syndication copy exist for %s", self.dpath)
return
if "fed.brid.gy" in self.target:
return
if "brid.gy" not in self.target:
return
if not self.exists:
return
with open(self.fpath) as f:
txt = f.read()
if "telegraph.p3k.io" not in txt:
return
try:
maybe = json.loads(txt)
if "location" not in maybe:
return
if "http_body" not in maybe:
wio = requests.get(maybe["location"])
if wio.status_code != requests.codes.ok:
return
maybe = wio.json()
with open(self.fpath, "wt") as update:
update.write(json.dumps(maybe, sort_keys=True, indent=4))
if "url" in maybe["http_body"]:
data = json.loads(maybe["http_body"])
url = data["url"]
sp = os.path.join(
self.dpath, "%s.copy" % url2slug(url, 200)
)
with open(sp, "wt") as f:
logger.info(
"writing syndication copy URL %s to %s",
url,
sp
)
f.write(url)
except Exception as e:
logger.error(
"failed to fetch syndication URL for %s: %s",
self.dpath,
e
)
pass
@property @property
def exists(self): def exists(self):
if not os.path.isfile(self.fpath): if not os.path.isfile(self.fpath):
@ -243,6 +295,7 @@ class Webmention(object):
async def send(self): async def send(self):
if self.exists: if self.exists:
self.check_syndication()
return return
elif settings.args.get('noping'): elif settings.args.get('noping'):
self.save("noping entry at %s" % arrow.now() ) self.save("noping entry at %s" % arrow.now() )
@ -265,7 +318,6 @@ class Webmention(object):
else: else:
self.save(r.text) self.save(r.text)
class MarkdownDoc(object): class MarkdownDoc(object):
""" Base class for anything that is stored as .md """ """ Base class for anything that is stored as .md """
@property @property
@ -464,7 +516,7 @@ class Singular(MarkdownDoc):
@property @property
def sameas(self): def sameas(self):
r = [] r = {}
for k in glob.glob( for k in glob.glob(
os.path.join( os.path.join(
os.path.dirname(self.fpath), os.path.dirname(self.fpath),
@ -472,8 +524,8 @@ class Singular(MarkdownDoc):
) )
): ):
with open(k, 'rt') as f: with open(k, 'rt') as f:
r.append(f.read()) r.update({f.read(): True})
return r return list(r.keys())
@cached_property @cached_property
def comments(self): def comments(self):
@ -1108,6 +1160,20 @@ class WebImage(object):
}) })
if self.is_mainimg: if self.is_mainimg:
r.update({"representativeOfPage": True}) r.update({"representativeOfPage": True})
if self.exif['GPSLatitude'] != 0 and self.exif['GPSLongitude'] != 0:
r.update({
"locationCreated": struct({
"@context": "http://schema.org",
"@type": "Place",
"geo": struct({
"@context": "http://schema.org",
"@type": "GeoCoordinates",
"latitude": self.exif['GPSLatitude'],
"longitude": self.exif['GPSLongitude']
})
})
})
return struct(r) return struct(r)
def __str__(self): def __str__(self):
@ -1215,7 +1281,9 @@ class WebImage(object):
'FocalLength': '', 'FocalLength': '',
'ISO': '', 'ISO': '',
'LensID': '', 'LensID': '',
'CreateDate': str(arrow.get(self.mtime)) 'CreateDate': str(arrow.get(self.mtime)),
'GPSLatitude': 0,
'GPSLongitude': 0
} }
if not self.is_photo: if not self.is_photo:
return exif return exif
@ -1227,7 +1295,9 @@ class WebImage(object):
'FocalLength': ['FocalLength'], # ['FocalLengthIn35mmFormat'], 'FocalLength': ['FocalLength'], # ['FocalLengthIn35mmFormat'],
'ISO': ['ISO'], 'ISO': ['ISO'],
'LensID': ['LensID', 'LensSpec', 'Lens'], 'LensID': ['LensID', 'LensSpec', 'Lens'],
'CreateDate': ['CreateDate', 'DateTimeOriginal'] 'CreateDate': ['CreateDate', 'DateTimeOriginal'],
'GPSLatitude': ['GPSLatitude'],
'GPSLongitude': ['GPSLongitude']
} }
for ekey, candidates in mapping.items(): for ekey, candidates in mapping.items():
@ -1399,9 +1469,9 @@ class WebImage(object):
thumb.compression_quality = 88 thumb.compression_quality = 88
thumb.unsharp_mask( thumb.unsharp_mask(
radius=1, radius=1,
sigma=1, sigma=0.5,
amount=0.5, amount=0.7,
threshold=0.1 threshold=0.5
) )
thumb.format = 'pjpeg' thumb.format = 'pjpeg'

View file

@ -96,21 +96,23 @@
#<span class="p-category">{{ keyword }}</span>{% if not loop.last %} {% endif %} #<span class="p-category">{{ keyword }}</span>{% if not loop.last %} {% endif %}
{% endfor %} {% endfor %}
{% endif %} {% endif %}
<!-- {% if post['@type'] == 'Photograph' %}
{% if post.sameAs|length %} {% if post.image[0].locationCreated %}
<br />
Location:
<a class="h-geo" href="https://www.openstreetmap.org/#map=14/{{ post.image[0].locationCreated.geo.longitude }}/{{ post.image[0].locationCreated.geo.latitude }}">
<span class="p-longitude">{{ post.image[0].locationCreated.geo.longitude }}</span>,
<span class="p-latitude">{{ post.image[0].locationCreated.geo.latitude }}</span>
</a>
{% endif %}
{% endif %}
{% if post.sameAs|length %}
<br /> <br />
Syndicated to: Syndicated to:
<ul>
{% for url in post.sameAs %} {% for url in post.sameAs %}
<li> <a class="u-syndication" href="{{ url }}">{{ url }}</a>{% if not loop.last and post.sameAs|length >1 %}, {% endif %}
<a class="u-syndication" href="{{ url }}">
{{ url }}
</a>
</li>
{% endfor %} {% endfor %}
</ul> {% endif %}
{% endif %}
-->
</p> </p>
{% if post.subjectOf %} {% if post.subjectOf %}