Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Some basic scripts
from ast import While
import os
from fuzzywuzzy import fuzz
import json
import datetime
import time
import requests
import glob
from rich import print as rprint
def clear():
os.system('clear')
def is_number(s):
try:
float(s)
return True
except ValueError:
return False
def get_date(t):
if is_number(t):
# get Today dat of month
d = datetime.datetime.today().day
m = datetime.datetime.today().month
y = datetime.datetime.today().year
if int(t) <= d:
return f"{m}/{t}/{y}"
else:
return f"{m-1}/{t}/{y}"
else:
return t
def get_row():
obj = {
'Purchase Date': get_last_prop('Purchase Date') or '',
'Item': '',
'Amount': '',
'Category': '',
}
while True:
clear()
t = {
"old": {
"Purchase Date": get_last_prop('Purchase Date'),
"Item": get_last_prop('Item'),
"Amount": get_last_prop('Amount'),
"Category": get_last_prop('Category'),
},
"new": obj
}
print(json.dumps(t, indent=2))
if obj['Purchase Date'] == '':
prop = "Purchase Date"
elif obj['Item'] == '':
prop = "Item"
elif obj['Amount'] == '':
prop = "Amount"
elif obj['Category'] == '':
prop = "Category"
else:
prop = ""
if prop == "":
return obj
user_input = input(f"{prop}: ")
if(user_input == "."):
obj[prop] = get_last_prop(prop)
if obj[prop] == None:
obj[prop] = ""
print("No previous value")
time.sleep(1)
continue
elif user_input == "..":
for key in obj.keys():
if obj[key] == '':
obj[key] = get_last_prop(key)
if obj[key] == None:
obj[key] = ""
continue
elif user_input == "d":
obj['Purchase Date'] = ''
continue
elif user_input == "q":
return None
elif user_input == "Q":
exit(0)
if prop == 'Purchase Date':
obj['Purchase Date'] = get_date(user_input)
elif prop == 'Item':
if is_number(user_input):
obj['Amount'] = user_input
else:
obj['Item'] = user_input
elif prop == 'Amount':
while True:
if user_input == "":
user_input = input("Amount: ")
if is_number(user_input):
obj['Amount'] = user_input
break
else:
print("Not a number")
user_input = ""
elif prop == 'Category':
obj['Category'] = get_category("Category", user_input)
if obj[prop] == "":
obj[prop] = ""
def get_category(text, c):
categories = [
["Bills"],
["Subscriptions"],
["Entertainment"],
["Snacks"],
["Healthy snacks"],
["Quick food"],
["Groceries"],
["Night out"],
["Health"],
["Hygiene"],
["Transport"],
["Beauty"],
["Clothes"],
["Tech"],
["Home"],
["Gifts"],
["Miscellaneous"],
["Total Expenditure"],
["Income"],
["Gross Savings"],
]
while True:
if not 'c' in locals():
c = ""
if len(categories[0]) == 2:
c += input(f"{text}: {c}")
for category in categories:
f_radio = fuzz.ratio(c, category)
if len(category) == 1:
category.append(f_radio)
else:
category[1] = f_radio
# sort by fuzz.ratio
categories.sort(key=lambda x: x[1], reverse=True)
# print(categories)
if categories[0][1] == categories[1][1]:
print_categories(categories, c)
print("Ambiguous, add something..")
elif not categories[0][1] > 30:
print_categories(categories, c)
print("No less confident to match category")
else:
return categories[0][0]
def get_last_prop(prop):
# filename with today date
filename = f"{datetime.datetime.today().strftime('%Y-%m-%d')}-budget.json"
# check if file exists
if os.path.isfile(filename):
with open(filename, 'r') as f:
data = json.load(f)
if len(data) > 0:
return data[-1][prop]
else:
return None
else:
return None
def print_categories(categories, input):
clear()
for category in categories:
# Clear terminal
# print category[0] with green color where in 'c'
temp = ''
input_copy = input
for letter in category[0]:
if len(input_copy) > 0 and letter.lower() == input_copy[0].lower():
temp += '[green]' + letter + '[/green]'
input_copy = input_copy[1:]
continue
else:
temp += letter
rprint(f"{temp}: {category[1]}%")
def add_items():
while True:
clear()
o = get_row()
if o is None:
print("Data not saved!")
else:
print("Saving to file...")
# Add timestamp M/D/Y H:M:S
m = datetime.datetime.today().month
d = datetime.datetime.today().day
y = datetime.datetime.today().year
h = datetime.datetime.today().hour
m_ = datetime.datetime.today().minute
s = datetime.datetime.today().second
m_ = ("0" if m_ < 10 else "") + str(m_)
s = ("0" if s < 10 else "") + str(s)
o['Timestamp'] = f"{m}/{d}/{y} {h}:{m_}:{s}"
# filename with today date
filename = f"{datetime.datetime.today().strftime('%Y-%m-%d')}-budget.json"
# Check if file exists
if os.path.isfile(filename):
with open(filename, 'r') as f:
data = json.load(f)
data.append(o)
with open(filename, 'w') as f:
json.dump(data, f, indent=2)
else:
with open(filename, 'w') as f:
json.dump([o], f, indent=2)
# Sleep for a bit
time.sleep(1)
def print_menu(text, w = 54):
print(f"| {text:<{w-3}}|")
def is_file_ended_with(filename, end):
if filename.endswith(end):
return True
else:
return False
def send_to_db(file):
if is_file_ended_with(file, ".sent") or is_file_ended_with(file, "(sent)"):
print("Already sent")
time.sleep(1)
return
data = json.load(open(file))
# read token from file /home/simon/Documents/.env/API-budget
with open("/home/simon/Documents/.env/API-budget", 'r') as f:
token = f.read().strip()
print(f"token: {token}")
url = f"https://n8n.hryszko.dev/webhook/06890505-422a-4537-8caa-b7a3df44ad53?token={token}"
r = requests.post(url, json=data)
if r.status_code == 200 or r.status_code == 201:
# rename file to .sent
os.rename(file, f"{file}.sent")
print("Sent to db")
input("Press enter to continue")
else:
print("Error contacting server!")
print(r.status_code)
input(r.text)
input("Press enter to continue")
def list_last_items():
# read token from file /home/simon/Documents/.env/API-budget
with open("/home/simon/Documents/.env/API-budget", 'r') as f:
token = f.read().strip()
print(f"token: {token}")
url = f"https://n8n.hryszko.dev/webhook/15051515-151a-5158-2caa-b5a5df15ad64?token={token}"
clear()
print("Loading...")
r = requests.get(url)
if r.status_code == 200 or r.status_code == 201:
clear()
for item in r.json()[:20]:
print(item['json']['Purchase Date'], end="\t\t")
print(item['json']['Item'], end="\t\t")
print(item['json']['Amount'], end="\t\t")
print(item['json']['Category'])
input("Press enter to continue")
else:
print("Error contacting server!")
print(r.status_code)
input(r.text)
input("Press enter to continue")
if __name__ == '__main__':
while True:
# find all files has '-budget.json'
# files = [f for f in os.listdir('.') if f.endswith('-budget.json')]
files = glob.glob('*-budget.json*')
clear()
print("+----------------------------------------------------+")
print(f"| {'Welcome to the budgeting app!':^{51}}|")
print("+----------------------------------------------------+")
print_menu(f"A. Add new item")
print_menu(f"L. List last few items")
for i, file in enumerate(files):
print_menu(f"{i}. {file} {'(sent)' if is_file_ended_with(file, '.sent') else ''}")
print_menu(f"Q. Quit")
print("+----------------------------------------------------+")
user_input = input("Select: ")
if user_input.lower() == 'q':
break
elif user_input.lower() == 'a':
add_items()
elif is_number(user_input):
if int(user_input) < len(files) and int(user_input) >= 0:
send_to_db(files[int(user_input)])
elif user_input.lower() == 'l':
list_last_items()
else:
print("Invalid input")
time.sleep(1)
import os
import time
from rich import print as r
import math
def shared_chars(s1, s2):
count = 0
m = max(len(s1), len(s2))
for i in range(m):
if s1[i] == s2[i]:
count += 1
else:
break
return count
# clear screen
os.system('clear')
# 4(1-1/3+1/5-1/7 .. )
i = 3
sum = 1
negativeSign = True
while True:
if negativeSign:
sum -= 1/i
else:
sum += 1/i
negativeSign = not negativeSign
i += 2
iteration = int((i-1)/2 - 1)
PI = sum * 4
correctDigits = shared_chars(str(PI), str(math.pi))
if(len(str(PI)) > len(str(math.pi))):
correctDigits = len(str(PI))
correctPartOfPI = str(PI)[:correctDigits]
wrongPartOfPI = str(PI)[correctDigits:]
# back to the beginning of the line
print("\r", end="")
r(f"i = {iteration} | [green]{correctPartOfPI}[red]{wrongPartOfPI}", end="")
from re import L
import sqlite3
import requests
from bs4 import BeautifulSoup as bs
from rich import print
import sqlite3
import json
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy import create_engine
from sqlalchemy import Table, Column, Integer, String, MetaData, ForeignKey
from sqlalchemy.sql import select
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy import create_engine
from sqlalchemy import Table, Column, Integer, String, MetaData, ForeignKey
import os
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy import create_engine
from sqlalchemy import Table, Column, Integer, String, MetaData, ForeignKey
from sqlalchemy.sql import select
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy import create_engine
from sqlalchemy import Table, Column, Integer, String, MetaData, ForeignKey
def scrap(saveToDb=True, saveToJson=False, debug=False, paginationLimit=None):
# Variables
shoes_db={}
link="https://worldbox.pl/menu/meskie/obuwie"
header = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'}
# Make soup obj
r = requests.get(link, headers=header)
soup = bs(r.text, "html.parser")
# Get pagination limit
pagination = soup.find("ul", {"class": "pagination__list"})
pagination = pagination.find_all("li")
pagination = pagination[-2].find("a").text
if debug:
print(f"Pagination limit: {pagination}")
if paginationLimit:
pagination = paginationLimit
if debug:
print(f"Pagination limit changed to: {pagination}")
# go through pages
for shoes in range(int(pagination)):
# soup obj
_link="https://worldbox.pl/menu/meskie/obuwie/page=" + str(shoes + 1)
r = requests.get(_link, headers=header)
soup = bs(r.text, "html.parser")
# get shoes and add to list (dict)
shoes = soup.find_all("div", {"class": "listing-product"})
for shoe in shoes:
link = shoe.find("a").get("href")
shoes_db[link] = {}
if debug:
print(f"Found shoe: {link}")
if debug:
print(f"Finished scraping pages, found {len(shoes_db)} shoes!")
print("Starting scraping shoes (each shoe)...")
# go through shoes list (dict)
for link, i in shoes_db.items():
# soup obj
r = requests.get(link, headers=header)
soup = bs(r.text, "html.parser")
#get name
i["name"] = soup.find("h1", {"class": "product__name"}).text
#get price
i["price"] = soup.find("div", {"class": "product-price"}).text.replace(u'\xa0', ' ')
#get sizes
tmp = soup.find("ul", {"class": "size__list"}).find_all("li")
i["size"] = []
for size in tmp:
# get li['data-size']
i["size"].append(size.text.strip())
# get props
i["properties"] = []
tmp = soup.find_all("div", {"class": "default-style"})
props = tmp[0].find("p")
# split props by <br>
props = props.text.split("<br>")
# split props by \n
props = props[0].split("\n")
for prop in props:
temp = prop.split(":")
if len(temp) > 1:
i["properties"].append({temp[0].replace(u'\xa0', ''): temp[1].replace(u'\xa0', '')})
if debug:
print(f"Scrapped shoe: {i['name']}")
if saveToJson:
# save to file
with open("shoes.json", "w") as f:
json.dump(shoes_db, f)
if debug:
print("Saved to file: shoes.json")
if saveToDb:
# remove file if exists
if os.path.exists("shoes.db"):
os.remove("shoes.db")
if debug:
print("Removed file: shoes.db")
if debug:
print("Opening connections with database...")
# sqlite connection
engine = create_engine('sqlite:///shoes.sqlite', echo=False)
# Shoes database object
shoes = Table('shoes', MetaData(),
Column('id', Integer, primary_key=True),
Column('link', String),
Column('name', String),
Column('price', String),
Column('size', String),
Column('properties', String)
)
# Make a connection
conn = engine.connect()
if debug:
added = 0
# Insert data into database
for shoe in shoes_db:
s = shoes.insert()
s = s.values(link=shoe, name=shoes_db[shoe]["name"], price=str(shoes_db[shoe]["price"]), size=str(shoes_db[shoe]["size"]), properties=str(shoes_db[shoe]["properties"]))
conn.execute(s)
# print how much left
if debug:
added += 1
print(f"Added {added}/{len(shoes_db)} shoes to database! Remaining: {len(shoes_db) - added}")
def search(brand, size, debug=False):
if debug:
print("Opening connection with database...")
# sqlite connection
engine = create_engine('sqlite:///shoes.sqlite', echo=False)
# Shoes database object
shoes = Table('shoes', MetaData(),
Column('id', Integer, primary_key=True),
Column('link', String),
Column('name', String),
Column('price', String),
Column('size', String),
Column('properties', String)
)
# Make a connection
conn = engine.connect()
# Build select statement
stmt = select(shoes)
# where brand is like brand
stmt = stmt.where(shoes.c.size.like("%" + size + "%"))
# where properties is like brand
stmt = stmt.where(shoes.c.properties.like("%, {'Marka': '" + brand + "%"))
# Execute statement
result = conn.execute(stmt)
# print results
for row in result:
print(f"{row['name']}:")
print(f" Price: {row['price']}")
print(" Size: {}".format(row['size'].replace("[", "").replace("]", "").replace("'", "").replace(".0", "")))
print(f" Link: {row['link']}")
import json
t = row['properties']
t = t.replace("[", "").replace("]", "").split(", ")
for i in t:
print(" {}".format(i.replace("'", "").replace("{", "").replace("}", "")))
print("")
# --------------------------------------------------
print("MENU:")
print("1. Scrape shoes to db")
print("2. Scrape shoes to json")
print("3. Scrape shoes to db and json")
print("4. Search shoes")
c = input("Choose option: ")
if c == "1":
scrape(saveToDb=True, saveToJson=False, debug=False)
elif c == "2":
scrape(saveToDb=False, saveToJson=True, debug=False)
elif c == "3":
scrape(saveToDb=True, saveToJson=True, debug=False)
elif c == "4":
brand = input("Brand: ")
size = input("Size: ")
search(brand, size, debug=False)
else:
print("Wrong option!")
exit(1)
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
import time
from turtle import *
color('white')
bgcolor('black')
def drawBranch(angel, deep, length):
if deep == 3:
return
else:
left(angel/2)
forward(length)
drawBranch(angel/2, deep+1, length)
backward(length)
right(angel)
forward(length)
backward(length)
drawBranch(angel/2, deep+1, length)
speed(1)
l = 100
# go to bottom center
# color('black')
# goto(0, -250)
# color('white')
# left(90)
# forward(l)
# drawBranch(90, 1, 100)
# array from 1 to 10
# r = range(3, 11)
# for i in r:
# for j in range(i):
# forward(25)
# left(360 / i)
# hideturtle()
# begin_fill()
# circle(30, 180)
# forward(200)
# circle(30, 180)
# forward(200)
# hideturtle()
# end_fill()
# left(173)
# forward(190)
# circle(-18, 170)
# done()
time.sleep(2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment