Ejemplo n.º 1
0
 def get(self, name, **kw):
     helper = RequestHelper(self)
     source = ArtifactSourceAccessor.get_by_name(name, return_none=True)
     if not source:
         helper.error(404)
         return
     helper.write_json(source_hash(source))
Ejemplo n.º 2
0
    def get(self, **kw):
        helper = RequestHelper(self)
        q = self.request.get("q", None)
        output = self.request.get("o", None)
        max_results = int(self.request.get("max", -1))
        
        if not q:
            helper.error(400, "q not provided.")
            return

        q_results = ArtifactContent.all().search(q)

        json_results = None
        if output == "short":
            json_results = {}
            json_results["count"] = q_results.count()
        elif output == "id":
            json_results = {}
            count = q_results.count()
            if max_results > 0 and max_results < q_results.count():
                count = max_results
                
            json_results["count"] = count
            ids = []
            json_results["ids"] = ids
            results = q_results.fetch(1000) if max_results == -1 else q_results.fetch(max_results)
            for c in results:
                ids.append(c.guid)
        else:
            json_results = []
            if q_results.count():
                for content in q_results.fetch(10):
                    info = ArtifactInfo.get_by_guid(content.guid)
                    json_results.append(ArtifactsHelper.artifact_to_hash(info, content))
        helper.write_json(json_results)
Ejemplo n.º 3
0
 def delete(cls, rhandler, guid, **kw):
     helper = RequestHelper(rhandler)
     try:
         ArtifactAccessor.delete(guid)
         helper.set_status(204)
     except NotFoundException, ex:
         helper.error(404)
Ejemplo n.º 4
0
 def get(self, name, **kw):
     helper = RequestHelper(self)
     source = ArtifactSourceAccessor.get_by_name(name, return_none=True)
     if not source:
         helper.error(404)
         return
     helper.write_json(source_hash(source))
Ejemplo n.º 5
0
    def put(self, source_name):
        helper = RequestHelper(self)
        
        source_name = urllib.unquote(source_name)
        success, values = read_json_fields(helper, "url", "active", logger=logging)
        if not success:
            return
        url, active = values
        
        # a Feed must be sole owner of an ArtifactSource;
        # fails if source already exists and is already linked to a feed
        source = ArtifactSourceAccessor.get_by_name(source_name, return_none=True)
        if source:
            source_feed_key = Feed.get_by_source(source, keys_only=True, return_none=True)
            if source_feed_key:
                msg = "source '%s' is referenced by feed %s" % (source_name, source_feed_key.name())
                helper.error(409, msg)
        else:
            source = ArtifactSourceAccessor.create(source_name)
        
        # creates UrlResource if necessary
        resource = UrlResourceAccessor.get_by_url(url, return_none=True)
        if not resource:
            resource = UrlResourceAccessor.create(url)

        # create or update Feed
        feed = Feed.get_by_source_name(source_name, return_none=True)
        if feed:
            feed.artifact_source = source
            feed.url_resource = resource
            feed.put()
        else:
            Feed.create(source_name, artifact_source=source, url=url, url_resource=resource, active=bool(active))
        helper.set_status(204)
Ejemplo n.º 6
0
    def delete(self, **kw):
      helper = RequestHelper(self)
      q = self.request.get("q", None)
      if not q:
        helper.error(400, "q not provided.")
        return

      results = {}
      deleted_guids = []
      results["deleted"] = deleted_guids
      errors = 0
      q_results = ArtifactContent.all().search(q)
      
      infos = []
      batch_size = 50
      q_count = q_results.count()
      batches = (q_count / batch_size) + 1
      count = 0
      for i in range(0, batches):
        for c in q_results.fetch(batch_size, i * batch_size):
          try:
            logging.debug("deleting guid:%s" % c.guid)
            c.delete()
            count += 1
            deleted_guids.append(c.guid)
            if c.info is not None:
              c.info.delete()              
          except Exception, ex:
            logging.error(ex)
            errors += 1
Ejemplo n.º 7
0
 def delete(self, name, **kw):
     helper = RequestHelper(self)
     try:
         ArtifactSourceAccessor.delete_by_name(name)
         helper.set_status(204)
     except NotFoundException, ex:
         helper.error(404)
         return
Ejemplo n.º 8
0
 def get(self, source_name):
     helper = RequestHelper(self)
     source_name = urllib.unquote(source_name)
     f = Feed.get_by_source_name(source_name, return_none=True)
     if not f:
         helper.error(404)
         return
     helper.write_json(build_feed_hash(f))
Ejemplo n.º 9
0
 def delete(self, name, **kw):
     helper = RequestHelper(self)
     try:
         ArtifactSourceAccessor.delete_by_name(name)
         helper.set_status(204)
     except NotFoundException, ex:
         helper.error(404)
         return
Ejemplo n.º 10
0
 def args_call(*args, **kw):
     handler = args[0]
     helper = RequestHelper(handler)
     if not Authenticator.is_authenticated(handler.request.path, handler.request.method, helper):
         helper.header(error_header, error_message)
         helper.error(403)
     else:
         f(*args, **kw)
Ejemplo n.º 11
0
 def args_call(*args, **kw):
     handler = args[0]
     helper = RequestHelper(handler)
     if not users.is_current_user_admin():
         helper.header(error_header, error_msg)
         helper.error(403)
     else:
         f(*args, **kw)
Ejemplo n.º 12
0
 def args_call(*args, **kw):
     handler = args[0]
     helper = RequestHelper(handler)
     if not users.is_current_user_admin():
         helper.header(error_header, error_msg)
         helper.error(403)
     else:
         f(*args, **kw)
Ejemplo n.º 13
0
 def args_call(*args, **kw):
     handler = args[0]
     helper = RequestHelper(handler)
     if not Authenticator.is_authenticated(
             handler.request.path, handler.request.method, helper):
         helper.header(error_header, error_message)
         helper.error(403)
     else:
         f(*args, **kw)
Ejemplo n.º 14
0
 def delete(self, source_name):
     helper = RequestHelper(self)
     source_name = urllib.unquote(source_name)
     feed = Feed.get_by_source_name(source_name, return_none=True)
     if not feed:
         helper.error(404)
         return
     feed.delete()
     helper.set_status(204)
Ejemplo n.º 15
0
 def get(cls, rhandler, guid, **kw):
     helper = RequestHelper(rhandler)
     
     artifact_info = ArtifactInfo.get_by_guid(guid)
     artifact_content = ArtifactContent.get_by_guid(guid)
     if artifact_info and artifact_content:
         artifact_hash = ArtifactsHelper.artifact_to_hash(artifact_info, artifact_content)
         helper.write_json(artifact_hash)
     else:
         helper.error(404)
Ejemplo n.º 16
0
    def ingest(cls, handler, source_name):
        helper = RequestHelper(handler)
        source_name = urllib.unquote(source_name)

        keep = handler.request.get("keep")
        if keep:
            keep = int(keep)
        else:
            keep = 50

        # TODO: get from cache
        f = Feed.get_by_source_name(source_name, return_none=True)
        if not f:
            helper.error(404)
            return

        results = {}
        entries = []
        results['created'] = entries

        # TODO: use etag from previous ingest
        error_call = lambda entry, ex: logging.error(Exceptions.format_last())

        user = users.get_current_user()
        if not user:
            # there is no logged in user for cron requests
            user = User(Services.API_USER)

        try:
            for artifact_guid, entry, created in model.ingest_feed_entries(
                    f, user, error_call=error_call):
                entries.append({
                    "artifact-guid": artifact_guid,
                    "url": entry.link,
                    "title": entry.title,
                    "created": created
                })
        finally:
            # delete oldest feed entries
            # TODO: shouldn't I be deleting ArtifactContent instances also?
            def delete_info(c):
                try:
                    i = c.info
                    if i:
                        i.delete()
                except Exception, e:
                    pass

            deleted_key_names = ArtifactContent.delete_oldest_by_source(
                f.artifact_source, keep, pre_call=delete_info)

            results['deleted'] = deleted_key_names
            Counters.source_counter(f.artifact_source.name).decrement(
                len(deleted_key_names))
Ejemplo n.º 17
0
 def post(self):
     helper = RequestHelper(self)
     
     if not len(self.request.body):
         helper.error(400, "body required")
         return
         
     decoded_body = urllib.unquote(self.request.body)
     t_api = TwitterConnector.new_api()
     status = t_api.PostUpdate(decoded_body)
     helper.write_json(status.asDict())
Ejemplo n.º 18
0
 def args_call(*args, **kw):
     handler = args[0]
     helper = RequestHelper(handler)
     user = users.get_current_user()
     if not user:
         helper.header(error_header, error_msg)
         helper.error(403)
     elif not emails or (not user.email() in emails):
         helper.header(error_header, "unauthorized-user")
         helper.error(403)
     else:
         f(*args, **kw)
Ejemplo n.º 19
0
 def args_call(*args, **kw):
     handler = args[0]
     helper = RequestHelper(handler)
     user = users.get_current_user()
     if not user:
         helper.header(error_header, error_msg)
         helper.error(403)
     elif not emails or (not user.email() in emails):
         helper.header(error_header, "unauthorized-user")
         helper.error(403)
     else:
         f(*args, **kw)
Ejemplo n.º 20
0
    def put(self, key):
        helper = RequestHelper(self)
        if not self.request.body:
            helper.error(400, "body required")
            return

        value = self.request.body
        logging.info("%s=%s" % (key, value))
        try:
            ConfigurationAccessor.update(**{key: value})
        except IllegalArgumentException, e:
            helper.error(400, "invalid key")
            return
Ejemplo n.º 21
0
 def put(self, key):
     helper = RequestHelper(self)
     if not self.request.body:
         helper.error(400, "body required")
         return
         
     value = self.request.body
     logging.info("%s=%s" % (key, value))
     try:
         ConfigurationAccessor.update(**{key: value})
     except IllegalArgumentException, e:
         helper.error(400, "invalid key")
         return
Ejemplo n.º 22
0
 def args_call(*args, **kw):
     handler = args[0]
     helper = RequestHelper(handler)
     auth_header = handler.request.headers.get(Headers.AUTHORIZATION, "")
     code, headers, user = DigestAuth(realm, users).authenticate(handler.request.method, 
         handler.request.path,
         auth_header)
     if code >= 400 and code < 499:
         for k, v in headers:
             helper.header(k, v)
         helper.error(code)
     else:
         kw['username'] = user
         return f(*args, **kw)
Ejemplo n.º 23
0
 def args_call(*args, **kw):
     handler = args[0]
     helper = RequestHelper(handler)
     auth_header = handler.request.headers.get(Headers.AUTHORIZATION,
                                               "")
     code, headers, user = DigestAuth(realm, users).authenticate(
         handler.request.method, handler.request.path, auth_header)
     if code >= 400 and code < 499:
         for k, v in headers:
             helper.header(k, v)
         helper.error(code)
     else:
         kw['username'] = user
         return f(*args, **kw)
Ejemplo n.º 24
0
 def ingest(cls, handler, source_name):
     helper = RequestHelper(handler)
     source_name = urllib.unquote(source_name)
     
     keep = handler.request.get("keep")
     if keep:
         keep = int(keep)
     else:
         keep = 50
     
     # TODO: get from cache
     f = Feed.get_by_source_name(source_name, return_none=True)
     if not f:
         helper.error(404)
         return
 
     results = {}
     entries = []
     results['created'] = entries
 
     # TODO: use etag from previous ingest
     error_call = lambda entry, ex: logging.error(Exceptions.format_last())
 
     user = users.get_current_user()
     if not user:
         # there is no logged in user for cron requests
         user = User(Services.API_USER)
         
     try:
         for artifact_guid, entry, created in model.ingest_feed_entries(f, user, error_call=error_call):
             entries.append({ "artifact-guid": artifact_guid,
                 "url": entry.link,
                 "title": entry.title,
                 "created": created })
     finally:
         # delete oldest feed entries
         # TODO: shouldn't I be deleting ArtifactContent instances also?
         def delete_info(c):
           try:
             i = c.info
             if i:
               i.delete()
           except Exception, e:
             pass
             
         deleted_key_names = ArtifactContent.delete_oldest_by_source(f.artifact_source, keep, pre_call=delete_info)
         
         results['deleted'] = deleted_key_names
         Counters.source_counter(f.artifact_source.name).decrement(len(deleted_key_names))
Ejemplo n.º 25
0
 def put(cls, rhandler, guid, **kw):
     helper = RequestHelper(rhandler)
     
     artifact = ArtifactInfo.get_by_guid(guid)
     if not artifact:
         helper.error(404)
         return
         
     # removes existing properties
     props = ArtifactInfo.properties().keys()
     for prop in props:
         delattr(artifact, prop)
     
     # save artifact
     ArtifactInfo.save(artifact)
Ejemplo n.º 26
0
 def get(self, **kw):
     helper = RequestHelper(self)
     message = self.request.get("q", None)
     if not message:
         helper.error(400, "q must be provided")
         return
     message = urllib.unquote(message)
     try:
         sources, response = Mixer(new_speaker()).mix_response(message)
         logging.debug("sources:%s, response:%s" % (sources, response))
         result = dict(response=response)
         helper.write_json(result)
     except DataException, ex:
         helper.error(503, Exceptions.format(ex))
         logging.error(ex)
Ejemplo n.º 27
0
 def get(self, **kw):
     helper = RequestHelper(self)
     message = self.request.get("q", None)
     if not message:
         helper.error(400, "q must be provided")
         return
     message = urllib.unquote(message)
     try:
         sources, response = Mixer(new_speaker()).mix_response(message)
         logging.debug("sources:%s, response:%s" % (sources, response))
         result = dict(response=response)
         helper.write_json(result)
     except DataException, ex:
         helper.error(503, Exceptions.format(ex))
         logging.error(ex)
Ejemplo n.º 28
0
 def get(self, key):
     helper = RequestHelper(self)
     config = ConfigurationAccessor.get_or_create()
     result = None
     if config:
         if key:
             if entity_has_property(config, key):
                 result = getattr(config, key)
             else:
                 helper.error(400, "invalid key")
                 return
         else:
             result = entity_to_hash(config)
     else:
         result = {}
     helper.write_json(result)
Ejemplo n.º 29
0
 def get(self, key):
     helper = RequestHelper(self)
     config = ConfigurationAccessor.get_or_create()
     result = None
     if config:
         if key:
             if entity_has_property(config, key):
                 result = getattr(config, key)
             else:
                 helper.error(400, "invalid key")
                 return
         else:
             result = entity_to_hash(config)
     else:
         result = {}
     helper.write_json(result)
Ejemplo n.º 30
0
 def post(self):
     helper = RequestHelper(self)
     
     json_body = self.request.body
     if not json_body:
         helper.error(400, "body required")
         return
         
     decoded_body = urllib.unquote(json_body)
     try:
         body_hash = json.loads(decoded_body)
     except json.JSONDecodeError, e:
         msg = "malformed json: %s" % decoded_body
         helper.error(400, msg)
         logging.info(msg)
         return
Ejemplo n.º 31
0
    def get(self, source_name, **kw):
        helper = RequestHelper(self)

        try:
            speaker_name = self.request.get("s", None)
            if speaker_name:
                speaker = new_speaker(speaker_name)[1]
                mixer = Mixer.new(speaker)
            else:
                speaker_name, speaker = new_random_speaker()
                logging.debug("speaker: %s" % str(speaker))
                mixer = Mixer(speaker)

            # direct message
            message = self.request.get("q", None)
            if message:
                message = urllib.unquote(message)
                sources, content = mixer.mix_response(message)
            else:
                if not source_name:
                    sources, content = mixer.mix_random_limit_sources(
                        2, degrade=True)
                else:
                    source_name = urllib.unquote(source_name)
                    logging.debug("get source_name: %s" % source_name)
                    if ";" in source_name:
                        # multiple sources
                        sources_split = set(source_name.split(";"))
                        sources, content = mixer.mix_sources(*sources_split)
                    else:
                        # single source
                        sources, content = mixer.mix_sources(source_name)
            logging.debug("sources: %s" % str(sources))
            source_hash_list = [s.name for s in sources]
            mix_hash = {
                "sources": source_hash_list,
                "speaker": {
                    "name": speaker_name,
                    "id": hash(speaker),
                    "details": str(speaker)
                },
                "body": content
            }
            helper.write_json(mix_hash)
        except NotFoundException, ex:
            helper.error(404, Exceptions.format(ex))
            logging.error(ex)
Ejemplo n.º 32
0
 def post(cls, request_handler, **kw):
     helper = RequestHelper(request_handler)
     request = request_handler.request
     username = kw.get("username", None)
     user = User(username) if username else users.get_current_user()
     
     json_body = request.body
     if not json_body:
         helper.error(400, "body required")
         return
         
     decoded_body = urllib.unquote(json_body)
     try:
         artifact_hash = json.loads(decoded_body)
     except json.JSONDecodeError, e:
         msg = "malformed json: %s" % decoded_body
         helper.error(400, msg)
         logging.info(msg)
         return
Ejemplo n.º 33
0
    def get(self):
        helper = RequestHelper(self)
        try:
            if not users.is_current_user_admin():
                helper.error(403, "admin required")
                return

            oauth_token = self.request.get("oauth_token")
            if not oauth_token:
                # initial request, I store the request token key/secret as cookies and redirect to twitter's auth page
                request_token, auth_url = TwitterConnector.get_request_token_and_auth_url()
                logging.debug("request token key:%s, secret:%s" % (request_token.key, request_token.secret))

                helper.set_cookie("%s=%s; path=/" % (self._rtoken_key_cookie, request_token.key))
                helper.set_cookie("%s=%s; path=/" % (self._rtoken_secret_cookie, request_token.secret))
                helper.write_text("redirecting request token %s to %s" % (request_token.to_string(), auth_url))
                self.redirect(auth_url)
            else:
                # post verification request
                key = self.request.cookies.get(self._rtoken_key_cookie, None)
                secret = self.request.cookies.get(self._rtoken_secret_cookie, None)
                if not (key and secret):
                    helper.error(400, "key and secret not stored as cookies")
                    return
                access_token = TwitterConnector.get_access_token(key, secret)
                token_string = access_token.to_string()
                ConfigurationAccessor.update(twitter_access_token=token_string)
                helper.write_text("saved access token %s" % token_string)
        except Exception, e:
            msg = traceback.print_exc()
            helper.error(500, msg)
Ejemplo n.º 34
0
    def get(self, source_name, **kw):
        helper = RequestHelper(self)

        try:
            speaker_name = self.request.get("s", None)
            if speaker_name:
                speaker = new_speaker(speaker_name)[1]
                mixer = Mixer.new(speaker)
            else:
                speaker_name, speaker = new_random_speaker()
                logging.debug("speaker: %s" % str(speaker))
                mixer = Mixer(speaker)
        
            # direct message
            message = self.request.get("q", None)
            if message:
                message = urllib.unquote(message)
                sources, content = mixer.mix_response(message)
            else:
                if not source_name:
                    sources, content = mixer.mix_random_limit_sources(2, degrade=True)
                else:
                    source_name = urllib.unquote(source_name)
                    logging.debug("get source_name: %s" % source_name)
                    if ";" in source_name:
                        # multiple sources
                        sources_split = set(source_name.split(";"))
                        sources, content = mixer.mix_sources(*sources_split)
                    else:
                        # single source
                        sources, content = mixer.mix_sources(source_name)
            logging.debug("sources: %s" % str(sources))
            source_hash_list = [s.name for s in sources]
            mix_hash = {"sources": source_hash_list,
                "speaker": {"name": speaker_name, "id": hash(speaker), "details": str(speaker)},
                "body": content}
            helper.write_json(mix_hash)
        except NotFoundException, ex:
            helper.error(404, Exceptions.format(ex))
            logging.error(ex)
Ejemplo n.º 35
0
    def get(self, url):
        helper = RequestHelper(self)
        resource = UrlResource.get_by_url(url, return_none=True)
        if not resource:
            helper.error(404)
            return
        helper.write_json(resource_hash(resource))


# application = webapp.WSGIApplication([
#     ('/api/resources/-/search', ResourcesSearchHandler),
#     ('/api/resources/?(\d+)?', ResourcesHandler),
#     ('/api/resources/(.+)', ResourceHandler)
#     ])
# 
# 
# def main():
#   log.config()
#   run_wsgi_app(application)
# 
# if __name__ == "__main__":
#   main()
Ejemplo n.º 36
0
    def get(self):
        helper = RequestHelper(self)
        try:
            if not users.is_current_user_admin():
                helper.error(403, "admin required")
                return

            oauth_token = self.request.get("oauth_token")
            if not oauth_token:
                # initial request, I store the request token key/secret as cookies and redirect to twitter's auth page
                request_token, auth_url = TwitterConnector.get_request_token_and_auth_url(
                )
                logging.debug("request token key:%s, secret:%s" %
                              (request_token.key, request_token.secret))

                helper.set_cookie("%s=%s; path=/" %
                                  (self._rtoken_key_cookie, request_token.key))
                helper.set_cookie(
                    "%s=%s; path=/" %
                    (self._rtoken_secret_cookie, request_token.secret))
                helper.write_text("redirecting request token %s to %s" %
                                  (request_token.to_string(), auth_url))
                self.redirect(auth_url)
            else:
                # post verification request
                key = self.request.cookies.get(self._rtoken_key_cookie, None)
                secret = self.request.cookies.get(self._rtoken_secret_cookie,
                                                  None)
                if not (key and secret):
                    helper.error(400, "key and secret not stored as cookies")
                    return
                access_token = TwitterConnector.get_access_token(key, secret)
                token_string = access_token.to_string()
                ConfigurationAccessor.update(twitter_access_token=token_string)
                helper.write_text("saved access token %s" % token_string)
        except Exception, e:
            msg = traceback.print_exc()
            helper.error(500, msg)