Skip to content

Instantly share code, notes, and snippets.

@ty-shaikh
ty-shaikh / main_p3.py
Created December 3, 2019 16:30
Combined code from Part 1 and 2.
# Import packages and functions from local scripts
import pickle
from scraping import run_search, get_attributes, get_days
from mailer import generate_markup, send_email
# Constant variables
SEARCH_URL = "https://poshmark.com/brand/Naked_&_Famous_Denim-Men-Jeans?sort_by=added_desc"
DAYS = 4
USER_EMAIL = "PERSONAL_ACCOUNT@gmail.com"
SENDER_EMAIL = "DUMMY_ACCOUNT@gmail.com"
@ty-shaikh
ty-shaikh / send_email_p2.py
Created December 1, 2019 16:55
Send email with generated markup to a personal Gmail account.
def send_email(items, recipient):
sender_email = "DUMMY_ACCOUNT@gmail.com"
receiver_email = recipient
password = "DUMMY_ACCOUNT_PASSWORD"
message = MIMEMultipart("alternative")
message["Subject"] = "Latest Poshmark Listings"
message["From"] = sender_email
message["To"] = receiver_email
@ty-shaikh
ty-shaikh / imports_p2.py
Last active December 1, 2019 16:55
Imports for part 2 of Poshmark tutorial.
from html2text import html2text
import smtplib, ssl
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
USER_EMAIL = "PERSONAL_ACCOUNT@gmail.com"
@ty-shaikh
ty-shaikh / generate_markup_p2.py
Last active December 1, 2019 16:40
Generate email markup for recent items.
import pickle
def generate_markup(title, collection):
html = "<div style='text-align: center;'>"
heading = f'<p style="font-size: 1.5rem; font-weight: bold;">{title}&nbsp;<span style="font-size: 1rem;">({len(collection)} items)</span></p>'
item_group = ""
for item in collection:
title = item[0]
price = item[1]
@ty-shaikh
ty-shaikh / pickle_p1.py
Created December 1, 2019 00:01
Pickle the formatted search results.
pickle.dump(recent_items, open("naked_and_famous.p", "wb"))
@ty-shaikh
ty-shaikh / email_template_p2.html
Created November 30, 2019 23:43
Email template for Poshmark listings.
<html>
<body>
<div style="margin: 0 auto;">
<!-- Heading -->
<div>
<h1 style="text-align: center;">New Poshmark Listings</h1>
<hr>
</div>
<br>
recent_items = []
for card in product_cards:
difference = get_days(card)
if difference <= DAYS:
card_values = get_attributes(card)
recent_items.append(card_values)
else:
break
@ty-shaikh
ty-shaikh / days_difference_p1.py
Created November 30, 2019 18:14
Calculate the days between when an item is posted and when the script is run.
def get_days(soup_obj):
"Convert to EST and return difference in days"
created_date = soup_obj['data-created-at']
pst_date = parse(created_date, ignoretz=True)
est_date = pst_date + timedelta(hours=3)
now = datetime.now()
diff = abs((est_date-now).days)
@ty-shaikh
ty-shaikh / product_attributes_p1.py
Last active November 30, 2019 18:12
Extract product attributes from card container.
def get_attributes(soup_obj):
"Extract product values from card"
price = soup_obj['data-post-price']
url_tag = soup_obj.a
url = "https://poshmark.com" + url_tag['href']
title = url_tag['title']
img_tag = url_tag.img
img = img_tag['src']
@ty-shaikh
ty-shaikh / run_search_p1.py
Created November 30, 2019 17:17
Pull down Poshmark search results.
def run_search(search_url):
"Pull down search results and extract out product cards"
response = get(search_url, headers=HEADER)
html_soup = BeautifulSoup(response.text, 'html.parser')
item_container = html_soup.find_all('div', class_ = 'tile')
return item_container
product_cards = run_search(SEARCH_URL)
print(product_cards)