Example #1
0
def main():
	"""Parse the command line arguments, expecting one of the following formats:
		-) (-i ChannelID | -u Username) (add | check | remove)
		-) check | list
	and perform the appropriate action
	"""
	parser = get_parser()
	args = parser.parse_args()

	youtube = YouTube()
	store = DataStore('%s-data.sqlite3' % sys.argv[0], 'schema.sql')

	channel = None
	if args.username is not None:
		channel = youtube.get_channel_by_username(args.username)
	elif args.id is not None:
		channel = youtube.get_channel_by_id(args.id)

	if args.action == 'add':
		store.store_channel(channel)
	elif args.action == 'remove':
		store.remove_channel(channel)
	elif args.action == 'list':
		data = []
		for item in store.get_channels():
			data.append([
				item['id'],
				item['title'],
				arrow.get(item['added_on']).humanize(),
				arrow.get(item['last_checked']).humanize()
			])

		pretty_print(['ID', 'Title', 'Added', 'Last Checked'], data)
	elif args.action == 'check':
		# If the user passed a specific channel, check for new uploads
		# otherwhise check for uploads from every previously added channel
		channels = []
		if channel is not None:
			channels.append(store.get_channel_by_id(channel['id']))
		else:
			channels = store.get_channels()

		data = []
		to_check = dict()
		for channel_item in channels:
			to_check[channel_item['id']] = channel_item['last_checked']

		uploads = youtube.get_uploads(to_check)
		for upload in uploads:
			data.append([
				upload['channel_title'],
				upload['title'],
				arrow.get(upload['published_at']).humanize(),
				'https://youtube.com/watch?v=%s' % (upload['id'], )
			])

		pretty_print(['Channel', 'Title', 'Published', 'Link'], data)

		for channel_id in to_check.keys():
			store.update_last_checked(channel_id)
Example #2
0
def main():
    """Parse the command line arguments, expecting one of the following formats:
        -) (-i ChannelID | -u Username) (add | check | remove)
        -) check | list
    and perform the appropriate action
    """
    print("Starting..")
    parser = get_parser()
    args = parser.parse_args()

    youtube = YouTube()
    store = DataStore('username', 'passw', 'host', 'dbname') # Your db credentials

    channel = None
    if args.username is not None:
        channel = youtube.get_channel_by_username(args.username)
    elif args.id is not None:
        channel = youtube.get_channel_by_id(args.id)

    if args.action == 'add':
        store.store_channel(channel)
    elif args.action == 'remove':
        store.remove_channel(channel)
    elif args.action == 'list':
        print_from_database(store)
    elif args.action == 'check':
        print("Done! Waiting for new videos to be uploaded..")
        while True:
            # If the user passed a specific channel, check for new uploads
            # otherwhise check for uploads from every previously added channel
            channels = []
            if channel is not None:
                channels.append(store.get_channel_by_id(channel['id']))
            else:
                channels = store.get_channels()

            data = []
            to_check = dict()
            for channel_item in channels:
                to_check[channel_item['id']] = channel_item['last_checked']

            uploads = youtube.get_uploads(to_check)
            try:
                for upload in uploads:
                    current_link = 'https://youtube.com/watch?v=%s' % (upload['id'], )
                    data.append([
                        upload['channel_title'],
                        upload['title'],
                        arrow.get(upload['published_at']).humanize(),
                        current_link
                    ])
                    Comment(youtube.api, upload['id'], upload['channel_title'])

                print_from_youtube(['Channel', 'Title', 'Published', 'Link'], data)

                for channel_id in to_check.keys():
                    store.update_last_checked(channel_id)

                # Look for new videos every 15 seconds
                time.sleep(15)
            except BaseException as be:
                # If it reaches the 100 seconds api threshold, wait for 100 seconds
                print("Error: Too many requests:\n{}".format(be))
                print("Waiting 100 seconds..")
                time.sleep(100)
                print("Waiting for new videos to be uploaded..")