def load(self, short_code): cur = self.db.urls.find({"short_code": short_code}) if cur.count() != 1: raise ShortInvalidException("invalid short code") row = cur.next() short_url = self.prefix + short_code surl = ShortURL(short_code, short_url) surl.deserialize(row) return surl
def new(self, long_url, link_type): """Creates and returns a new ShortURL representing the url. """ short_code = self._hash_url(long_url) short_url = self.prefix + short_code try: if long_url.startswith("static/"): info = get_page_info(self.prefix + long_url) else: info = get_page_info(long_url) except: info = {} surl = ShortURL(short_code, short_url, long_url, info) surl.link_type = link_type mime = info["mimetype"] if not long_url.startswith("static/") and mime and mime.startswith("image/"): print "DOWNLOADING %s" % surl.get_long_url() dl_file = DownloadedFile(long_url) dl_file.download() dl_file.save() # Override some info... print "downloaded to: %s" % dl_file.get_filename() surl.long_url = dl_file.get_filename() surl.link_type = ShortURL.IMG surl.mime_type = dl_file.get_mimetype() # Rehash since we just changed the long_url surl.short_code = self._hash_url(surl.long_url) surl.short_url = self.prefix + surl.short_code print "new short_url %s" % surl.get_short_url() self.save(surl) return surl