From 8d7f9126261612c0ff23d5ba875668a2d29bd035 Mon Sep 17 00:00:00 2001 From: Uday Date: Sun, 13 Aug 2023 09:44:01 +0530 Subject: [PATCH] Add files via upload --- app.ipynb | 83 +++++++++++++++++++++++++++++++++++++++++++++ app.py | 94 +++++++++++++++++++++++++++++++++++++++++++++++++++ background.js | 5 +++ manifest.json | 14 ++++++++ popup.html | 17 ++++++++++ popup.js | 26 ++++++++++++++ 6 files changed, 239 insertions(+) create mode 100644 app.ipynb create mode 100644 app.py create mode 100644 background.js create mode 100644 manifest.json create mode 100644 popup.html create mode 100644 popup.js diff --git a/app.ipynb b/app.ipynb new file mode 100644 index 0000000..66ea8aa --- /dev/null +++ b/app.ipynb @@ -0,0 +1,83 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "e65ac427", + "metadata": {}, + "outputs": [], + "source": [ + "from flask import Flask, request\n", + "import pandas as pd\n", + "import numpy as np\n", + "import smtplib\n", + "import getpass\n", + "app = Flask(__name__)\n", + "\n", + "@app.route('/submit', methods=['POST'])\n", + "def submit():\n", + " email = request.json['email']\n", + " price = request.json['price']\n", + " \n", + " print('Email:', email)\n", + " print('Price:', price)\n", + " data = pd.read_csv(r\"C:\\\\Users\\\\DELL\\\\GPTEXTENSION\\\\data.csv\",index_col=0)\n", + "# print(column_array)\n", + "# print(column_array[-2])\n", + "# print(column_array[-1])\n", + " dat = np.array(data)\n", + " current_price=dat[len(dat)-1]\n", + "# print(required_price)\n", + " if(int(price) > int(current_price)):\n", + " HOST = \"smtp-mail.outlook.com\"\n", + " PORT = 587\n", + "\n", + " FROM_EMAIL = \"\"\n", + " TO_EMAIL = email\n", + " PASSWORD = \"\"\n", + "\n", + " MESSAGE = \"\"\"Subject: \n", + " \"\"\"\n", + "\n", + " smtp = smtplib.SMTP(HOST, PORT)\n", + "\n", + " status_code, response = smtp.ehlo()\n", + " print(f\"[*] Echoing the server: {status_code} {response}\")\n", + "\n", + " status_code, response = smtp.starttls()\n", + " print(f\"[*] Starting TLS connection: {status_code} {response}\")\n", + "\n", + " status_code, response = smtp.login(FROM_EMAIL, PASSWORD)\n", + " print(f\"[*] Logging in: {status_code} {response}\")\n", + "\n", + " smtp.sendmail(FROM_EMAIL, TO_EMAIL, MESSAGE)\n", + " smtp.quit()\n", + " return { 'success': True }\n", + "\n", + "if __name__ == '__main__':\n", + " app.run(debug=True)\n" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "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.9.13" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/app.py b/app.py new file mode 100644 index 0000000..43ed022 --- /dev/null +++ b/app.py @@ -0,0 +1,94 @@ +# app.py +from selenium.webdriver.chrome.service import Service +from selenium.webdriver.chrome.options import Options +from selenium.webdriver.common.keys import Keys +from selenium.webdriver import Chrome +from selenium.webdriver.support.ui import WebDriverWait +from selenium.webdriver.support import expected_conditions as EC +from selenium.webdriver.common.by import By +# import time +import json +import requests +import re +from bs4 import BeautifulSoup +import plotly.graph_objs as go +from flask import Flask, request, send_file +from flask import Flask, request, jsonify, make_response +import io +import pandas as pd + + +service = Service('/home/uday/Chromedriver') +service.start() +options = Options() +options.add_argument('--no-sandbox') +options.add_argument('--disable-dev-shm-usage') +options.add_argument('--headless') +browser = Chrome(service=service, options=options) + +app = Flask(__name__) + +@app.route('/generate_graph') +def generate_graph(): + # Get product URL from request + product_url = request.args.get('product_url') + # print(f"Product URL :- {product_url}") + + browser.get('https://www.pricebefore.com/') + print("Ok") + + search_box = WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.ID, 'search-box-home'))) + search_box.send_keys(product_url) + search_box.send_keys(Keys.RETURN) + + # time.sleep(7) + + new_url = browser.current_url + # print('New URL:', new_url) + + r = requests.get(new_url) + htmlcontent = r.content + + soup = BeautifulSoup(htmlcontent,'html.parser') + title = soup.title + # print(title) + + paras = soup.find_all('canvas') + + paras1 = None + scripts = soup.find_all('script') + for script in scripts: + if 'var data' in script.text: + paras1 = script.text + break + + if paras1: + match = re.search(r'var\s+data\s*=\s*(.*?);', paras1) + if match: + data_str = match.group(1) + # print(data_str) + data = json.loads(data_str) + dates_list = data["dates"] + prices_list = data["prices"] + df = pd.DataFrame({'col1': dates_list, 'col2': prices_list}) + df.to_csv('output.csv',index=False) + + return generate_and_return_graph(dates_list, prices_list) + +def generate_and_return_graph(x_data, y_data): + trace = go.Scatter(x=x_data, y=y_data, mode='lines+markers') + fig = go.Figure(data=[trace]) + fig.update_layout(hovermode='x') + + # Generate the plot using plotly + fig.show() + + # Save the plot to a PNG file + buf = io.BytesIO() + fig.write_image(buf, format='png') + buf.seek(0) + # Return the file as a Flask response + return send_file(buf, mimetype='image/png') + +if __name__ == '__main__': + app.run() diff --git a/background.js b/background.js new file mode 100644 index 0000000..78b33e2 --- /dev/null +++ b/background.js @@ -0,0 +1,5 @@ +chrome.browserAction.onClicked.addListener(function(tab) { + chrome.tabs.executeScript({ + file: 'popup.js' + }); +}); diff --git a/manifest.json b/manifest.json new file mode 100644 index 0000000..bd64313 --- /dev/null +++ b/manifest.json @@ -0,0 +1,14 @@ +{ + "manifest_version": 2, + "name": "Price History Graph", + "version": "1.0", + "description": "Displays a graph of price history for a product.", + "browser_action": { + "default_title": "Price History Graph", + "default_popup": "popup.html" + }, + "permissions": [ + "activeTab", + "http://localhost:5000/*" + ] +} diff --git a/popup.html b/popup.html new file mode 100644 index 0000000..f0f25ec --- /dev/null +++ b/popup.html @@ -0,0 +1,17 @@ + + + + + Product Graph + + + +

Product Price History Graph

+ + + + diff --git a/popup.js b/popup.js new file mode 100644 index 0000000..5ab6bad --- /dev/null +++ b/popup.js @@ -0,0 +1,26 @@ +document.addEventListener('DOMContentLoaded', function() { + var graph = document.getElementById('graph'); + var detailsForm = document.getElementById('detailsForm'); + detailsForm.addEventListener('submit', function(event) { + event.preventDefault(); + var email = detailsForm.email.value; + var price = detailsForm.price.value; + console.log('Email:', email); + console.log('Price:', price); + }); + + chrome.tabs.query({active: true, currentWindow: true}, function(tabs) { + var product_url = tabs[0].url; + var xhr = new XMLHttpRequest(); + xhr.open('GET', 'http://localhost:5000/generate_graph?product_url=' + encodeURIComponent(product_url), true); + xhr.responseType = 'blob'; + xhr.onload = function(e) { + if (this.status == 200) { + var blob = this.response; + var objectURL = URL.createObjectURL(blob); + graph.src = objectURL; + } + }; + xhr.send(); + }); +});