|
| 1 | +#!/bin/env python3 |
| 2 | +import sys |
| 3 | +import argparse |
| 4 | +import arrow |
| 5 | +import time |
| 6 | +from youtubeapi import YouTube |
| 7 | +from datastore import DataStore |
| 8 | +from commenter import Comment |
| 9 | + |
| 10 | +def print_from_youtube(headers, data): |
| 11 | + """Print the provided header and data in a visually pleasing manner |
| 12 | +
|
| 13 | + Args: |
| 14 | + headers (list of str): The headers to print |
| 15 | + data (list of list of str): The data rows |
| 16 | + """ |
| 17 | + |
| 18 | + if (len(data) == 0): |
| 19 | + return |
| 20 | + separators = [] |
| 21 | + for word in headers: |
| 22 | + separators.append('-' * len(word)) |
| 23 | + output = [headers, separators] + data |
| 24 | + col_widths = [0] * len(headers) |
| 25 | + for row in output: |
| 26 | + for idx, column in enumerate(row): |
| 27 | + if len(column) > col_widths[idx]: |
| 28 | + col_widths[idx] = len(column) |
| 29 | + for row in output: |
| 30 | + for idx, column in enumerate(row): |
| 31 | + print("".join(column.ljust(col_widths[idx])), end = ' ' * 2) |
| 32 | + print() |
| 33 | + |
| 34 | +def print_from_database(store): |
| 35 | + print("{}".format("_"*186)) |
| 36 | + print("|{:-^30}|{:-^30}|{:-^60}|{:-^30}|{:-^30}|".format('ID', 'Username', 'Title', 'Added On', 'Last Checked')) |
| 37 | + for item in store.get_channels(): |
| 38 | + print("|{:^30}|{:^30}|{:^60}|{:^30}|{:^30}|".format(item['id'], item['username'], str(item['title']), arrow.get(item['added_on']).humanize(), arrow.get(item['last_checked']).humanize())) |
| 39 | + print("|{}|".format("_"*184)) |
| 40 | + |
| 41 | +def get_parser(): |
| 42 | + parser = argparse.ArgumentParser() |
| 43 | + parser.add_argument('-i', '--id', help = 'Channel ID', default = None) |
| 44 | + parser.add_argument('-u', '--username', help = 'Username', default = None) |
| 45 | + parser.add_argument('action', help = 'Perform the specified action', default = 'check', |
| 46 | + nargs = '?', choices = ['add', 'check', 'list', 'remove']) |
| 47 | + return parser |
| 48 | + |
| 49 | +def main(): |
| 50 | + """Parse the command line arguments, expecting one of the following formats: |
| 51 | + -) (-i ChannelID | -u Username) (add | check | remove) |
| 52 | + -) check | list |
| 53 | + and perform the appropriate action |
| 54 | + """ |
| 55 | + print("Starting..") |
| 56 | + parser = get_parser() |
| 57 | + args = parser.parse_args() |
| 58 | + |
| 59 | + youtube = YouTube() |
| 60 | + store = DataStore('username', 'passw', 'host', 'dbname') # Your db credentials |
| 61 | + |
| 62 | + channel = None |
| 63 | + if args.username is not None: |
| 64 | + channel = youtube.get_channel_by_username(args.username) |
| 65 | + elif args.id is not None: |
| 66 | + channel = youtube.get_channel_by_id(args.id) |
| 67 | + |
| 68 | + if args.action == 'add': |
| 69 | + store.store_channel(channel) |
| 70 | + elif args.action == 'remove': |
| 71 | + store.remove_channel(channel) |
| 72 | + elif args.action == 'list': |
| 73 | + print_from_database(store) |
| 74 | + elif args.action == 'check': |
| 75 | + print("Done! Waiting for new videos to be uploaded..") |
| 76 | + while True: |
| 77 | + # If the user passed a specific channel, check for new uploads |
| 78 | + # otherwhise check for uploads from every previously added channel |
| 79 | + channels = [] |
| 80 | + if channel is not None: |
| 81 | + channels.append(store.get_channel_by_id(channel['id'])) |
| 82 | + else: |
| 83 | + channels = store.get_channels() |
| 84 | + |
| 85 | + data = [] |
| 86 | + to_check = dict() |
| 87 | + for channel_item in channels: |
| 88 | + to_check[channel_item['id']] = channel_item['last_checked'] |
| 89 | + |
| 90 | + uploads = youtube.get_uploads(to_check) |
| 91 | + try: |
| 92 | + for upload in uploads: |
| 93 | + current_link = 'https://youtube.com/watch?v=%s' % (upload['id'], ) |
| 94 | + data.append([ |
| 95 | + upload['channel_title'], |
| 96 | + upload['title'], |
| 97 | + arrow.get(upload['published_at']).humanize(), |
| 98 | + current_link |
| 99 | + ]) |
| 100 | + Comment(youtube.api, upload['id'], upload['channel_title']) |
| 101 | + |
| 102 | + print_from_youtube(['Channel', 'Title', 'Published', 'Link'], data) |
| 103 | + |
| 104 | + for channel_id in to_check.keys(): |
| 105 | + store.update_last_checked(channel_id) |
| 106 | + |
| 107 | + # Look for new videos every 15 seconds |
| 108 | + time.sleep(15) |
| 109 | + except BaseException as be: |
| 110 | + # If it reaches the 100 seconds api threshold, wait for 100 seconds |
| 111 | + print("Error: Too many requests:\n{}".format(be)) |
| 112 | + print("Waiting 100 seconds..") |
| 113 | + time.sleep(100) |
| 114 | + print("Waiting for new videos to be uploaded..") |
| 115 | + |
| 116 | +if __name__ == '__main__': |
| 117 | + main() |
0 commit comments