youtubecallback 2 KB
Newer Older
Gregory Martin committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#!/usr/bin/env python
import os
import sys
import argparse

project_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
if project_path not in sys.path:
    sys.path.append(project_path)

from control.veda_utils import EmailAlert
from youtube_callback.daemon import generate_course_list, get_course
from youtube_callback.sftp_id_retrieve import callfunction

"""
Youtube Callback

Command Line Interface
"""
19

Gregory Martin committed
20

21
class YoutubeCallbackCli(object):
Gregory Martin committed
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52

    def __init__(self, **kwargs):
        self.args = None
        self.test = False
        self.logging = kwargs.get('logging', True)

        self.course_list = []

    def get_args(self):
        parser = argparse.ArgumentParser()
        parser.usage = '''
        {cmd} -l List
        {cmd} -c course
        [-l -c ]
        Use --help to see all options.
        '''.format(cmd=sys.argv[0])

        parser.add_argument(
            '-l', '--list',
            help='List Eligible Courses, Exit',
            action='store_true'
            )

        parser.add_argument(
            '-c', '--courseid',
            default=None,
            help='Parse Specific Course ID, Exit',
            )

        self.args = parser.parse_args()

53
        self._parse_args()
Gregory Martin committed
54

55
    def _parse_args(self):
Gregory Martin committed
56 57
        self.course_id = self.args.courseid

58
    def run(self):
Gregory Martin committed
59

60
        self.loop()
Gregory Martin committed
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84

    def loop(self):
        """
        Daemon Loop
        """
        if self.course_id is not None:
            course = get_course(course_id=self.course_id)
            if course is not None:
                callfunction(course)
            return None

        runcommand = ' '.join((
            'python',
            os.path.join(os.path.dirname(os.path.abspath(__file__)), 'loop.py'),
            '-y'
        ))
        os.system(runcommand)
        E1 = EmailAlert(message='Youtube Callback Daemon Crash', subject='Youtube Callback Daemon')
        E1.email()


def main():
    YTCC = YoutubeCallbackCli()
    YTCC.get_args()
85
    YTCC.run()
Gregory Martin committed
86 87 88

if __name__ == '__main__':
    sys.exit(main())