-
Notifications
You must be signed in to change notification settings - Fork 0
/
start_posting.py
59 lines (46 loc) · 1.51 KB
/
start_posting.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
52
53
54
55
56
57
58
59
import glob
import os
import sys
import time
from io import open
sys.path.append(os.path.join(sys.path[0], "../../"))
from instabot import Bot # noqa: E402
posted_pic_list = []
try:
with open("posted.txt", "r", encoding="utf8") as f:
posted_pic_list = f.read().splitlines()
except Exception:
posted_pic_list = []
timeout = 24 * 60 * 60 # pics will be posted every 24 hours
bot = Bot()
bot.login()
while True:
folder_path = "./pics"
pics = glob.glob(folder_path + "/*.jpg")
pics = sorted(pics)
try:
for pic in pics:
if pic in posted_pic_list:
continue
pic_name = pic[:-4].split("-")
pic_name = "-".join(pic_name[1:])
print("upload: " + pic_name)
description_file = folder_path + "/" + pic_name + ".txt"
if os.path.isfile(description_file):
with open(description_file, "r") as file:
caption = file.read()
else:
caption = pic_name.replace("-", " ")
bot.upload_photo(pic, caption=caption)
if bot.api.last_response.status_code != 200:
print(bot.api.last_response)
# snd msg
break
if pic not in posted_pic_list:
posted_pic_list.append(pic)
with open("posted.txt", "a", encoding="utf8") as f:
f.write(pic + "\n")
time.sleep(timeout)
except Exception as e:
print(str(e))
time.sleep(60)