def retrieve_csv(self, camera_url_file, duration, interval, result_path):
        """
        Reads camera urls from csv file and archives the images at the requested directory.
        """

        #verify file exists and can be read
        if not check_file_exists(camera_url_file):
            return -1

        with open(camera_url_file, 'r') as camera_file:
            camera_reader = csv.reader(camera_file)
            id = 1
            cams = []
            for camera_url in camera_reader:
                #These cameras do not come from the database and so have no ID.  Assign one to them so they can be placed in a result folder.
                camera_type = camera_url[0].split(".")[-1]
                if (camera_type == "m3u8"):
                    camera = StreamCamera(id, duration, interval,
                                          camera_url[0])
                else:
                    camera = NonIPCamera(id, duration, interval, camera_url[0])
                id += 1
                cams.append(camera)
        if len(cams):
            self.__archive_cameras(cams, result_path)
def parse_input(args):
    # List of camera objects to archive
    cams = []
    # Read the input arguments.
    parser = argparse.ArgumentParser(description="This program downloads image snapshots from 2 sources\
        (1) A given URL address (2) A camera ID in the MySQL database * MySQL database must be available on host computer.")
    parser.add_argument('-f','--filename',help="Name of CSV file containing camera info with .csv extension(should\
         be in same directory as program)",type = str)
    parser.add_argument('-l','--list',help="List of camera ID's to be archived separated by spaces. \
        * This requires a version of the MYSQL database on your local machine.", type = int, nargs='+')
    parser.add_argument('-d','--duration',help="Duration of time to archive snapshots from the cameras. Required\
         for -l argument.",type = int)
    parser.add_argument('-i','--interval',help="Interval between snapshots from each camera. Required\
         for -l argument.",type = int)
    args = parser.parse_args()

    if args.filename != None:
        if(os.path.isfile(args.filename) != True):
            print("Input File \"{}\" could not be found.".format(args.filename))
        else:
            f = open(args.filename)
            f_csv = csv.reader(f, delimiter=",")
            # Skip row labels
            f_csv.next()
            for line in f_csv:
                try:
                    camera_id = int(line[0])
                except:
                    raise(Exception("Error: No camera_id exists in line {} of input file \"{}\"".format(line, args.filename)))
                try:
                    duration = int(line[2])
                    interval = int(line[3])
                except:
                    raise(Exception("Error: No duration or interval exists in line {} of input file \"{}\"".format(line, args.filename)))
                if line[5] != '':
                    url = line[5]
                    cam = NonIPCamera(camera_id, duration, interval, url)
                    if cam != None:
                        cams.append(cam)
                else:
                    cam = get_camera_db(camera_id, duration, interval)
                    if cam != None:
                        cams.append(cam)


            # Do file parsing Stuff
    if args.list != None: 
        if args.duration != None and args.interval != None:
            for ID in args.list:
                cam = get_camera_db(ID, args.duration, args.interval)
                if cam != None:
                    cams.append(cam)
        else:
            parser.print_help()

    if len(cams) == 0:
        parser.print_help()

    return cams
    def __get_camera_from_db(self, camera_id, duration, interval):
        """
        Reads camera IDs from file, and queries database for those cameras.  Archives the images from those cameras in the indicated result path.
        """
        connection = MySQLdb.connect(self.db_server, self.db_username,
                                     self.db_password, self.db_name)
        cursor = connection.cursor()

        camera = None
        #
        cursor.execute(
            'SELECT camera.id, ip_camera.ip, ip_camera.port, '
            'ip_camera_model.image_path, ip_camera_model.video_path '
            'FROM camera, ip_camera, ip_camera_model '
            'WHERE camera.id = ip_camera.camera_id '
            'and ip_camera.ip_camera_model_id = ip_camera_model.id '
            'and camera.id = {};'.format(camera_id))
        camera_row = cursor.fetchone()

        # Create an IPCamera object.
        if camera_row:
            camera = IPCamera(camera_row[0], duration, interval, camera_row[1],
                              camera_row[3], camera_row[4],
                              camera_row[2])  # 1 is ip and 3 is image path
        else:
            # Get the non-IP camera with the given ID.
            cursor.execute('select camera.id, non_ip_camera.snapshot_url '
                           'FROM camera, non_ip_camera '
                           'WHERE camera.id = non_ip_camera.camera_id '
                           'and camera.id = {};'.format(camera_id))
            camera_row = cursor.fetchone()

            # Create a NonIPCamera object.
            if camera_row:
                camera = NonIPCamera(camera_row[0], duration, interval,
                                     camera_row[1])
            else:
                # Get the stream camera with the given ID.
                cursor.execute('select camera.id, camera.m3u8_key '
                               'FROM camera, stream_camera '
                               'WHERE camera.id = stream_camera.camera_id '
                               'and camera.id = {};'.format(camera_id))
                camera_row = cursor.fetchone()
                # Create a stream camera object.
                if camera_row:
                    camera = StreamCamera(camera_row[0], duration, interval,
                                          camera_row[1])

        cursor.close()
        connection.close()

        if not camera:
            print 'There is no camera with the ID {}.'.format(camera_id)
            return None

        return camera
def get_camera_db(camera_id, duration, interval):
    """ Get a camera from the database. """
    try:
        import MySQLdb
    except Exception as e:
        raise(e)

    # The server database credentials.
    DB_SERVER = 'localhost'
    DB_USER_NAME = 'root'
    DB_PASSWORD = '******'
    DB_NAME = 'cam2'

    camera = None

    # Connect to the database, and get the connection cursor
    connection = MySQLdb.connect(DB_SERVER, DB_USER_NAME, DB_PASSWORD, DB_NAME)
    cursor = connection.cursor()

    # Get the IP camera with the given ID.
    cursor.execute('SELECT camera.id, ip_camera.ip, ip_camera.port, '
                   'ip_camera_model.image_path, ip_camera_model.video_path '
                   'FROM camera, ip_camera, ip_camera_model '
                   'WHERE camera.id = ip_camera.camera_id '
                   'and ip_camera.ip_camera_model_id = ip_camera_model.id '
                   'and camera.id = {};'.format(camera_id))
    camera_row = cursor.fetchone()

    # Create an IPCamera object.
    if camera_row:
        camera = IPCamera(camera_row[0], duration, interval, camera_row[1], camera_row[3],
                          camera_row[4], camera_row[2])
        print ("This is IPCamera:\n")
        print (camera_row)
    else:
        # Get the non-IP camera with the given ID.
        cursor.execute('select camera.id, non_ip_camera.snapshot_url '
                       'FROM camera, non_ip_camera '
                       'WHERE camera.id = non_ip_camera.camera_id '
                       'and camera.id = {};'.format(camera_id))
        camera_row = cursor.fetchone()

         
        # Create a NonIPCamera object.
        if camera_row:
            camera = NonIPCamera(camera_row[0], duration, interval, camera_row[1])
            print("This is non-IP camera:\n")
            print(camera_row)
    cursor.close()
    connection.close()

    if not camera:
        print 'There is no camera with the ID {}.'.format(camera_id)
        return None

    return camera
示例#5
0
def get_camera(camera_id):
    """ Get a camera from the database. """

    camera = None

    # Connect to the database, and get the connection cursor
    connection = MySQLdb.connect(DB_SERVER, DB_USER_NAME, DB_PASSWORD, DB_NAME)
    cursor = connection.cursor()

    # Get the IP camera with the given ID.
    cursor.execute('SELECT camera.id, ip_camera.ip, ip_camera.port, '
                   'ip_camera_model.image_path, ip_camera_model.video_path '
                   'FROM camera, ip_camera, ip_camera_model '
                   'WHERE camera.id = ip_camera.camera_id '
                   'and ip_camera.ip_camera_model_id = ip_camera_model.id '
                   'and camera.id = {};'.format(camera_id))
    camera_row = cursor.fetchone()

    # Create an IPCamera object.
    if camera_row:
        camera = IPCamera(camera_row[0], camera_row[1], camera_row[3],
                          camera_row[4], camera_row[2])
    else:
        # Get the non-IP camera with the given ID.
        cursor.execute('select camera.id, non_ip_camera.snapshot_url '
                       'FROM camera, non_ip_camera '
                       'WHERE camera.id = non_ip_camera.camera_id '
                       'and camera.id = {};'.format(camera_id))
        camera_row = cursor.fetchone()

        # Create a NonIPCamera object.
        if camera_row:
            camera = NonIPCamera(camera_row[0], camera_row[1])

    cursor.close()
    connection.close()

    if not camera:
        print 'There is no camera with the ID {}.'.format(camera_id)
        return None

    return camera
示例#6
0
def get_camera_db(camera_id, duration, interval):
    """ Get a camera from the database. 
    
    **Parameters:** 
    
    camera_id : str
    	The camera from the database's ID. 
    	
    duration : int 
    	The duration of downloading the integer in seconds.  
    	
    interval : int 
    	The interval between two successive snapshots. 
       
        
    **Returns:** 
    
    camera : camera object
    	The camera from which snapshots will be taken. 
    
    
    **Raises:** 
    
    Exceptions : 
    	If there is an error downloading MYSQLdb. 
     
    """
    try:
        import MySQLdb
    except Exception as e:
        raise(e)

    # The server database credentials.
    DB_SERVER = 'localhost'
    DB_USER_NAME = 'root'
    DB_PASSWORD = ''
    DB_NAME = ''

    camera = None

    # Connect to the database, and get the connection cursor
    connection = MySQLdb.connect(DB_SERVER, DB_USER_NAME, DB_PASSWORD, DB_NAME)
    cursor = connection.cursor()

    # Get the IP camera with the given ID.
    cursor.execute('SELECT camera.id, ip_camera.ip, ip_camera.port, '
                   'ip_camera_model.image_path, ip_camera_model.video_path '
                   'FROM camera, ip_camera, ip_camera_model '
                   'WHERE camera.id = ip_camera.camera_id '
                   'and ip_camera.ip_camera_model_id = ip_camera_model.id '
                   'and camera.id = {};'.format(camera_id))
    camera_row = cursor.fetchone()

    # Create an IPCamera object.
    if camera_row:
        camera = IPCamera(camera_row[0], duration, interval, camera_row[1], camera_row[3],
                          camera_row[4], camera_row[2]) #1 is ip and 3 is image path
    else:
        # Get the non-IP camera with the given ID.
        cursor.execute('select camera.id, non_ip_camera.snapshot_url '
                       'FROM camera, non_ip_camera '
                       'WHERE camera.id = non_ip_camera.camera_id '
                       'and camera.id = {};'.format(camera_id))
        camera_row = cursor.fetchone()

         
        # Create a NonIPCamera object.
        if camera_row:
            camera = NonIPCamera(camera_row[0], duration, interval, camera_row[1])
        else:
            # Get the stream camera with the given ID.
            cursor.execute('select camera.id, camera.m3u8_key '
                           'FROM camera, stream_camera '
                           'WHERE camera.id = stream_camera.camera_id '
                           'and camera.id = {};'.format(camera_id))
            camera_row = cursor.fetchone()
            # Create a stream camera object.
            if camera_row:
                camera = StreamCamera(camera_row[0],duration, interval, camera_row[1])

    cursor.close()
    connection.close()

    if not camera:
        print 'There is no camera with the ID {}.'.format(camera_id)
        return None

    return camera