def fetch_photos(sheet_url):
    """fill dictionaries and compress images"""
    photo_df = fetch_sheet.get_google_sheet(sheet_url,
                                            'Photo')  # fetch image sheet
    if ('ImageDir' in photo_df.columns
            and 'Context/Caption' in photo_df.columns
            and 'Photographer' in photo_df.columns
            and 'Section' in photo_df.columns):
        paths = photo_df['ImageDir'].values
        captions = photo_df['Context/Caption'].values
        credits = photo_df['Photographer'].values
        sections = photo_df['Section'].values

        if (not (len(paths) == len(captions)
                 and len(captions) == len(credits))):
            print('error: photo budget columns not the same length')
            exit(0)

        for i in range(len(paths)):
            if (paths[i] == NOPHOTO or captions[i] == '' or credits[i] == ''
                    or sections[i] == ''):  # skip empty fields
                continue
            paths[i] = paths[i].strip()

            # fill in the dictionaries
            photo_caption[paths[i]] = captions[i]

            credit = ''
            for special_case in special_photo_credits:
                if (special_case.lower() in credits[i].lower()
                    ):  # if it's a special case, use raw credits[i]
                    credit = credits[i]
                    break
            if (credit == ''):
                credit = credits[i][0] + '.' + credits[i].split(
                    ' ')[1] + '/The Phillipian'

            photo_credit[paths[i]] = credit

            # compress image

            full_path = local_img_path + sections[i].lower() + '/' + paths[i]
            imgs = os.listdir(full_path)
            ind = 0
            while (imgs[ind][0] == '.'
                   ):  # skip hidden directories ('.anything')
                ind += 1
            if (imgs[ind][0] != '0'):  # compress if not already compressed
                print('compressing ' + full_path)
                img = full_path + '/' + imgs[ind]
                img = imgprepare_python_2.compress_img(
                    img, 30)  # TODO: use imgprepare
    else:
        print('error: missing col')
def fetch_illustrations(sheet_url):
    illus_df = fetch_sheet.get_google_sheet(
        sheet_url, 'Illustrations')  #fetch illustration sheet
    if ('ImageDir' in illus_df.columns and 'Illustrator' in illus_df.columns
            and 'Section' in illus_df.columns):
        paths = illus_df['ImageDir'].values
        credits = illus_df['Illustrator'].values
        sections = illus_df['Section'].values

        if (not (len(paths) == len(credits))
                or not (len(paths) == len(sections))):
            print('error: illustration budget columns not the same length')
            exit(0)

        # fill dictionaries
        for i in range(len(paths)):
            if (paths[i] == ''):
                print('missing imagedir')
                continue
            if (credits[i] == ''):
                print('missing credit')
                continue
            if paths[i] == NOPHOTO:
                print('no photo')
                continue

            credit = ''
            if (credit == ''):
                credit = credits[i][0] + '.' + credits[i].split(
                    ' ')[1] + '/The Phillipian'
            illus_credit[paths[i]] = credit

            # compress image
            full_path = local_img_path + sections[i].lower() + '/' + paths[i]
            imgs = os.listdir(full_path)
            ind = 0
            while (imgs[ind][0] == '.'
                   ):  # skip hidden directories ('.anything')
                ind += 1
            if imgs[ind][0] != '0':
                print('compressing ' + full_path)
                img = full_path + '/' + imgs[ind]
                img = imgprepare_python_2.compress_img(
                    img, 30)  # TODO: use imgprepare
    else:
        print('error: missing col')
        print(illus_df.columns)
def fetch_photos(sheet_url):
    """fill dictionaries and compress images"""
    photo_df = fetch_sheet.get_google_sheet(sheet_url, 'Photo') # fetch image sheet
    if ('ImageDir' in photo_df.columns and 'Photographer' in photo_df.columns and 'Section' in photo_df.columns):
        paths = photo_df['ImageDir'].values
        captions = photo_df['Context/Caption'].values
        credits = photo_df['Photographer'].values
        sections_col = photo_df['Section'].values

        assert len(paths) == len(captions), 'error: photo budget columns not the same length'
        assert len(captions) == len(credits), 'error: photo budget columns not the same length'

        for i in range(len(paths)):
            if (paths[i].lower() == 'nophoto' or paths[i] == '' or sections_col[i] == '' or sections_col[i] not in [s.lower() for s in sections]): # skip empty fields
                continue

            paths[i] = paths[i].strip()


            # fill in the dictionaries
            photo_caption[paths[i]] = captions[i]

            credit = ''
            for special_case in special_photo_credits:
                if (special_case.lower() in credits[i].lower()): # if it's a special case, use raw credits[i]
                    credit = credits[i]
                    break
            if (credit == ''):
                credit = credits[i]+'/The Phillipian'

            photo_credit[paths[i]] = credit

            # compress image
            full_path = os.path.join(local_img_path+sections_col[i].lower(), paths[i].lower())
            imgs = os.listdir(full_path)

            for img in imgs:
                if img[0] == '.': # skip hidden directories ('.anything')
                    continue 
                if (img.split('_')[0] != 'Compressed'): # compress if not already compressed
                    print('compressing '+full_path)
                    img = os.path.join(full_path, img)
                    img = imgprepare_python_2.compress_img(img, 70) # TODO: use imgprepare
    else:
        print('error: missing col')
def fetch_graphics(sheet_url):
    graphics_df = fetch_sheet.get_google_sheet(sheet_url, 'Graphic') #fetch illustration sheet
    if ('ImageDir' in graphics_df.columns and 'Designer' in graphics_df.columns and 'Section' in graphics_df.columns):
        paths = graphics_df['ImageDir'].values
        credits = graphics_df['Designer'].values
        sections = graphics_df['Section'].values
        do_upload = graphics_df['Upload?'].values

        assert len(paths) == len(credits), 'error: graphic budget columns not the same length'
        assert len(paths) == len(sections), 'error: graphic budget columns not the same length'

        # fill dictionaries
        for i in range(len(paths)):
            if (paths[i] == ''):
                print('missing imagedir')
                continue
            if(credits[i] == ''):
                print('missing credit')
                continue
            if paths[i] == NOPHOTO:
                print('no photo')
                continue
            

            credit = ''
            if (credit == ''):
                credit = credits[i]+'/The Phillipian'
            graphics_credit[paths[i]] = credit
            # compress image
            full_path = local_img_path+sections[i].lower()+'/'+paths[i]
            imgs = os.listdir(full_path)
            ind = 0
            while (imgs[ind][0] == '.'): # skip hidden directories ('.anything')
                ind += 1
            if imgs[ind][0] != '0':
                print('compressing '+full_path)
                img = full_path+'/'+imgs[ind]
                img = imgprepare_python_2.compress_img(img, 70) # TODO: use imgprepare
    else:
        print('error: missing col')
        print(graphics_df.columns)
    else:
        print('error: missing col')
        print(illus_df.columns)


fetch_photos(sheet_url)
#fetch_illustrations(sheet_url)
#fetch_graphics(sheet_url)
# COPY PHOTOS OVER TO SERVER
copy_photos_to_server() # TODO: uncomment after done testing

# FETCH ARTICLES
for s in sections:
    print('starting section:\t' + s)
    # fetch and verify sheet dataframe content
    section_df = fetch_sheet.get_google_sheet(sheet_url, s)
    languages = None
    img_names = None
    translators = None
    featured_posts = None
    if s == 'multilingual':
        helper.check_columns(section_df, ['Link','Translator','Translated Headline','Writer','Language','Upload'])
        headlines = section_df['Translated Headline'].values
        translators = section_df['Translator'].values
        languages = section_df['Language'].values
    if s == 'sports':
        helper.check_columns(section_df, ['Subcategory'])
        categories = section_df['Subcategory'].values
    
    else:
        helper.check_columns(section_df, ['Link','ImageDir','Headline','Writer','Featured','ready for autoupload', 'uploaded online', 'TAGS'])