diff options
Diffstat (limited to 'handlers.py')
| -rw-r--r-- | handlers.py | 71 |
1 files changed, 32 insertions, 39 deletions
diff --git a/handlers.py b/handlers.py index bfca09b..433b76a 100644 --- a/handlers.py +++ b/handlers.py @@ -24,10 +24,6 @@ class Site(ABC): def process(self): return -class Parry(Site): - def process(self): - yield {} - class Website(Site): tag = "" prefix = "" @@ -46,49 +42,44 @@ class Website(Site): id = ObjectId.from_datetime(datetime.strptime(m["updatedAt"], self.date_format)) try: entry = session.query(Upload).filter_by(id=id).one() - try: - entry.author = session.query(User).filter_by(name=m["author"]).one() - except db.orm.exc.NoResultFound: - pass - + exists = True continue except db.orm.exc.NoResultFound: - pass + entry = Upload(id=id) + exists = False - entry = Upload( - id=id, - title=html.unescape(m["title"]), - tags=[self.tag], - slug="".join(i for i in html.unescape(m["title"]).lower() if i in string.ascii_letters), - updated_at=datetime.strptime(m["updatedAt"], self.date_format), - _v=1 - ) + entry.title = html.unescape(m["title"]) + entry.tags = [self.tag] + entry.slug = "".join(i for i in html.unescape(m["title"]).lower() if i in string.ascii_letters) + entry.updated_at = datetime.strptime(m["updatedAt"], self.date_format) + entry._v = 1 try: entry.author = session.query(User).filter_by(name=m["author"]).one() except db.orm.exc.NoResultFound: pass - downloadURL = self.prefix + m["downloadURL"] - try: - r = requests.get(downloadURL, stream=True, allow_redirects=True) - except requests.exceptions.ConnectionError: - continue - - if not r: - continue - - locale.setlocale(locale.LC_ALL, "C") - session.add(File( - hash=calculateHashForResource(r).hexdigest(), - id=entry.id, - name=downloadURL.split("/")[-1], - content_type=r.headers.get("Content-Type", "application/octet-stream"), - length=int(r.headers["Content-Length"]), - date=datetime.strptime(r.headers["Date"], "%a, %d %b %Y %H:%M:%S GMT"), - download_url=downloadURL, - upload=entry - )) + if not exists: + downloadURL = self.prefix + m["downloadURL"] + try: + r = requests.get(downloadURL, stream=True, allow_redirects=True) + except requests.exceptions.ConnectionError: + continue + + if not r: + continue + + locale.setlocale(locale.LC_ALL, "C") + session.add(File( + hash=calculateHashForResource(r).hexdigest(), + id=entry.id, + name=downloadURL.split("/")[-1], + content_type=r.headers.get("Content-Type", "application/octet-stream"), + length=int(r.headers["Content-Length"]), + date=datetime.strptime(r.headers["Date"], "%a, %d %b %Y %H:%M:%S GMT"), + download_url=downloadURL, + upload=entry + )) locale.setlocale(locale.LC_ALL, "") @@ -97,7 +88,9 @@ class Website(Site): if d and "description" in d.groups(): entry.description = html.unescape(d["description"]) - session.add(entry) + + if not exists: + session.add(entry) session.commit() class CCAN(Website): |
