-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlauncher.py
147 lines (128 loc) · 5.26 KB
/
launcher.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# Main Script to launch the app
# Includes: Scheduler, Primary Logics etc.
# import libraries
import os
import time
import random
import logging
import datetime
from instagrapi import Client
from tinydb import TinyDB, Query
# import functions
from functions import validate_folder
from functions import list_files
from functions import list_folders
# config
ACCOUNT_USERNAME = "jpl_tester"
ACCOUNT_PASSWORD = "fJjC7cU65ftk372E"
MEDIA_FOLDER = "media"
DB_FILE = "db/db.json"
CRED_DB_NAME = "insta_db"
UPLOAD_DB_NAME = "upload_db"
# create log folder if not exists
if not os.path.exists("log"):
os.makedirs("log")
if not os.path.exists("db"):
os.makedirs("db")
# create log file if not exists
logging.basicConfig(filename='log/app_primary.log',
filemode='a',
format='%(asctime)s,%(msecs)d %(name)s %(levelname)s %(message)s',
datefmt='%H:%M:%S',
level=logging.INFO)
logging.info("")
logging.info("")
logging.info("Launching app...")
print("Launching app...")
# login to instagram
logging.info("Attempt login to instagram")
print("Attempt login to instagram")
cl = Client()
# cl.login(ACCOUNT_USERNAME, ACCOUNT_PASSWORD)
# login validation
# user_id = cl.user_id_from_username("jpl_tester")
# medias = cl.user_medias(user_id, 20)
# logging.info("Fetched Account Info for user: " + str(cl.user_info(user_id)))
# validate media folder, check if it's empty
subf = validate_folder(MEDIA_FOLDER)
logging.info("Folder /" + MEDIA_FOLDER + " is not empty. Starting upload process with " + str(subf) + " subfolder(s).")
print("Folder /" + MEDIA_FOLDER + " is not empty. Starting upload process with " + str(subf) + " subfolder(s).")
# load folder structure
root_list = list_folders(MEDIA_FOLDER)[0]
# load db
db = TinyDB(DB_FILE)
cred_db = db.table(CRED_DB_NAME)
upload_db = db.table(UPLOAD_DB_NAME)
# enter loop
while True:
# check if there are more folders than already uploaded
folders = []
for items in upload_db.all():
folders.append(items['folder'])
num_values = len(set(folders))
if (len(root_list) <= num_values):
logging.info("All folders have been uploaded. Exiting app.")
print("All folders have been uploaded. Exiting app.")
break
while True:
# randomly choose a folder that has not been uploaded
curr_root = random.choice(root_list)
# check if folder was previously uploaded
if (len(upload_db.search(Query().folder == curr_root)) > 0):
logging.info("Folder " + curr_root + " has been uploaded. Skip this folder.")
print("Folder " + curr_root + " has been uploaded. Skip this folder.")
continue
else:
break
logging.info("Randomly choose folder: " + curr_root)
print("Randomly choose folder: " + curr_root)
# check files in subfolder
subf_list = list_files(MEDIA_FOLDER + "/" + curr_root)
logging.info("Found " + str(len(subf_list)) + " files in folder: " + curr_root)
print("Found " + str(len(subf_list)) + " files in folder: " + curr_root)
valid_files = []
# check if filetypes are .mp4 or .jpg
for subf in subf_list:
if ((os.path.splitext(subf)[1] == ".mp4") | (os.path.splitext(subf)[1] == ".jpg")):
logging.info("Found file: " + subf)
print("Found file: " + subf)
valid_files.append(subf)
# if there are valid files, start uploading process
if (len(valid_files) > 0):
i = 0
# check if upload media is album or single media
if (len(valid_files) > 1):
logging.info("Uploading album")
print("Uploading album")
for file in valid_files:
i+=1
print("Uploading " + str(len(valid_files)) + " files in folder: " + curr_root + " (" + str(i) + "/" + str(len(valid_files)) + ")")
logging.info("Uploading " + str(len(valid_files)) + " files in folder: " + curr_root)
# cl.album_upload(valid_files, "Album from Python")
else:
file = valid_files[0]
# upload single file to instagram
if (os.path.splitext(file)[1] == ".mp4"):
# upload video
logging.info("Uploading video: " + file)
print("Uploading video: " + file)
# cl.video_upload(file, "Video from Python")
elif (os.path.splitext(file)[1] == ".jpg"):
# upload photo
logging.info("Uploading photo: " + file)
print("Uploading photo: " + file)
# cl.photo_upload(file, "Photo from Python")
else:
logging.info("File type not supported: " + file)
print("File type not supported: " + file)
continue
# put uploaded file into db
upload_db.insert({'folder': curr_root, 'file': file, 'uploaded_at': str(datetime.datetime.now()), 'account': ACCOUNT_USERNAME})
else:
upload_db.insert({'folder': curr_root, 'file': 'No files found.', 'uploaded_at': 'n/A', 'account': 'n/A'})
# go to sleep for 20 to 45 seconds before next loop
sleep_time = random.randint(20, 45)
logging.info("Sleeping for " + str(sleep_time) + " seconds before next loop.")
print("Sleeping for " + str(sleep_time) + " seconds before next loop.")
time.sleep(sleep_time)
# end of script