adding webmention update/delete support and re-adding enclosure to feed
This commit is contained in:
parent
9e9d989aca
commit
fe4ce23776
1 changed files with 76 additions and 34 deletions
110
nasg.py
110
nasg.py
|
@ -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,23 +1034,31 @@ class WebImage(object):
|
||||||
if self.mtime > mtime:
|
if self.mtime > mtime:
|
||||||
exists = False
|
exists = False
|
||||||
|
|
||||||
|
smeta = {
|
||||||
|
'fpath': fpath,
|
||||||
|
'exists': False,
|
||||||
|
'url': "%s/%s/%s" % (
|
||||||
|
shared.config.get('site', 'url'),
|
||||||
|
shared.config.get('common', 'files'),
|
||||||
|
name
|
||||||
|
),
|
||||||
|
'crop': shared.config.getboolean(
|
||||||
|
'crop',
|
||||||
|
size,
|
||||||
|
fallback=False
|
||||||
|
),
|
||||||
|
'fsize': int(self.meta.get('FileSize'))
|
||||||
|
}
|
||||||
|
|
||||||
|
if os.path.isfile(fpath):
|
||||||
|
smeta.update({
|
||||||
|
'exists': True,
|
||||||
|
'fsize': os.path.getsize(fpath)
|
||||||
|
})
|
||||||
|
|
||||||
sizes.append((
|
sizes.append((
|
||||||
int(size),
|
int(size),
|
||||||
{
|
smeta
|
||||||
'fpath': fpath,
|
|
||||||
'exists': os.path.isfile(fpath),
|
|
||||||
'url': "%s/%s/%s" % (
|
|
||||||
shared.config.get('site', 'url'),
|
|
||||||
shared.config.get('common', 'files'),
|
|
||||||
name
|
|
||||||
),
|
|
||||||
'crop': shared.config.getboolean(
|
|
||||||
'crop',
|
|
||||||
size,
|
|
||||||
fallback=False
|
|
||||||
),
|
|
||||||
'fsize': os.path.getsize(fpath)
|
|
||||||
}
|
|
||||||
))
|
))
|
||||||
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'
|
||||||
|
|
Loading…
Reference in a new issue