adding webmention update/delete support and re-adding enclosure to feed

This commit is contained in:
Peter Molnar 2018-06-01 09:49:14 +00:00
parent 9e9d989aca
commit fe4ce23776

88
nasg.py
View file

@ -388,6 +388,13 @@ class Category(NoDupeContainer):
shared.site.get('author').get('name'), shared.site.get('author').get('name'),
p.get('licence').get('text') p.get('licence').get('text')
)) ))
if p.get('enclosure'):
enclosure = p.get('enclosure')
fe.enclosure(
enclosure.get('url'),
"%d" % enclosure.get('size'),
enclosure.get('mime')
)
with open(o, 'wb') as f: with open(o, 'wb') as f:
f.write(fg.atom_str(pretty=True)) f.write(fg.atom_str(pretty=True))
@ -882,9 +889,12 @@ class WebImage(object):
@property @property
def mime_size(self): def mime_size(self):
if not self.is_downsizeable: if self.is_downsizeable:
return int(os.path.getsize(self.fpath)) try:
return int(self.sizes[-1][1]['fsize']) return int(self.sizes[-1][1]['fsize'])
except Exception as e:
pass
return int(self.meta.get('FileSize'))
@property @property
def href(self): def href(self):
@ -1024,11 +1034,9 @@ class WebImage(object):
if self.mtime > mtime: if self.mtime > mtime:
exists = False exists = False
sizes.append(( smeta = {
int(size),
{
'fpath': fpath, 'fpath': fpath,
'exists': os.path.isfile(fpath), 'exists': False,
'url': "%s/%s/%s" % ( 'url': "%s/%s/%s" % (
shared.config.get('site', 'url'), shared.config.get('site', 'url'),
shared.config.get('common', 'files'), shared.config.get('common', 'files'),
@ -1039,8 +1047,18 @@ class WebImage(object):
size, size,
fallback=False fallback=False
), ),
'fsize': os.path.getsize(fpath) 'fsize': int(self.meta.get('FileSize'))
} }
if os.path.isfile(fpath):
smeta.update({
'exists': True,
'fsize': os.path.getsize(fpath)
})
sizes.append((
int(size),
smeta
)) ))
return sorted(sizes, reverse=False) return sorted(sizes, reverse=False)
@ -1287,9 +1305,8 @@ class Comment(object):
if 'name' in author: if 'name' in author:
r.update({ 'name': self.meta.get('author').get('name')}) r.update({ 'name': self.meta.get('author').get('name')})
elif 'url' in author:
if 'url' in author: r.update({ 'name': urlparse(self.meta.get('author').get('url')).hostname})
r.update({ 'name': self.meta.get('author').get('url')})
return r return r
@ -1343,18 +1360,7 @@ class Webmention(object):
self.source, self.source,
self.target self.target
) )
self._source = None
def _fetch(self):
self._source = shared.XRay(self.source).parse()
def _save(self):
fm = frontmatter.loads('')
fm.content = self.content
fm.metadata = self.meta
with open(self.fpath, 'wt') as f:
logging.info("Saving webmention to %s", self.fpath)
f.write(frontmatter.dumps(fm))
return
def send(self): def send(self):
rels = shared.XRay(self.target).set_discover().parse() rels = shared.XRay(self.target).set_discover().parse()
@ -1384,17 +1390,53 @@ class Webmention(object):
} }
) )
if p.status_code == requests.codes.ok: if p.status_code == requests.codes.ok:
logging.info("webmention sent")
return True return True
elif p.status_code == 400 and 'brid.gy' in self.target:
logging.warning("potential bridgy duplicate: %s %s", p.status_code, p.text)
return True
else:
logging.error("webmention failure: %s %s", p.status_code, p.text)
return False
except Exception as e: except Exception as e:
logging.error("sending webmention failed: %s", e) logging.error("sending webmention failed: %s", e)
return False return False
def receive(self): def receive(self):
self._fetch() head = requests.head(self.source)
if head.status_code == 410:
self._delete()
return
elif head.status_code != requests.codes.ok:
logging.error(
"webmention source failure: %s %s",
head.status_code,
self.source
)
return
self._source = shared.XRay(self.source).parse()
if 'data' not in self._source: if 'data' not in self._source:
logging.error("no data found in webmention source: %s", self.source)
return return
self._save() self._save()
def _delete(self):
if os.path.isfile(self.fpath):
logging.info("Deleting webmention %s", self.fpath)
os.unlink(self.fpath)
return
def _save(self):
fm = frontmatter.loads('')
fm.content = self.content
fm.metadata = self.meta
with open(self.fpath, 'wt') as f:
logging.info("Saving webmention to %s", self.fpath)
f.write(frontmatter.dumps(fm))
return
@property @property
def relation(self): def relation(self):
r = 'webmention' r = 'webmention'