Exemple #1
0
def run(payload):
    """ Executes the thread with the settings """

    try:
        task = json.loads(payload)
    except:
        # logger.error(traceback.format_exc()) <-   You want to implement some
        #                                           logging here to catch this
        return

    # I don't normally care enough to track if it failed or not... but the
    # following returns True or False according to its success.
    # Consider resubmitting the task to the REDIS queue on a fail.
    track(task)
Exemple #2
0
def run(payload):
    """ Executes the thread with the settings """

    try:
        task = json.loads(payload)
    except:
        # logger.error(traceback.format_exc()) <-   You want to implement some
        #                                           logging here to catch this
        return

    # I don't normally care enough to track if it failed or not... but the
    # following returns True or False according to its success.
    # Consider resubmitting the task to the REDIS queue on a fail.
    track(task)
Exemple #3
0
    def upvote(self, comment):
        """ Given a HNComment, upvote the associated comment """
        if settings.UPVOTE_ENABLED:
            res = self.make_request(comment.upvote_url)
            print res.status_code, res.text

            # track comment to mixpanel
            mixpanel.track('Upvoted Comment', {
                'Comment ID': comment.id,
                'Text Length': len(comment.text),
                'Debug Mode': not settings.UPVOTE_ENABLED,
            }.update(comment.sentiment['probability']))

            sleep(settings.VOTE_DELAY)
        else:
            print 'Would upvote %s' % comment
Exemple #4
0
def run_positivity_bot():

    import argparse
    parser = argparse.ArgumentParser(description='Hacker News Positivity Bot')
    parser.add_argument('session', help='Session cookie (value of "user" cookie on news.ycombinator.com when logged in)')
    parser.add_argument('--validate', action='store_true', help='Validate session cookie only, then exit')
    args = parser.parse_args()

    api = HackerNews(args.session)

    if not api.validate_session():
        print 'Error: session is invalid!'
        exit(1)

    if args.validate:
        print 'Session OK'
        exit(0)

    for post in api.get_posts(settings.NUM_POSTS):
        print 'Processing %s' % post

        comments = api.get_comments(post.url, settings.NUM_COMMENTS)
        print 'Fetched %d comments' % len(comments)

        total, avg, num_positive, num_negative = aggregate_stats(comments)

        print 'Total positivity: %f  Average positivity: %f' % (total, avg)
        print 'Positive Comments: %d  Negative Comments: %d' % (num_positive, num_negative)

        # track stats to mixpanel
        mixpanel.track("Post Analyzed", {
            'Post ID': post.id,
            'Comments': len(comments),
            'Average Positivity': avg,
            'Positive Comments': num_positive,
            'Negative Comments': num_negative,
        })

        upvotes = filter(lambda comment: comment.category == 'pos', comments)
        print 'Upvoting %d comments' % len(upvotes)
        for comment in upvotes:
            api.upvote(comment)
Exemple #5
0
    def post(self):
        gen = self.get_argument("gen")
        if gen: gen = gen.lower()

        if not self._is_valid_gen_value(gen):
            raise tornado.web.HTTPError(
                400,
                "Invalid 'gen' value. Please refer to the documentation at http://thriftify.org/documentation"
            )

        do_zip = self._do_zip()

        thrift_bin = self._get_thrift_bin_by_version(
            self.get_argument("thriftversion", None))

        first_filename = None

        url = self.get_argument("url", None)

        if url:
            http_client = httpclient.AsyncHTTPClient()
            http_client.fetch(url, self._handle_request)
            return

        path = os.path.join(settings.TEMP_PATH, self._generate_temp_id())
        os.makedirs(path)
        try:
            if len(self.request.files) > 0:
                for k in self.request.files:
                    o = self.request.files[k][0]
                    filename = o["filename"]

                    if not first_filename:
                        first_filename = filename

                    f = open(os.path.join(path, filename), "wb")
                    try:
                        f.write(o["body"])
                    finally:
                        f.close()
            else:
                thrift_file_content = self.get_argument("thriftcontent")
                f = open(os.path.join(path, "file.thrift"), "w")
                try:
                    f.write(thrift_file_content)
                    first_filename = "file.thrift"
                finally:
                    f.close()

            self._generate_bindings(thrift_bin, path, first_filename, gen=gen)

            if do_zip:
                package_filename = self._pack_zip_result(
                    path, first_filename, gen)
                self.set_header("Content-Type", "application/octet-stream")
                self.set_header("Content-Disposition",
                                "attachment; filename=%s" % package_filename)
                f = open(os.path.join(path, package_filename), "rb")
                try:
                    self.write(f.read())
                finally:
                    f.close()

                if options.mixpaneltoken:
                    mixpanel.track(options.mixpaneltoken, "GenerateAPI",
                                   {"gen": gen})
            else:
                json = self._pack_json_result(path, first_filename, gen)
                self.set_header("Content-Type", "application/json")
                self.write(json)

            self.finish()
        finally:
            logging.debug("Removing temp path '" + path + "'")
            shutil.rmtree(path)
Exemple #6
0
	def post(self):
		gen = self.get_argument("gen")
		if gen: gen = gen.lower()

		if not self._is_valid_gen_value(gen):
			raise tornado.web.HTTPError(400, "Invalid 'gen' value. Please refer to the documentation at http://thriftify.org/documentation")

		do_zip = self._do_zip()

		thrift_bin = self._get_thrift_bin_by_version(self.get_argument("thriftversion", None))	
		

		first_filename = None

		url = self.get_argument("url", None)

		if url:
			http_client = httpclient.AsyncHTTPClient()
			http_client.fetch(url, self._handle_request)
			return

		path = os.path.join(settings.TEMP_PATH, self._generate_temp_id())
		os.makedirs(path)
		try:
			if len(self.request.files) > 0:
				for k in self.request.files:
					o = self.request.files[k][0]
					filename = o["filename"]

					if not first_filename:
						first_filename = filename

					f = open(os.path.join(path, filename), "wb")
					try:
						f.write(o["body"])
					finally:
						f.close()
			else:
				thrift_file_content = self.get_argument("thriftcontent")
				f = open(os.path.join(path, "file.thrift"), "w")
				try:
					f.write(thrift_file_content)
					first_filename = "file.thrift"
				finally:
					f.close()

			self._generate_bindings(thrift_bin, path, first_filename, gen=gen)

			if do_zip:
				package_filename = self._pack_zip_result(path, first_filename, gen)
				self.set_header("Content-Type", "application/octet-stream")
				self.set_header("Content-Disposition","attachment; filename=%s" % package_filename)
				f = open(os.path.join(path, package_filename), "rb")
				try:
					self.write(f.read())
				finally:
					f.close()

				if options.mixpaneltoken:
					mixpanel.track(options.mixpaneltoken, "GenerateAPI", { "gen" : gen })
			else:
				json = self._pack_json_result(path, first_filename, gen)
				self.set_header("Content-Type", "application/json")
				self.write(json)

			self.finish()
		finally:
			logging.debug("Removing temp path '" + path + "'")
			shutil.rmtree(path)