Skip to content

Instantly share code, notes, and snippets.

@Xnuvers007
Last active October 14, 2023 10:19
Show Gist options
  • Select an option

  • Save Xnuvers007/cd5cdd01830cba8aff8e91f86427d058 to your computer and use it in GitHub Desktop.

Select an option

Save Xnuvers007/cd5cdd01830cba8aff8e91f86427d058 to your computer and use it in GitHub Desktop.
lirikgoogle.py
from flask import Flask, request, jsonify
import requests, random, time
from bs4 import BeautifulSoup
from fake_useragent import UserAgent
app = Flask(__name__)
user_agent = UserAgent()
random_user_agent = user_agent.random
google_sites = [
"google.com", "google.ad", "google.ae", "google.com.af", "google.com.ag", "google.al", "google.am",
"google.co.ao", "google.com.ar", "google.as", "google.at", "google.com.au", "google.az", "google.ba",
"google.com.bd", "google.be", "google.bf", "google.bg", "google.com.bh", "google.bi", "google.bj",
"google.com.bn", "google.com.bo", "google.com.br", "google.bs", "google.bt", "google.co.bw",
"google.by", "google.com.bz", "google.ca", "google.cd", "google.cf", "google.cg", "google.ch",
"google.ci", "google.co.ck", "google.cl", "google.cm", "google.cn", "google.com.co", "google.co.cr",
"google.com.cu", "google.cv", "google.com.cy", "google.cz", "google.de", "google.dj", "google.dk",
"google.dm", "google.com.do", "google.dz", "google.com.ec", "google.ee", "google.com.eg", "google.es",
"google.com.et", "google.fi", "google.com.fj", "google.fm", "google.fr", "google.ga", "google.ge",
"google.gg", "google.com.gh", "google.com.gi", "google.gl", "google.gm", "google.gr", "google.com.gt",
"google.gy", "google.com.hk", "google.hn", "google.hr", "google.ht", "google.hu", "google.co.id",
"google.ie", "google.co.il", "google.im", "google.co.in", "google.iq", "google.is", "google.it",
"google.je", "google.com.jm", "google.jo", "google.co.jp", "google.co.ke", "google.com.kh",
"google.ki", "google.kg", "google.co.kr", "google.com.kw", "google.kz", "google.la", "google.com.lb",
"google.li", "google.lk", "google.co.ls", "google.lt", "google.lu", "google.lv", "google.com.ly",
"google.co.ma", "google.md", "google.me", "google.mg", "google.mk", "google.ml", "google.com.mm",
"google.mn", "google.com.mt", "google.mu", "google.mv", "google.mw", "google.com.mx", "google.com.my",
"google.co.mz", "google.com.na", "google.com.ng", "google.com.ni", "google.ne", "google.nl", "google.no",
"google.com.np", "google.nr", "google.nu", "google.co.nz", "google.com.om", "google.com.pa",
"google.com.pe", "google.com.pg", "google.com.ph", "google.com.pk", "google.pl", "google.pn",
"google.com.pr", "google.ps", "google.pt", "google.com.py", "google.com.qa", "google.ro", "google.ru",
"google.rw", "google.com.sa", "google.com.sb", "google.sc", "google.se", "google.sh", "google.si",
"google.sk", "google.com.sl", "google.sn", "google.so", "google.sm", "google.sr", "google.st",
"google.com.sv", "google.td", "google.tg", "google.co.th", "google.com.tj", "google.tl", "google.tm",
"google.tn", "google.to", "google.com.tr", "google.tt", "google.com.tw", "google.co.tz",
"google.com.ua", "google.co.ug", "google.co.uk", "google.com.uy", "google.co.uz", "google.com.vc",
"google.co.ve", "google.co.vi", "google.com.vn", "google.vu", "google.ws", "google.rs",
"google.co.za", "google.co.zm", "google.co.zw", "google.cat"
]
def search_google(query, headers):
random.shuffle(google_sites)
for site in google_sites:
search_url = f"https://www.{site}/search"
params = {
'q': query,
}
response = requests.get(search_url, params=params, headers=headers)
if "Hasil pencarian tidak ditemukan" not in response.text or "Tidak ada hasil pencarian yang ditemukan" not in response.text or response.status_code == 200:
soup = BeautifulSoup(response.text, 'html.parser')
div = soup.find('div', class_='xaAUmb')
result = []
result.append(soup.find('div', class_='PZPZlf ssJ7i B5dxMb').text.strip())
result.append(soup.find('div', class_='iAIpCb PZPZlf').text.strip())
result.append("")
for i in div.find_all('span'):
result.append(i.text.strip())
return result
time.sleep(random.uniform(3, 5))
return []
@app.route('/googlelirik', methods=['GET'])
def google_lirik():
user_input = request.args.get('lagu')
if not user_input:
return jsonify({'error': request.host_url + 'googlelirik?lagu=Nadhif+basalamah+penjaga+hati'}), 200
query = f"{user_input}+lyrics"
headers = {
'User-Agent': random_user_agent,
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8',
'Accept-Language': 'id,en-US;q=0.7,en;q=0.3',
'Alt-Used': 'www.google.com',
'Connection': 'keep-alive',
'Upgrade-Insecure-Requests': '1',
'Sec-Fetch-Dest': 'document',
'Sec-Fetch-Mode': 'navigate',
'Sec-Fetch-Site': 'none',
'Sec-Fetch-User': '?1',
}
search_result = search_google(query, headers)
if search_result:
return jsonify({'result': search_result})
else:
return jsonify({'error': 'No search results found'})
if __name__ == '__main__':
app.run(debug=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment