from flask import Flask
import requests
import re
from html.parser import HTMLParser
import bs4
app = Flask(__name__)


@app.route('/')
def index():
	return "Hello World"

# e.g. http://127.0.0.1:5000/myvouchercodes/boots
@app.route('/myvouchercodes/<string:company>')
def my_voucher_codes(company):
    response = requests.get('http://www.myvouchercodes.co.uk/s?q=' + company)
    soup = bs4.BeautifulSoup(response.text, "html.parser")
    
    codes = []
    for p in soup.findAll('div'):
	    code = p.find('div', {'class':'Offer-code'})
	    if code:
	        if code.text not in codes: 
		        codes.append(code.text)

    print (codes)
    return ' '.join(codes)

# e.g. http://127.0.0.1:5000/hotukdeals/boots
@app.route('/hotukdeals/<string:company>')
def hotukdeals(company):
    response = requests.get('http://www.hotukdeals.com/vouchers/' + company)
    soup = bs4.BeautifulSoup(response.text, "html.parser")
    codes = []
    for p in soup.findAll('div'):
	    code = p.find('input', {'class':'voucherReveal-peel-bottom-code'})
	    if code:
	        if code.attrs["value"] not in codes: 
		        codes.append(code.attrs["value"])

    print (codes)
    return ' '.join(codes)



if __name__ == '__main__':
    app.run()


@@ -89,16 +89,28 @@
 ort bs4%0A
+import json%0A
 app = Fl
@@ -451,34 +451,34 @@
    %0A    codes = 
-%5B%5D
+%7B%7D
 %0A    for p in so
@@ -488,33 +488,36 @@
 findAll('div'):%0A
-%09
+    
     code = p.fin
@@ -553,61 +553,109 @@
 '%7D)%0A
-%09    if code:%0A%09        if code.text not in codes: %0A%09%09
+        title = p.find('h3', %7B'class':'Offer-title'%7D)%0A        if code:%0A            if title:%0A        
     
@@ -659,32 +659,25 @@
        codes
-.append(
+%5B
 code.text)%0A%0A
@@ -673,19 +673,64 @@
 ode.text
-)%0A%0A
+%5D = title.text%0A    %0A    # for c in codes:%0A    # 
     prin
@@ -724,38 +724,45 @@
    #     print (
+c, 
 codes
-)
+%5Bc%5D)%0A
 %0A    return ' '.
@@ -757,31 +757,22 @@
  return 
-' '.join(
 codes
-)
+%0A
 %0A%0A# e.g.
@@ -1031,10 +1031,10 @@
 s = 
-%5B%5D
+%7B%7D
 %0A   
@@ -1064,17 +1064,120 @@
 'div'):%0A
-%09
+        title = p.find('h2', %7B'class':'thread-title-text voucherbox-thread-title-text hd--inline'%7D)%0A    
     code
@@ -1239,17 +1239,20 @@
 code'%7D)%0A
-%09
+    
     if c
@@ -1256,17 +1256,46 @@
 f code:%0A
-%09
+            if title:%0A        
         
@@ -1334,101 +1334,838 @@
 des:
- %0A%09%09        codes.append(code.attrs%5B%22value%22%5D)%0A%0A    print (codes)%0A    return ' '.join(codes)%0A%0A
+%0A                    codes%5Bcode.attrs%5B%22value%22%5D%5D = title.text%0A%0A    # for c in codes:%0A    #     print (c, codes%5Bc%5D)%0A%0A    return codes%0A%0A%0A@app.route('/%3Cstring:company%3E')%0Adef allvouchers(company):%0A    c1 = hotukdeals(company)%0A    c2 = my_voucher_codes(company)%0A%0A    codeslikely = %7B%7D%0A    codesother = %7B%7D%0A%0A    for c in c1:%0A        if c in c2:%0A            codeslikely%5Bc%5D = c1%5Bc%5D%0A        else:%0A            codesother%5Bc%5D = c1%5Bc%5D%0A%0A    for c in c2:%0A        if c not in codeslikely:%0A            codesother%5Bc%5D = c2%5Bc%5D%0A%0A    print (%22likely codes: %22, codeslikely)%0A    codesall = codeslikely%0A    for c in codesother:%0A        if c not in codeslikely:%0A            codesall%5Bc%5D = codesother%5Bc%5D%0A    print(%22%22)%0A%0A    print (%22all codes: %22, codesall)%0A    print(%22%22)%0A%0A    output = json.dumps(codesall, ensure_ascii=False)%0A%0A    print (output)%0A%0A    return output
 %0A%0Aif

jsonified output