forked from asimejaz14/Instagram-Post-Uploader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.py
51 lines (43 loc) · 1.42 KB
/
script.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import json
import os
import requests
from PIL import Image
from instabot import Bot
from resizeimage import resizeimage
import random
import config
data = ''
# replace eg.json with your own file name
with open('eg.json', encoding='utf-8') as file:
data = file.read()
json_data = json.loads(data)
bot = Bot()
bot.login(username=config.USERNAME,
password=config.PASSWORD)
for j_data in json_data:
caption = j_data['caption']
for image_path in (j_data['images']):
print("Caption:", caption)
print(image_path)
response = requests.get(image_path)
filename = str(random.randint(0,10000)) + '.jpg'
file = open(filename, "wb")
file.write(response.content)
file.close()
fd_img = open(filename, 'rb')
img = Image.open(fd_img)
width, height = img.size
# If image width is greater than 1080, just upload it
if(width > 1080):
img = resizeimage.resize_crop(img, [width, width])
img.save(filename, img.format)
fd_img.close()
#If image width is not equal to height crop image to match width x width(as height)
if(width != height):
size = width, width
img = resizeimage.resize_crop(img, size)
img.save(filename, img.format)
fd_img.close()
bot.upload_photo(filename, caption=caption)
os.remove(filename+".REMOVE_ME")
print()