{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from selenium import webdriver\n",
    "from selenium.webdriver.common.keys import Keys\n",
    "from selenium.webdriver.support.select import Select\n",
    "from selenium.webdriver.common.by import By\n",
    "from selenium.webdriver.support.ui import WebDriverWait\n",
    "from selenium.webdriver.support import expected_conditions as EC\n",
    "from selenium.webdriver.common.action_chains import ActionChains\n",
    "import random\n",
    "from selenium.common.exceptions import NoSuchElementException\n",
    "from selenium.webdriver.common.keys import Keys\n",
    "from bs4 import BeautifulSoup as b\n",
    "import time\n",
    "from selenium.common.exceptions import ElementClickInterceptedException\n",
    "import winsound"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "browser = webdriver.Firefox(executable_path=\"C:/Users/unknown/Desktop/projects/poshmark/geckodriver\")\n",
    "wait = WebDriverWait(browser, 30)\n",
    "browser.get('https://poshmark.com/login')\n",
    "usename = browser.find_element_by_id('login_form_username_email')\n",
    "usename.send_keys('SpeanutButter')\n",
    "psw = browser.find_element_by_id('login_form_password')\n",
    "psw.send_keys('***REMOVED***')\n",
    "iframe = 'default value'\n",
    "page_number = list(range(1,18))\n",
    "base_url = 'https://poshmark.com/closet/speanutbutter?availability=available&sort_by=price_desc&all_size=true&max_id='\n",
    "\n",
    "def reCaptcha_finder(): # Still not working. Works on the first recaptcha popup but not second one for some reason\n",
    "    global iframe\n",
    "    time.sleep(4)\n",
    "    iframes = browser.find_elements_by_tag_name('iframe')\n",
    "    for frame in iframes:\n",
    "        if not iframe in iframes:\n",
    "            browser.switch_to.frame(frame)\n",
    "            try:\n",
    "                browser.find_element_by_xpath('//label[text()=\"I\\'m not a robot\"]')\n",
    "                winsound.Beep(540, 1000)\n",
    "                iframe = frame\n",
    "                input('Complete reCaptcha then press \"enter\" to continue')\n",
    "                browser.switch_to.default_content()\n",
    "                break\n",
    "            except NoSuchElementException:\n",
    "                browser.switch_to.default_content()\n",
    "\n",
    "psw.submit()\n",
    "\n",
    "reCaptcha_finder()\n",
    "\n",
    "wait.until(EC.presence_of_element_located((By.XPATH, \"//title[text()='Feed - Poshmark']\")))\n",
    "\n",
    "\n",
    "for page in page_number:\n",
    "    \n",
    "    browser.get(base_url+str(page))\n",
    "    input('press \"enter\" to continue')\n",
    "\n",
    "    html = browser.page_source\n",
    "    soup = b(html, \"lxml\")\n",
    "\n",
    "    list_titles = soup.find_all('a', {'class': 'title'})\n",
    "    active_listings = soup.find_all('i', {'class': \"icon share-gray\"})\n",
    "    print(len(active_listings))\n",
    "    container = browser.find_elements_by_xpath(\"//div[@id='tiles-con']/div\")\n",
    "    i = -1\n",
    "    share_to = input('share to followers (F) or share to party (P)?: ')\n",
    "\n",
    "    for divs in container:\n",
    "        i += 1\n",
    "        wait.until(EC.presence_of_element_located((By.XPATH, \".//i[@class = 'icon share-gray']\")))\n",
    "        try:\n",
    "            if divs.find_element_by_xpath(\".//i[@class = 'icon inventory-tag not-for-sale-tag']\"):\n",
    "                pass\n",
    "        except NoSuchElementException:\n",
    "            try:\n",
    "                if divs.find_element_by_xpath(\".//i[@class = 'icon inventory-tag sold-tag']\"):\n",
    "                    pass\n",
    "            except NoSuchElementException:\n",
    "                share = divs.find_element_by_xpath(\".//i[@class = 'icon share-gray']\")\n",
    "                time.sleep(random.uniform(.6, 1.2))\n",
    "                try:\n",
    "                    share.click()\n",
    "                    # reCaptcha_finder() # Might only need reCaptcha_finder() here and not afterwards or the other way around. Pay attention to where the recaptcha occurs\n",
    "                    if share_to == 'F':\n",
    "                        wait.until(EC.presence_of_element_located((By.XPATH, \"//span[text()='To My Followers']\")))\n",
    "                        share = browser.find_element_by_xpath(\"//span[text()='To My Followers']\")\n",
    "                        time.sleep(random.uniform(.6, 1.2))\n",
    "                        share.click()\n",
    "                        reCaptcha_finder()\n",
    "                        print(i)\n",
    "                        title = list_titles[i].get_text()\n",
    "                        print(title)\n",
    "                    if share_to == 'P':\n",
    "                        wait.until(EC.presence_of_element_located((By.XPATH, \"//*[contains(text(), 'Happening Now')]\")))\n",
    "                        share = browser.find_element_by_xpath(\"//*[contains(text(), 'Happening Now')]\")\n",
    "                        time.sleep(random.uniform(.6, 1.2))\n",
    "                        share.click()\n",
    "                        reCaptcha_finder()\n",
    "                        print(i)\n",
    "                        title = list_titles[i].get_text()\n",
    "                        print(title)\n",
    "                except ElementClickInterceptedException:\n",
    "                    pass\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "scrapers",
   "language": "python",
   "name": "scrapers"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
