Beispiel #1
0
def single_photo_handler(nid):
    """
    This function accepts a node ID and creates the medium and small size pictures for the photo associated with this
    node.

    :param nid: Node ID for which medium and small picture need to be created.
    :return:
    """
    # Get directory names
    original_dirname = app.config["ORIGINAL_FOLDER"]
    medium_dirname = app.config["MEDIUM_FOLDER"]
    small_dirname = app.config["SMALL_FOLDER"]
    # Connect to pcloud and get directory structure
    pcloud = pcloud_handler.PcloudHandler()
    public_cloud_id = pcloud.get_public_cloud_id()
    # Get folders from Public Folder
    subdirs, _ = pcloud.folder_contents(public_cloud_id)
    original_folderid = subdirs[original_dirname[:-1]]["folderid"]
    medium_folderid = subdirs[medium_dirname[:-1]]["folderid"]
    small_folderid = subdirs[small_dirname[:-1]]["folderid"]
    file = ds.get_file_from_nid(nid)
    _, files = pcloud.folder_contents(original_folderid)
    filedata = files[file]
    # Get file contents and convert to an image - also required to get exif for date and time of picture taken.
    content = pcloud.get_content(filedata)
    app.logger.debug("File {} length: {} (expected: {})".format(file, len(content), filedata["size"]))
    parser = ImageFile.Parser()
    parser.feed(content)
    img = parser.close()
    # Get exif information from picture
    exif = get_labeled_exif(file, img)
    # Create medium image
    medium_img = to_medium(img)
    if isinstance(exif, dict):
        try:
            medium_img = rotate_image(medium_img, exif["Orientation"])
        except KeyError:
            app.logger.info("{} no Orientation in exif data".format(file))
    medium_ffn = os.path.join(os.getenv('LOGDIR'), file)
    medium_img.save(medium_ffn)
    res = pcloud.upload_file(file, medium_ffn, medium_folderid)
    app.logger.info("File {} medium format loaded, result: {}".format(file, res["result"]))
    os.remove(medium_ffn)
    # Create small image
    small_img = to_small(medium_img)
    small_ffn = os.path.join(os.getenv('LOGDIR'), file)
    small_img.save(small_ffn)
    res = pcloud.upload_file(file, small_ffn, small_folderid)
    app.logger.info("File {} small format loaded, result: {}".format(file, res["result"]))
    os.remove(small_ffn)
    pcloud.close_connection()
    return
Beispiel #2
0
import logging
import os
from tuin.lib import my_env, pcloud_handler


from_dirname = "original"
to_dirname = "tempOrigin"

my_env.init_env("tuin", __file__)

# Get filenames for files to be converted
fd = os.getenv('LOGDIR')
fn = 'recent_img'
fln = os.path.join(fd, '{}.csv'.format(fn))

pcloud = pcloud_handler.PcloudHandler()
public_cloud_id = os.getenv("PCLOUD_PUBLIC_ID")
subdirs, files = pcloud.folder_contents(public_cloud_id)
print(subdirs)
from_dirid = subdirs[from_dirname]["folderid"]
to_dirid = subdirs[to_dirname]["folderid"]
_, from_files = pcloud.folder_contents(from_dirid)
with open(fln, 'r', newline='') as csvfile:
    reader = csv.DictReader(csvfile)
    for row in reader:
        file = row["filename"]
        file_id = from_files[file]["fileid"]
        print("Copy file {} with ID {} to folder with ID {}".format(file, file_id, to_dirid))
        pcloud.copyfile(file_id, to_dirid)
pcloud.logout()
Beispiel #3
0
def photo_handler():
    """
    Main function for photo handling.

    :return:
    """
    # Get directory names
    source_dirname = app.config.get("SOURCE_FOLDER")
    original_dirname = app.config["ORIGINAL_FOLDER"]
    medium_dirname = app.config["MEDIUM_FOLDER"]
    small_dirname = app.config["SMALL_FOLDER"]
    # Connect to pcloud and get directory structure
    pcloud = pcloud_handler.PcloudHandler()
    public_cloud_id = pcloud.get_public_cloud_id()
    # Get folders from Public Folder
    subdirs, _ = pcloud.folder_contents(public_cloud_id)
    # Directory names to folder IDs - remove trailing slashes from directory names
    if source_dirname:
        source_folderid = subdirs[source_dirname[:-1]]["folderid"]
    else:
        source_folderid = public_cloud_id
    original_folderid = subdirs[original_dirname[:-1]]["folderid"]
    medium_folderid = subdirs[medium_dirname[:-1]]["folderid"]
    small_folderid = subdirs[small_dirname[:-1]]["folderid"]
    # Collect files from source directory
    _, files = pcloud.folder_contents(source_folderid)
    # Only handle accepted file types
    accepted_types = [".JPG", ".jpg"]
    files = [files[file] for file in files if Path(file).suffix in accepted_types]
    for filedata in files:
        file = filedata["name"]
        fileid = filedata["fileid"]
        app.logger.debug("Working on file {}".format(file))
        # Get file contents and convert to an image - also required to get exif for date and time of picture taken.
        content = pcloud.get_content(filedata)
        app.logger.debug("File {} length: {} (expected: {})".format(file, len(content), filedata["size"]))
        parser = ImageFile.Parser()
        parser.feed(content)
        img = parser.close()
        # Get exif information from picture
        exif = get_labeled_exif(file, img)
        app.logger.debug("EXIF: {}".format(exif))
        # Calculate new filename including date/time picture taken
        created_dt = get_created_datetime(filedata, exif)
        fn = get_filename(file, created_dt)
        create_node(fn, file, created_dt)
        # Move file to Original directory
        pcloud.movefile(fileid, original_folderid, fn)
        # Create medium image
        medium_img = to_medium(img)
        if isinstance(exif, dict):
            try:
                medium_img = rotate_image(medium_img, exif["Orientation"])
            except KeyError:
                app.logger.info("{} ({}) no Orientation in exif data".format(file, fn))
        medium_ffn = os.path.join(os.getenv('LOGDIR'), fn)
        medium_img.save(medium_ffn)
        res = pcloud.upload_file(fn, medium_ffn, medium_folderid)
        app.logger.info("File {} medium format loaded, result: {}".format(fn, res["result"]))
        os.remove(medium_ffn)
        # Create small image
        small_img = to_small(medium_img)
        small_ffn = os.path.join(os.getenv('LOGDIR'), fn)
        small_img.save(small_ffn)
        res = pcloud.upload_file(fn, small_ffn, small_folderid)
        app.logger.info("File {} small format loaded, result: {}".format(fn, res["result"]))
        os.remove(small_ffn)
    pcloud.close_connection()
    nr_files = len(files)
    app.logger.info("{} pictures have been processed.".format(nr_files))
    return nr_files
Beispiel #4
0
 def setUp(self):
     # Initialize Environment
     self.app = create_app()
     self.app_ctx = self.app.app_context()
     self.app_ctx.push()
     self.pcloud = pcloud_handler.PcloudHandler()