SHARE
TWEET

Untitled

a guest Sep 20th, 2019 152 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from django.shortcuts import render,redirect
  2. from .forms import UrlForm
  3. from .models import URLCollection
  4. import requests
  5. from bs4 import BeautifulSoup
  6. import smtplib
  7. import time
  8.  
  9.  
  10.  
  11. def sign(request):
  12.    
  13.     if request.method == 'POST':
  14.         form = UrlForm(request.POST)
  15.  
  16.         if form.is_valid():
  17.             Url=request.POST['Url']
  18.             Email=request.POST['Email']
  19.             Price=request.POST['Price']
  20.             Price=float(Price)
  21.             new_Url = URLCollection(URL=request.POST['Url'],email=request.POST['Email'],price=request.POST['Price'])
  22.             new_Url.save()
  23.             print("Object Created")  
  24.             return redirect('mail')
  25.     else:
  26.         form = UrlForm()
  27.     context = {'form':form}    
  28.     return render(request, 'sign.html',context)
  29.     every()
  30. def check_price(Id,URL,email,Price) :
  31.         headers = {"User-Agent":'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36'}
  32.         pages = requests.get(URL, headers=headers)
  33.         soup = BeautifulSoup(pages.content,'html.parser')
  34.         print(Id)
  35.         print(URL)
  36.         print(email)
  37.         Price=float(Price)
  38.         if(URL.__contains__('amazon')):
  39.                 title = soup.find(id='productTitle').get_text()
  40.                 price = soup.find(id='priceblock_ourprice').get_text()
  41.                 price = price.replace(',','')
  42.                 price = price[2:]
  43.                 print(title.strip())
  44.                 print(price.strip())
  45.                 convertedprice = float(price)
  46.                 if(convertedprice < Price):
  47.                     send_mail(URL,title,email)
  48.                     return True
  49.                 else:
  50.                     return False    
  51.                    
  52.         else:
  53.                 soup = BeautifulSoup(pages.content, 'html.parser')
  54.                 price=''
  55.                 title=''
  56.                 for foo in soup.find_all('div', attrs={'class': '_1vC4OE _3qQ9m1'}):
  57.                     price=foo.text
  58.                 price = price.replace(',','')
  59.                 price = price[1:]                          
  60.                 print(price.strip())
  61.                 convertedprice = float(price)
  62.                 if(convertedprice < Price):
  63.                     send_mail(URL,title,email)
  64.                     print("Hey Email Has been Sent")
  65.                     return True
  66.                 else:
  67.                     return False                    
  68. def send_mail(URL,Title,Email):
  69.     server = smtplib.SMTP('smtp.gmail.com',587)
  70.     server.ehlo()
  71.     server.starttls()
  72.     server.ehlo()
  73.  
  74.     server.login('sipun2599@gmail.com','swzpexjtsptldsel')
  75.     subject = 'Price went down for '+ Title +''
  76.     if(URL.__contains__('amazon.com')):
  77.         body = ' check the amazon link '+ URL
  78.     else:
  79.         body = ' check the flipkart link '+ URL      
  80.     msg = f"Subject: {subject} \n\n {body}"
  81.     print(Email)
  82.     server.sendmail(
  83.         'sipun2599@gmail.com',
  84.         Email,
  85.         msg
  86.     )
  87.    
  88.     server.quit()            
  89.    
  90. def mail(request):
  91.     return render(request,'mail.html')
  92. def remove_from():
  93.     URLCollection.objects.filter(sendMail=True).delete()    
  94. ## To make this async !!!
  95. def every():
  96.     URLobject = URLCollection.objects.order_by('date_added')
  97.     for url in URLobject:
  98.         #print(url.id)
  99.         #print(url.email)
  100.         #print(url.URL)
  101.         #print(url.price)
  102.         url.sendMail=check_price(url.id,url.URL,url.email,url.price)
  103.         url.save()
  104.         print(url.sendMail)
  105.     remove_from()
  106.     time.sleep(60)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top