2020-01-21 23:26:49 +03:00
|
|
|
from app import app
|
2020-01-23 09:19:17 +03:00
|
|
|
from bs4 import BeautifulSoup
|
2020-01-21 23:26:49 +03:00
|
|
|
from flask import request, redirect, Response, render_template
|
|
|
|
import os
|
|
|
|
import pycurl
|
2020-01-23 09:19:17 +03:00
|
|
|
import re
|
2020-01-21 23:26:49 +03:00
|
|
|
from .url import url_parse
|
|
|
|
from io import BytesIO
|
|
|
|
|
2020-01-22 08:51:02 +03:00
|
|
|
MOBILE_UA = 'Mozilla/5.0 (Linux; Android 8.0.0; SM-G960F Build/R16NW) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.84 Mobile Safari/537.36'
|
|
|
|
DESKTOP_UA = 'Brozilla/5.0 (Windows NT 6.1; Win64; x64; rv:59.0) Gecko/20100101 Mobile LizzieMcGuirefox/59.0'
|
|
|
|
|
2020-01-21 23:26:49 +03:00
|
|
|
|
|
|
|
@app.route('/', methods=['GET'])
|
|
|
|
def index():
|
|
|
|
return render_template('index.html')
|
|
|
|
|
|
|
|
|
|
|
|
@app.route('/search', methods=['GET'])
|
|
|
|
def search():
|
|
|
|
q = request.args.get('q')
|
|
|
|
if q is None or len(q) <= 0:
|
|
|
|
return render_template('error.html')
|
|
|
|
|
2020-01-22 04:07:08 +03:00
|
|
|
tbm = ''
|
|
|
|
if 'tbm' in request.args:
|
|
|
|
tbm = '&tbm=' + request.args.get('tbm')
|
|
|
|
|
2020-01-23 09:19:17 +03:00
|
|
|
start = ''
|
|
|
|
if 'start' in request.args:
|
|
|
|
start = '&start=' + request.args.get('start')
|
|
|
|
|
2020-01-22 08:51:02 +03:00
|
|
|
user_agent = request.headers.get('User-Agent')
|
2020-01-23 09:19:17 +03:00
|
|
|
full_query = url_parse(q) + tbm + start
|
2020-01-22 08:51:02 +03:00
|
|
|
|
|
|
|
google_ua = DESKTOP_UA
|
|
|
|
if 'Android' in user_agent or 'iPhone' in user_agent:
|
|
|
|
google_ua = MOBILE_UA
|
|
|
|
|
2020-01-21 23:26:49 +03:00
|
|
|
b_obj = BytesIO()
|
|
|
|
crl = pycurl.Curl()
|
2020-01-23 09:19:17 +03:00
|
|
|
crl.setopt(crl.URL, 'https://www.google.com/search?gbv=1&q=' + full_query)
|
2020-01-22 08:51:02 +03:00
|
|
|
crl.setopt(crl.USERAGENT, google_ua)
|
2020-01-21 23:26:49 +03:00
|
|
|
crl.setopt(crl.WRITEDATA, b_obj)
|
|
|
|
crl.perform()
|
|
|
|
crl.close()
|
2020-01-23 09:19:17 +03:00
|
|
|
get_body = b_obj.getvalue().decode('utf-8', 'ignore')
|
|
|
|
get_body = get_body.replace('data-src', 'src').replace('.001', '1').replace('visibility:hidden', 'visibility:visible').replace('>G<', '>Bl<')
|
|
|
|
|
|
|
|
pattern = re.compile('4285f4|ea4335|fbcc05|34a853|fbbc05', re.IGNORECASE)
|
|
|
|
get_body = pattern.sub('0000ff', get_body)
|
|
|
|
|
|
|
|
soup = BeautifulSoup(get_body, 'html.parser')
|
|
|
|
try:
|
|
|
|
for script in soup("script"):
|
|
|
|
script.decompose()
|
|
|
|
soup.find('div', id='sfooter').decompose()
|
|
|
|
except Exception:
|
|
|
|
pass
|
|
|
|
|
|
|
|
return render_template('search.html', response=soup)
|
2020-01-21 23:26:49 +03:00
|
|
|
|
|
|
|
|
|
|
|
@app.route('/url', methods=['GET'])
|
|
|
|
def url():
|
2020-01-23 09:19:17 +03:00
|
|
|
if 'url' in request.args:
|
|
|
|
return redirect(request.args.get('url'))
|
|
|
|
|
2020-01-21 23:26:49 +03:00
|
|
|
q = request.args.get('q')
|
|
|
|
if len(q) > 0 and 'http' in q:
|
|
|
|
return redirect(q)
|
|
|
|
else:
|
|
|
|
return render_template('error.html')
|
|
|
|
|
|
|
|
|
2020-01-23 09:19:17 +03:00
|
|
|
@app.route('/imgres')
|
|
|
|
def imgres():
|
|
|
|
return redirect(request.args.get('imgurl'))
|
|
|
|
|
|
|
|
|
2020-01-21 23:26:49 +03:00
|
|
|
if __name__ == '__main__':
|
|
|
|
app.run(debug=True, host='0.0.0.0')
|