Created
March 24, 2020 11:02
-
-
Save ladyrassilon/7a30832231b9512dc46ff11bbc319494 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import os\n", | |
"import PIL\n", | |
"import exif\n", | |
"import requests\n", | |
"import dropbox\n", | |
"import time\n", | |
"import datetime\n", | |
"import dateutil\n", | |
"import uuid\n", | |
"import random" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"image_path = \"/Users/rassilon/Documents/Send To Yoink\"\n", | |
"jpegs = [os.path.join(image_path, file_name) for file_name in os.listdir(image_path) if file_name.endswith(\".jpeg\") or file_name.endswith(\".jpg\")]\n", | |
"jpegs.sort()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"dropbox_access_token = \"PUT IN YOUR DROPBOX ACCESS TOKEN\"\n", | |
"dbx = dropbox.Dropbox(dropbox_access_token)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"buffer_access_token = \"PUT IN YOUR BUFFER ACCESS TOKEN\"\n", | |
"auth_dict = {\"access_token\": buffer_access_token}" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"api_base_url = \"https://api.bufferapp.com/1/\"\n", | |
"profiles_url = f\"{api_base_url}profiles.json\"" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"profile_data = requests.get(profiles_url, {\"access_token\": buffer_access_token}).json()\n", | |
"profile_id = profile_data[0][\"_id\"]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"pending_url = f\"{api_base_url}profiles/{profile_id}/updates/pending.json\"" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"pending_posts = requests.get(pending_url, auth_dict).json()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"{'total': 0, 'updates': []}" | |
] | |
}, | |
"execution_count": 9, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"pending_posts" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 10, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"update_create_url = f\"{api_base_url}updates/create.json\"" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 11, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def get_tags(image_data):\n", | |
" tags = []\n", | |
" def set_aperture(tags, image_data):\n", | |
" if hasattr(image_data, \"f_number\"):\n", | |
" f_number = image_data.f_number\n", | |
" if f_number != int(f_number):\n", | |
" above, below = str(f_number).split(\".\")\n", | |
" aperture_tag = f\"F{above}_{below}\"\n", | |
" else:\n", | |
" aperture = int(f_number)\n", | |
" aperture_tag = f\"F{aperture}\"\n", | |
" tags.append(aperture_tag)\n", | |
" set_aperture(tags=tags, image_data=image_data)\n", | |
" def set_focal_length(tags, image_data):\n", | |
" if hasattr(image_data, \"focal_length\"):\n", | |
" actual_focal_length = f\"{image_data.focal_length}mm\".replace(\".0\", \"\").replace(\".\",\"_\")\n", | |
" tags.append(actual_focal_length)\n", | |
" if hasattr(image_data, \"focal_length_in_35mm_film\"):\n", | |
" equivalent_focal_length = f\"{int(image_data.focal_length_in_35mm_film)}mm\".replace(\".0\", \"\").replace(\".\",\"_\")\n", | |
" tags.append(equivalent_focal_length)\n", | |
" set_focal_length(tags=tags, image_data=image_data)\n", | |
" camera_tag_map = {\n", | |
" \"DSC-RX100\": [\"sony\", \"rx100\", \"sonyrx100\", \"rx100sony\", \"compact\", \"1inchsensor\"],\n", | |
" \"ILCE-6000\": [\"sonya6000\", \"a6000photography\", \"a6000sony\", \"sony\", \"a6000\", \"mirrorless\", \"emount\"],\n", | |
" \"ILCE-6400\": [\"sonya6400\", \"a6400photography\", \"a6400sony\", \"sony\", \"a6400\", \"mirrorless\", \"emount\"],\n", | |
" \"iPhone XS\": [\"iphone\", \"iphonexs\", \"iphonephotography\", \"shotoniphone\", \"iphoneori\", \"smartphone\", \"smartphonephotography\", \"ios\"]\n", | |
" }\n", | |
" def set_camera_tags(tags, image_data):\n", | |
" camera_tags = camera_tag_map[image_data.model]\n", | |
" tags.extend(camera_tags)\n", | |
" set_camera_tags(tags=tags, image_data=image_data)\n", | |
" lens_tag_map = {\n", | |
" \"28-100mm F1.8-4.9\": [\"integratedlens\", \"28100mm\", \"variableaperture\", \"zeisslens\", \"compactlens\"],\n", | |
" \"E 55-210mm F4.5-6.3 OSS\": [\"telephoto\", \"sonylens\", \"zoomlens\", \"55210mm\", \"variableaperture\", \"OSS\"],\n", | |
" \"E PZ 18-105mm F4 G OSS\": [\"telephoto\", \"sonylens\", \"zoomlens\", \"18105mm\", \"18_105mm\", \"fixedaperture\", \"OSS\"],\n", | |
" \"----\": [\"wideanglelens\", \"wideangle\", \"ultrawide\", \"wideanglephotography\", \"noautofocus\", \"wideanglephoto\", \"samyang\", \"samyang12mm\", \"12mm\", \"rokinon\", \"rokinon12mm\", \"manualfocus\", \"samyanglens\", \"rokinonlens\"],\n", | |
" \"E 50mm F1.8 OSS\": [\"portraitlens\", \"sonylens\", \"primelens\", \"OSS\", \"50mmprime\", \"50mmlens\"],\n", | |
" \"30mm F1.4 DC DN | Contemporary 016\": [\"sigmalens\", \"primelens\", \"30mmprime\", \"sigmacontemporary\", \"fixedaperture\", \"standardlens\"],\n", | |
" \"E 16mm F2.8\": [\"sonylens\", \"primelens\", \"16mmprime\", \"16mmlens\", \"pancakelens\", \"wideangle\", \"wideanglelens\", \"wideanglephoto\", \"wideanglephotography\"],\n", | |
" \"E PZ 16-50mm F3.5-5.6 OSS\": [\"sonylens\", \"kitlens\", \"shortzoom\", \"zoomlens\", \"variableaperture\", \"1650mm\", \"16_50mm\"],\n", | |
" \"iPhone XS front camera 2.87mm f/2.2\": [\"selfielens\", \"frontfacing\", \"frontfacingcamera\"],\n", | |
" \"iPhone XS back dual camera 6mm f/2.4\": [\"iphone\", \"iphone2x\", \"iphonezoom\", \"iphone\"],\n", | |
" \"iPhone XS back dual camera 4.25mm f/1.8\": [\"iphonexs\", \"iphone1x\", \"iphonewideangle\", \"iphonenormal\"],\n", | |
" \"iPhone XS front TrueDepth camera 2.87mm f/2.2\": [\"selfielens\", \"portraitmode\", \"frontfacing\", \"frontfacingcamera\"],\n", | |
" }\n", | |
" lens_tag_map[\"Sony E PZ 18-105mm F4.0 OSS\"] = lens_tag_map[\"E PZ 18-105mm F4 G OSS\"]\n", | |
" def set_lens_tags(tags, image_data):\n", | |
" lens_tags = lens_tag_map[image_data.lens_model]\n", | |
" tags.extend(lens_tags)\n", | |
" set_lens_tags(tags=tags, image_data=image_data)\n", | |
" def set_exposure_tags(tags=tags, image_data=image_data):\n", | |
" if image_data.exposure_time >= 1:\n", | |
" exposure_value = str(image_data.exposure_time).replace(\".\", \"point\")\n", | |
" exposure_tag = f\"{exposure_value}s\"\n", | |
" else:\n", | |
" exposure_value = int(1/image_data.exposure_time)\n", | |
" exposure_tag = f\"1_{exposure_value}s\"\n", | |
" tags.append(exposure_tag)\n", | |
" set_exposure_tags(tags=tags, image_data=image_data)\n", | |
" return list(set(tags))\n", | |
"starting_timecode = datetime.datetime.now(dateutil.tz.gettz())\n", | |
"\n", | |
"def get_random_delta():\n", | |
" minutes = random.randint(90, 60*4)\n", | |
" seconds = random.randint(0, 59)\n", | |
" total_seconds = seconds + ( minutes * 60 )\n", | |
" delta = datetime.timedelta(seconds=total_seconds)\n", | |
" return delta\n", | |
"\n", | |
"def get_next_timestamp(current_timecode):\n", | |
" while True:\n", | |
" current_timecode += get_random_delta()\n", | |
" while (current_timecode.hour < 8) or (current_timecode.hour > 22):\n", | |
" current_timecode += get_random_delta()\n", | |
" yield current_timecode.isoformat()\n", | |
"next_timestamp = get_next_timestamp(current_timecode=starting_timecode)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 12, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Uploading /Users/rassilon/Documents/Send To Yoink/0.jpg\n", | |
"Uploading /Users/rassilon/Documents/Send To Yoink/1.jpg\n", | |
"Uploading /Users/rassilon/Documents/Send To Yoink/2.jpg\n", | |
"Uploading /Users/rassilon/Documents/Send To Yoink/3.jpg\n", | |
"Uploading /Users/rassilon/Documents/Send To Yoink/4.jpg\n", | |
"Uploading /Users/rassilon/Documents/Send To Yoink/5.jpg\n", | |
"Uploading /Users/rassilon/Documents/Send To Yoink/6.jpg\n", | |
"Uploading /Users/rassilon/Documents/Send To Yoink/7.jpg\n" | |
] | |
} | |
], | |
"source": [ | |
"to_delete = []\n", | |
"for jpeg_path in jpegs:\n", | |
" print(f\"Uploading {jpeg_path}\")\n", | |
" file_name = str(uuid.uuid4())\n", | |
" uploaded_file_name = f\"{file_name}.jpg\"\n", | |
" with open(jpeg_path, \"rb\") as image_file:\n", | |
" image_file = image_file.read()\n", | |
" file_metadata = dbx.files_upload(image_file, f\"/{uploaded_file_name}\")\n", | |
" with open(jpeg_path, \"rb\") as image_file:\n", | |
" image_data = exif.Image(image_file)\n", | |
" raw_tags = get_tags(image_data)\n", | |
" hashtags = [f\"#{tag}\" for tag in raw_tags]\n", | |
" text = \" \".join(hashtags)\n", | |
" update_data = {\n", | |
" \"profile_ids\": [profile_id],\n", | |
" \"text\": text,\n", | |
" \"media[photo]\": dbx.files_get_temporary_link(file_metadata.path_display).link,\n", | |
" \"scheduled_at\": next(next_timestamp),\n", | |
" }\n", | |
" update_data.update(auth_dict)\n", | |
" try:\n", | |
" response = requests.post(update_create_url, update_data)\n", | |
" time.sleep(10)\n", | |
" except ConnectionError:\n", | |
" try:\n", | |
" print(\"Trying Again\")\n", | |
" time.sleep(20)\n", | |
" response = requests.post(update_create_url, update_data)\n", | |
" except ConnectionError:\n", | |
" print(\"One More Time\")\n", | |
" time.sleep(30)\n", | |
" response = requests.post(update_create_url, update_data)\n", | |
" \n", | |
" to_delete.append(file_metadata.path_display)\n" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.6.5" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment