Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added a ui #9

Merged
merged 1 commit into from
May 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 133 additions & 0 deletions app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
#!/usr/bin/env Python3
import PySimpleGUI as sg
import settings
settings.init()
import ninegag, glob, insta, os, subprocess, queue, threading



def long_operation_thread(gui_queue):
insta.launch_inst()
ninegag.loop_posting()
gui_queue.put('** Done **') # put a message into queue for GUI


# ------------------------------- UI -------------------------------
def the_gui():
sg.ChangeLookAndFeel('DarkBrown1')
gui_queue = queue.Queue()

col = [
# General options
[sg.Frame(layout=[
[sg.Text('Wait time (seconds)', size=(15, 0)),
sg.Spin(values=[i for i in range(5, 1000)], key="wait_time", initial_value=5, size=(6, 0))],
[sg.Text('The amount of time for an action to be done ex: closing a popup. I recommend a '
'min of 5. If you have slower internet, increase this value.', size=(50, 0), font=("Roboto", 8))],
[sg.Text('_' * 100, font=("Roboto", 4))],
[sg.Text('Next post wait time', size=(15, 1)),
sg.Spin(values=[i for i in range(1, 1000)], key="post_time", initial_value=50, size=(6, 1))],
[sg.Text('Delay between posts. I recommend 50 seconds or more to avoid instagram bot detection',
size=(40, 0), font=("Roboto", 8))],
[sg.Text('_' * 100, font=("Roboto", 4))],
[sg.Text('Number of posts', size=(15, 1)),
sg.Spin(values=[i for i in range(0, 1000)], key="post_limit", initial_value=50, size=(6, 1))],
[sg.Text('Number of posts before program stops', size=(50, 0), font=("Roboto", 8))],
[sg.Text('_' * 100, font=("Roboto", 4))],
[sg.Checkbox('Keep Images', key="keep_images", default=False)],
[sg.Text('Deletes images in folder on exit', size=(50, 0),
font=("Roboto", 8))],

],
title='General Options')], ]
layout = [
# Title

# 9gag Section
[sg.Frame(layout=[
[sg.Checkbox('Hot', key="hot", default=True), sg.Checkbox('Trending', key="trending"),
sg.Checkbox('Funny', key="funny"), sg.Checkbox('Animals', key="animals"),
sg.Checkbox('Anime/Manga', key="anime-manga"), sg.Checkbox('Anime/Waifu ', key="animewaifu")],
[sg.Checkbox('Awesome', key="awesome"), sg.Checkbox('Cosmic & Webtoon', key="comic-webtoon"),
sg.Checkbox('Cosplay', key="cosplay"), sg.Checkbox('Gaming', key="gaming"),
sg.Checkbox('WTF', key="wtf"), sg.Checkbox('Girl', key="girl")],
[sg.Checkbox('Girl/Celeb', key="girlcelebrity"), sg.Checkbox('League of Legends', key="leagueoflegends"),
sg.Checkbox('Meme', key="meme"), sg.Checkbox('NSFW', key="nsfw"),
sg.Checkbox('Politics', key="politics"), sg.Checkbox('Relationship', key="relationship")],
[sg.Checkbox('Savage', key="savage")],
],
title='9gag Options (What section of 9gag do you want your photos to come from?)')],

# Instagram options
[sg.Frame(layout=[
[sg.Text('How will you sign in?', size=(25, 1))],
[sg.InputCombo(('Facebook', 'Username'), default_value="Facebook" ,key="login_type", size=(25, 1))],
[sg.Text('Username/Email', size=(25, 1))],
[sg.InputText('', key="username", size=(25, 1))],
[sg.Text('Password', size=(25, 1))],
[sg.InputText('', key="password", password_char='*',size=(25, 1))],
],
title='Instagram Options'), sg.Column(col)],

# Output window
[sg.Output(size=(80, 10))],

# Buttons
[sg.Button('Run', size=(10, 1)), sg.Button('Exit', size=(10, 1))]
]
window = sg.Window('Made with love', layout, default_element_size=(40, 1), grab_anywhere=False)

while True:
event, values = window.read(timeout=100)
if event in (None, 'Exit'):
#if not values['keep_images']:
# # Removes images from files if left over from previous runs
# files = glob.glob('files/*')
# for f in files:
# os.remove(f)
break
elif event == 'Run':
print("Starting...")
# All option for 9gag categories
ninegagCategories = ["hot", "trending", "funny", "animals", "anime-manga", "animewaifu", "awesome",
"cosmic-webtoon", "cosplay", "gaming", "girl", "girlcelebrity",
"leagueoflegends", "meme", "nsfw", "politics", "relationship", "savage", "wtf"]
# Sends selected categories to setting.py
for key in values:
if key in ninegagCategories and values[key]:
settings.ninegag_categories.append(key)
settings.wait_time = values['wait_time']
settings.post_time = values['post_time']
settings.username = values['username']
settings.password = values['password']
settings.keep_images = values['keep_images']
settings.login_type = values['login_type']
settings.post_limit = values['post_limit']

try:
threading.Thread(target=long_operation_thread,
args=(gui_queue,), daemon=True).start()
except Exception as e:
print('Error')


# Check for incoming messages from threads
try:
message = gui_queue.get_nowait()
except queue.Empty: # get_nowait() will get exception when Queue is empty
message = None # break from the loop if no more messages are queued up

# if message received from queue, display the message in the Window
if message:
print('Got a message back from the thread: ', message)

# if user exits the window, then close the window and exit the GUI func
window.close()


######################################################################


if __name__ == '__main__':
the_gui()
print('Exiting Program')
133 changes: 56 additions & 77 deletions insta.py
Original file line number Diff line number Diff line change
@@ -1,41 +1,55 @@
import autoit, json, os
import autoit, json, os, settings, platform
from time import sleep
from selenium import webdriver

# Variables
username = ""
password = ""
minimizeWindow = False # True or False

# Chrome browser options
mobile_emulation = {"deviceMetrics": {"width": 360, "height": 640, "pixelRatio": 3.0},
"userAgent": "Mozilla/5.0 (Linux; Android 4.2.1; en-us; Nexus 5 Build/JOP40D) AppleWebKit/535.19 "
"(KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19"}
opts = webdriver.ChromeOptions()
opts.add_argument("window-size=1,765")
opts.add_experimental_option("mobileEmulation", mobile_emulation)
driver = webdriver.Chrome(executable_path=r"chromedriver.exe", options=opts) # you must enter the path to your driver

# Opens Instagram
main_url = "https://www.instagram.com"
driver.get(main_url)
sleep(4)

driver = ""



def launch_inst():
global driver
print("Opening instagram")
# Chrome browser options
mobile_emulation = {"deviceMetrics": {"width": 360, "height": 640, "pixelRatio": 3.0},
"userAgent": "Mozilla/5.0 (Linux; Android 4.2.1; en-us; Nexus 5 Build/JOP40D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19"}
opts = webdriver.ChromeOptions()
opts.add_argument("window-size=1,765")
opts.add_experimental_option("mobileEmulation", mobile_emulation)
# Checks if on Mac or Windows
if platform.system() == "Windows":
driver = webdriver.Chrome(executable_path=r"chromedriver.exe", options=opts)
else:
driver = webdriver.Chrome(options=opts)

# Opens Instagram
main_url = "https://www.instagram.com"
driver.get(main_url)
sleep(10)
ordered_functions()

def login():
print("In login")
login_button = driver.find_element_by_xpath("//button[contains(text(),'Log In')]")
login_button.click()
sleep(2)
username_input = driver.find_element_by_xpath("//input[@name='username']")
username_input.send_keys(username)
password_input = driver.find_element_by_xpath("//input[@name='password']")
password_input.send_keys(password)
sleep(1)
password_input.submit()
print("Logging into Instagram")
driver.find_element_by_xpath("//button[contains(text(),'Log In')]").click()
if settings.login_type == "Facebook":
print("Going through Facebook")
driver.find_element_by_xpath("//button[contains(text(),'Continue with Facebook')]").click()
sleep(settings.wait_time)
driver.find_element_by_xpath("//input[@name='email']").send_keys(settings.username)
driver.find_element_by_xpath("//input[@name='pass']").send_keys(settings.password)
sleep(settings.wait_time)
driver.find_element_by_xpath("//button[@name='login']").click()
else:
sleep(settings.wait_time)
driver.find_element_by_xpath("//input[@name='username']").send_keys(settings.username)
driver.find_element_by_xpath("//input[@name='password']").send_keys(settings.password)
sleep(settings.wait_time)
driver.find_element_by_xpath("//button[@type='submit']").click()


def remove_popups():
print("Removing any popups")
try:
driver.find_element_by_xpath("//a[contains(text(),'Not Now')]").click()
except:
Expand All @@ -48,45 +62,18 @@ def remove_popups():
pass


def close_save_info():
print("Close Save Info")
try:
driver.find_element_by_xpath("//a[contains(text(),'Not Now')]").click()
except:
pass


def close_get_notification():
print("Close get notification")
try:
driver.find_element_by_xpath("//button[contains(text(),'Not Now')]").click()
except:
pass


def close_add_to_home():
try:
print("close add to home")
driver.find_element_by_xpath("//button[contains(text(),'Cancel')]").click()
except:
print("passed")
pass


def add_post():
try:
print("add post")
driver.find_element_by_xpath("//div[@role='menuitem']").click()
except:
print("passed")
pass


def post():
print("Adding post")
# Readies the content for instagram
with open('filesDict.txt', encoding="utf8") as json_file:
data = json.load(json_file)
print("Instagram" + str(data))
if bool(data):
# gets first item in filesDict and sets it as the next instagram upload
image_path = os.getcwd() + "\\files\\" + data['dict'][0]['id'] + ".jpg"
Expand All @@ -101,47 +88,39 @@ def post():
with open('filesDict.txt', 'w', encoding="utf8") as outfile:
json.dump(data, outfile)

print(image_path)
print(caption)

# Opens File Explore window
print("Starting open")
print("Opening file explorer")
autoit.win_active("Open")
print("Starting open Edit 1")
autoit.control_send("Open", "Edit1", image_path)
sleep(0)
autoit.control_set_text("Open", "Edit1", image_path)
autoit.control_send("Open", "Edit1", "{ENTER}")
sleep(2)
sleep(settings.wait_time)

# Clicks through options and adds caption after file is added
try:
driver.find_element_by_xpath("//span[contains(text(),'Expand')]").click()
except:
pass
sleep(5)
sleep(settings.wait_time)
driver.find_element_by_xpath("//button[contains(text(),'Next')]").click()
sleep(5)
sleep(settings.wait_time)
caption_field = driver.find_element_by_xpath("//textarea[@aria-label='Write a caption…']")
caption_field.send_keys(caption)
driver.find_element_by_xpath("//button[contains(text(),'Share')]").click()
os.remove(image_path)
if not settings.keep_images:
os.remove(image_path)


# Executes functions in order
def orderedFunctions():
def ordered_functions():
login()
sleep(5)
remove_popups()
sleep(5)
sleep(settings.wait_time)
remove_popups()
sleep(5)
sleep(settings.wait_time)
remove_popups()
sleep(5)
sleep(5)
add_post()
sleep(5)
post()
sleep(5)
sleep(settings.wait_time)
remove_popups()
# Minimizes window if variable = True
if minimizeWindow:
driver.minimize_window()
driver.minimize_window()
Loading