Pers Scratchpad

Pers Scratchpad

#1

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.service import Service
from time import sleep


service = Service(
    "/home/rahul/Desktop/Class/Python/oct_26_2021_daily/scratchpads/chromedriver"
)
options = webdriver.ChromeOptions()

browser = webdriver.Chrome(service=service, options=options)

# YOUTUBE

browser.maximize_window()
browser.get("https://www.youtube.com")
sleep(3)

search_box = browser.find_element_by_xpath(
    "/html/body/ytd-app/div/div/ytd-masthead/div[3]/div[2]/ytd-searchbox/form/div[1]/div[1]/input"
)
search_box.click()
search_box.send_keys("spiderman trailer", Keys.ENTER)
sleep(4)
browser.find_element_by_partial_link_text("SPIDER-MAN: NO WAY HOME").click()
sleep(11)

try:
    browser.find_element_by_class_name("ytp-ad-skip-button").click()
except:
    print("No ads found")

sleep(15)

# WHATS APP

browser.get("https://web.whatsapp.com/")
sleep(12)

search_input = browser.find_element_by_xpath(
    "/html/body/div[1]/div[1]/div[1]/div[3]/div/div[1]/div/label/div/div[2]"
)
search_input.click()
search_input.send_keys("Rahul 2", Keys.ENTER)

i = 0
while i < 10:
    msg_box = browser.find_element_by_xpath(
        "/html/body/div[1]/div[1]/div[1]/div[4]/div[1]/footer/div[1]/div/span[2]/div/div[2]/div[1]/div/div[2]"
    )
    msg_box.click()
    msg_box.send_keys("SEND FROM MY SELENIUM SCRIPT", Keys.ENTER)

    i += 1

    # send_btn = browser.find_element_by_xpath(
    #     "/html/body/div[1]/div[1]/div[1]/div[4]/div[1]/footer/div[1]/div/span[2]/div/div[2]/div[2]/button"
    # )
    # send_btn.click()

sleep(5)

browser.quit()

# -------------
# SQL LITE 3

import sqlite3

data = ("john", "doe", 25)
data_lst = [
    ("jack", "parker", 19),
    ("james", "smith", 32),
    ("jane", "watson", 23),
    ("jane", "watson", 49),
]

conn = sqlite3.connect("users.db")

c = conn.cursor()

c.execute("CREATE TABLE users (first_name TEXT, last_name TEXT, age INTEGER);")

query = """INSERT INTO users VALUES (?,?,?)"""

for item in data_lst:
    print(f"Writing {item[0]} to database...")
    c.execute(query, item)

c.execute("SELECT * FROM users;")
# print(c)

print(c.fetchall())

conn.commit()
conn.close()


# --------
# MongoDB

from pymongo import MongoClient

client = MongoClient("mongodb://127.0.0.1:27017")

db = client.pyclass
coll = db["students"]

doc = {"first_name": "john", "last_name": "doe", "age": 25}

users = [
    {"first_name": "jane", "last_name": "doe", "age": 35},
    {"first_name": "jack", "last_name": "doe", "age": 45},
    {"first_name": "james", "last_name": "doe", "age": 65},
    {"first_name": "bullman", "last_name": "doe", "age": 95},
]

coll.insert_one(doc)

coll.insert_many(users)

query = {"age": {"$gt": 35}}
new_value = {"$set": {"subjects": ["physics", "electronics", "comp sci"]}}

search_res = coll.find(query)

for doc in search_res:
    print(doc["first_name"])

client.drop_database("pyclass")

Tkt

# INTRO

"""Hello World application for Tkinter"""
import tkinter as tk

root = tk.Tk()

label = tk.Label(root, text="Hello World")
label.pack()

root.mainloop()
# COMPLEX INFO


import tkinter as tk

root = tk.Tk()

root.title("Banana interest survey")

root.geometry("640x480+300+300")
root.resizable(False, False)

title = tk.Label(
    root, text="Please take the survey", font=("Arial 16 bold"), bg="brown", fg="#FF0"
)

name_var = tk.StringVar(root)
name_label = tk.Label(root, text="What is your name?")
name_inp = tk.Entry(root, textvariable=name_var)
print(name_var.get())

eater_var = tk.BooleanVar()
eater_inp = tk.Checkbutton(
    root, variable=eater_var, text="Check this box if you eat bananas"
)

num_var = tk.IntVar(value=3)
num_label = tk.Label(root, text="How many bananas do you eat per day?")
num_inp = tk.Spinbox(root, textvariable=num_var, from_=0, to=1000, increment=1)

color_var = tk.StringVar(value="Any")
color_label = tk.Label(root, text="What is the best color for a banana?")
color_inp = tk.Listbox(root, height=1)
color_choices = ("Any", "Green", "Green-Yello", "Yellow", "Brown Spotted", "Black")
color_inp = tk.OptionMenu(root, color_var, *color_choices)

# for choice in color_choices:
#     color_inp.insert(tk.END, choice)

plantain_var = tk.BooleanVar()
plantain_label = tk.Label(root, text="Do you eat plantains?")
plantain_frame = tk.Frame(root)
plantain_yes_inp = tk.Radiobutton(
    plantain_frame, text="Yes", value=True, variable=plantain_var
)
plantain_no_inp = tk.Radiobutton(
    plantain_frame, text="Ewww, no!", value=False, variable=plantain_var
)

banana_haiku_label = tk.Label(root, text="Write a haiku about bananas")
banana_haiku_inp = tk.Text(root, height=3)

submit_btn = tk.Button(root, text="Submit Survey")

output_var = tk.StringVar(value="")
output_line = tk.Label(root, textvariable=output_var, anchor="w", justify="left")

title.grid(columnspan=2)

name_label.grid(row=1, column=0)
name_inp.grid(row=1, column=1)

eater_inp.grid(row=2, columnspan=2, sticky="we")

num_label.grid(row=3, stick=tk.W)
num_inp.grid(row=3, column=1, sticky=(tk.W + tk.E))

plantain_yes_inp.pack(side="left", fill="x", ipadx=10, ipady=5)
plantain_no_inp.pack(side="left", fill="x", ipadx=10, ipady=5)
plantain_label.grid(row=6, columnspan=2, sticky=tk.W)
plantain_frame.grid(row=7, columnspan=2, sticky=tk.W)

banana_haiku_label.grid(row=8, sticky=tk.W)
banana_haiku_inp.grid(row=9, columnspan=2, sticky="NSEW")

submit_btn.grid(row=99)

output_line.grid(row=100, columnspan=2, sticky="NSEW")

root.columnconfigure(1, weight=1)
root.rowconfigure(99, weight=2)
root.rowconfigure(100, weight=1)


def on_submit():
    """To be run when the user submits the form"""
    name = name_var.get()
    try:
        number = num_var.get()
    except tk.TclError:
        number = 10000

    color = color_var.get()
    banana_eater = eater_var.get()
    plantain_eater = plantain_var.get()
    haiku = banana_haiku_inp.get("1.0", tk.END)

    message = f"Thanks for taking the survery, {name}.\n"

    if not banana_eater:
        message += "Sorry you don't like bananas!\n"
    else:
        message += f"Enter your {number} {color} bananas!\n"

    if plantain_eater:
        message += "Enjoy your plantains!"
    else:
        message += "May you successfully avoid plantains!"

    if haiku.strip():
        message += f"\n\nYour Haiku:\n{haiku}"

    output_var.set(message)


submit_btn.configure(command=on_submit)

root.mainloop()


# # Old on_submit code:
# def on_submit():
#     """To be run when the user submits the form"""

#     name = name_inp.get()
#     number = num_inp.get()

#     selected_idx = color_inp.curselection()
#     if selected_idx:
#         color = color_inp.get(selected_idx)
#     else:
#         color = ""

#     haiku = banana_haiku_inp.get("1.0", tk.END)

#     message = (
#         f"Thanks for taking the survey, {name}. \n"
#         f"Enjoy your {number} {color} bananas!"
#     )
#     output_line.configure(text=message)
#     print(haiku)