Exemplo n.º 1
0
    def put(self, groupname):
        """
		 Create a new feed providing the name and url are unique.
		 Feeds must be associated with a group.
		"""
        key = auth(forbid_reader_keys=True)

        parser = restful.reqparse.RequestParser()
        parser.add_argument("name", type=str, help="", required=True)
        parser.add_argument("url", type=str, help="", required=True)
        parser.add_argument("schedule", type=str, help="", required=True)
        parser.add_argument("active",
                            type=bool,
                            default=True,
                            help="Feed is active",
                            required=False)
        args = parser.parse_args()

        fg = FeedGroup.query.filter(
            and_(FeedGroup.key == key, FeedGroup.name == groupname)).first()
        if not fg:
            return {"message": "Unknown Feed Group %s" % groupname}, 304

        # Verify the schedule
        try:
            parse_timings(args.schedule)
        except CronError, err:
            return {"message": err.message}, 500
Exemplo n.º 2
0
def parse_crontab(filename):
	"""
	Get a file descriptor on filename and
	create feeds and groups for API keys therein.
	"""
	def log(message):
		print message
	# read filename into a string named crontab
	try:
		fd = open(filename, "r")
	except OSError:
		print "Error opening %s" % filename
		raise SystemExit
	crontab = fd.read()
	fd.close()

	# keep a resident api key on hand
	key = None

	for i, line in enumerate(crontab.split('\n')):

		# Set the APIKey we're working with when we find a line starting
		# with apikey:
		if line.startswith("apikey:"):
			if ' ' in line:
				key_str = line.split()[1]
				key = APIKey.query.filter(APIKey.key == key_str).first()
			if not key:
				print 'Malformed or unknown API key at line %i in %s: %s' % (i+1, filename, line)
				raise SystemExit
			else:
				print 'Using API key "%s".' % key.name

		if line.startswith("http"):
			feed = {'active': True}

			# Grab the URL and set the string to the remainder
			feed['url'] = line.split().pop(0)
			line = ' '.join(line.split()[1:])

			# Grab names and groups
			names = spaceparse(line)
			if not names:
				print "Error parsing feed or group name at line %i in %s: %s" % (i+1, filename, line)
				continue
			feed['name'], group = names[:2]

			# The schedule should be the last five items
			schedule = line.split()[-5:]
			try:
				parse_timings(schedule)
			except Exception, e:
				print "Error parsing schedule at line %i in %s: %s" % (i+1, filename, e.message)
				continue

			feed['schedule'] = ' '.join(schedule)

			create_feed(log, db, key, group, feed)
Exemplo n.º 3
0
    def post(self, groupname, name):
        """
		 Modify an existing feed.
		"""
        key = auth()

        parser = restful.reqparse.RequestParser()
        parser.add_argument("name", type=str, help="")
        parser.add_argument("group", type=str, help="")
        parser.add_argument("url", type=str, help="")
        parser.add_argument("schedule", type=str, help="")
        parser.add_argument("active",
                            type=bool,
                            default=None,
                            help="Feed is active")
        args = parser.parse_args()

        feed = Feed.query.filter(and_(Feed.key == key,
                                      Feed.name == name)).first()
        if not feed:
            restful.abort(404)

        if args.name:
            if Feed.query.filter(and_(Feed.key == key,
                                      Feed.name == args.name)).first():
                return {
                    "message": "A feed already exists with this name."
                }, 304
            feed.name = args.name

        if args.group:
            pass

        if args.active != None:
            feed.active = args.active

        if args.url:
            feed.url = args.url

        if args.schedule:
            try:
                parse_timings(args.schedule)
            except CronError, err:
                return {"message": err.message}, 500
            feed.schedule = args.schedule
Exemplo n.º 4
0
def create_feed(log, db, key, group, feed):
	"""
	Takes a key object, a group name and a dictionary
	describing a feed ({name:,url:,schedule:,active:})
	and reliably attaches a newly created feed to the key
	and group.
	"""
	if not type(feed) == dict:
		log('Unexpected type when creating feed for API key "%s"' % key.name)
		return

	for i in ['name', 'schedule', 'active', 'url']:
		if not i in feed.keys():
			log('%s: Error creating feed. Missing "%s" field from feed definition.' % (key.name, i))
			return

	f = Feed.query.filter(and_(Feed.key == key, Feed.name == feed['name'])).first()
	fg = FeedGroup.query.filter(and_(FeedGroup.key == key, FeedGroup.name == group)).first()

	if f:
		if f.group:
			log('%s: Error creating feed "%s" in group "%s", feed already exists in group "%s".' % \
				(key.name, feed['name'], group, f.group.name))
			return
		elif fg:
			log('%s: %s: Adding feed "%s"' % (key.name, fg.name, f.name))
			fg.append(f)
			db.session.add(fg)
			db.session.add(f)
			db.session.commit()
			return 

	if not fg:
		log('%s: Creating feed group %s.' % (key.name, group))
		fg = FeedGroup(name=group)
		key.feedgroups.append(fg)

	try:
		parse_timings(feed['schedule'])
	except Exception, e:
		log('%s: %s: Error creating "%s": %s' % \
			(key.name, fg.name, feed['name'], e.message))	
Exemplo n.º 5
0
	def create_crontab(self, feed):
		t        = cron.parse_timings(feed.schedule.split())
		evt      = cron.Event(                   # One possible design for these crontabs
					fetch.fetch_feed,            # is to have them correspond to a FeedGroup
					t[0], t[1], t[2], t[3], t[4],# where each event is a member feed
					[feed, self.log])            # and stopping the crontab stops the group.
		evt.feed = feed
		ct       = cron.CronTab(evt)
		ct.name  = self.generate_ct_name(feed)
		ct.inbox = Queue()
		return ct
Exemplo n.º 6
0
	def post(self, groupname, name):
		"""
		 Modify an existing feed.
		"""
		key = auth()

		parser = restful.reqparse.RequestParser()
		parser.add_argument("name",type=str, help="")
		parser.add_argument("group",type=str, help="")
		parser.add_argument("url",type=str, help="")
		parser.add_argument("schedule",type=str, help="")
		parser.add_argument("active",type=bool, default=None, help="Feed is active")
		args = parser.parse_args()

		feed = Feed.query.filter(and_(Feed.key == key, Feed.name == name)).first()
		if not feed:
			restful.abort(404)

		if args.name:
			if Feed.query.filter(and_(Feed.key == key, Feed.name == args.name)).first():
				return {"message":"A feed already exists with this name."}, 304
			feed.name = args.name

		if args.group:
			pass

		if args.active != None:
			feed.active = args.active

		if args.url:
			feed.url = args.url

		if args.schedule:
			try:
				parse_timings(args.schedule)
			except CronError, err:
				return {"message": err.message}, 500
			feed.schedule = args.schedule
Exemplo n.º 7
0
    def put(self, groupname):
        """
         Create a new feed providing the name and url are unique.
         Feeds must be associated with a group.
        """
        key = auth(forbid_reader_keys=True)

        parser = restful.reqparse.RequestParser()
        parser.add_argument("name",     type=str, required=True)
        parser.add_argument("url",      type=str, required=True)
        parser.add_argument("schedule", type=str, required=True)
        parser.add_argument("active",   type=bool, default=True, help="Feed is active", required=False)
        args = parser.parse_args()

        fg = FeedGroup.query.filter(and_(FeedGroup.key == key, FeedGroup.name == groupname)).first()
        if not fg:
            return {"message":"Unknown Feed Group %s" % groupname}, 304

        # Verify the schedule
        try:
            parse_timings(args.schedule)
        except CronError, err:
            return {"message": err.message}, 500