Skip to content

Instantly share code, notes, and snippets.

Created September 25, 2016 17:00
Show Gist options
  • Save anonymous/805f23a40855687533e2f154da2c4628 to your computer and use it in GitHub Desktop.
Save anonymous/805f23a40855687533e2f154da2c4628 to your computer and use it in GitHub Desktop.
#!python3
import requests
import openpyxl
import time
import os
link = 'https://www.eyeem.com/data/showcase/upload'
xl_path = '.\ImgAnalysis.xlsx'
def aesthetics(file):
header={
'Accept': '*/*',
'DNT': '1',
'Host': 'www.eyeem.com',
'Origin': 'https://www.eyeem.com',
'Referer': 'https://www.eyeem.com/tech',
'X-Requested-With': 'XMLHttpRequest'
}
session = requests.Session()
resp = session.get('https://www.eyeem.com/tech')
resp.raise_for_status()
time.sleep(3)
if resp.status_code == 200:
cookies = requests.utils.cookiejar_from_dict(
requests.utils.dict_from_cookiejar(session.cookies))
cookie = str(cookies).split()[1]
header.update({
'Cookie': cookie
})
response = session.post(link,
files={"photo": open(file, 'rb')},
headers=header)
# print(response.json())
return (response.json())
def excel_actions(fldr):
# Get the fileCount of all sub directories
FileCount = sum(len(list(f for f in fs if f.lower().endswith('.jpg') or f.lower().endswith('.png'))) for _, _, fs in os.walk(fldr))
if int(FileCount) >= 1:
# Load the workbook from path
# Dont use_iterators=True when read_only=False. File Wont save!
wb = openpyxl.load_workbook(xl_path, read_only=False, data_only=False)
# Open the worksheet from the name
sheet = wb.get_sheet_by_name('Sheet1')
print('Total No. of Files : ' + str(FileCount))
rowNum = 0
for root, dirs, files in os.walk(fldr):
for imgFile in files:
if imgFile.endswith(".jpg") or imgFile.endswith(".png"):
a = aesthetics(os.path.join(root, imgFile))
Score = a['aestheticsScore']
Caption = str(a['caption'])
TagList = list(a['concepts'])
Tags = ', '.join(str(t) for t in TagList)
# Make it a hyperlink (Easy to open)
filename = ('=HYPERLINK("%s", "%s")' % (os.path.join(root, imgFile), imgFile))
sheet.cell(row=rowNum + 2, column=1).value = filename
sheet.cell(row=rowNum + 2, column=2).value = Score
sheet.cell(row=rowNum + 2, column=3).value = Caption
sheet.cell(row=rowNum + 2, column=4).value = Tags
print(imgFile, ' done.')
rowNum += 1
# Save the workbook.
wb.save(xl_path)
def main(imageFolderPath):
# Check if Excel file is accessible
if os.path.exists(xl_path):
try:
os.rename(xl_path, xl_path)
print(os.path.basename(xl_path) + ' is available!\n')
logging.info(str(xl_path) + ': is available.')
excel_actions(imageFolderPath)
except OSError as e:
print('Error - Accessing the file! File may be Open!\n' + str(e))
else:
print(xl_path, ': is not available.\nPlease check the path/filename.')
if __name__ == '__main__':
main(input('Enter Folder Path to analyze: '))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment