Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
4.4 kB
2
Indexable
Never
import requests
import urllib.parse
import re
from bs4 import BeautifulSoup
from youtubesearchpython import VideosSearch
from pytube import YouTube
import os
import webbrowser

URL_ROOT = 'http://mojim.com/'

def findYT(search_words):
    id = VideosSearch(search_words, limit=2).result()["result"][0]["id"]
    # print(reuslt)
    youtube_url = f"https://www.youtube.com/watch?v={id}"
    return youtube_url

def search_song(song_name):
    
    song_name += '.html?t3'
    url = urllib.parse.urljoin(URL_ROOT, song_name)
    response = requests.get(url)
    soup = BeautifulSoup(response.text, 'lxml')
    songs = soup.find_all('dd', re.compile('^mxsh_dd'))
    del songs[0]
    song_list = list()
    for song  in songs:
        meta = song.find('span', 'mxsh_ss4').find('a')
        name_temp = meta.getText().split('.')
        song_list.append({
            'name':name_temp[1],
            'singer':song.find('span', 'mxsh_ss2').getText(),
            'album':song.find('span', 'mxsh_ss3').getText(),
            'link':meta.get('href'),
            })
    return song_list

def get_lyric(url):
    url = urllib.parse.urljoin(URL_ROOT, url)
    response = requests.get(url)
    soup = BeautifulSoup(response.text, 'lxml')
    lyric = soup.find('dl', 'fsZx1')

    a = re.compile('^\[\d+')

    lyric_list = list()
    for string in lyric.stripped_strings:
        if string == '更多更詳盡歌詞 在' or string == '※ Mojim.com 魔鏡歌詞網':
            continue
        if a.match(string):
            break
        lyric_list.append(string)

    singer = lyric_list.pop(0)
    name = lyric_list.pop(0)

    song_detail = {
        'singer':singer,
        'name':name,
        'lyric':lyric_list,
    }
    print(song_detail)
    return song_detail

import telebot
from telebot.types import InlineKeyboardMarkup, InlineKeyboardButton, InlineQueryResultPhoto
import json
bot = telebot.TeleBot('6425725714:AAEK-wSNfACLFCYcAt-EASxJ5EYoV61aHpI')

@bot.message_handler(commands=['search'])
def send_pong(message):
    if message.chat.type in ("supergroup","private"):
        global search_words
        search_words =message.text.split()[1]
        bot.reply_to(message, findYT(search_words))       
                 
@bot.message_handler(commands=['searchL'])
def send_ly(message):
    if message.chat.type in ("supergroup","private"):
        search_songName =message.text.split()[1]
        music_url = search_song(search_songName)[0]['link']
        # lyrics = ""
        # for lyric in get_lyric(music_url)['lyric']:
        #     lyrics += lyric
        #     lyrics += " "
        bot.reply_to(message, get_lyric(music_url)['lyric']) 

@bot.message_handler(commands=['searchE'])
def send_ly(message):
    if message.chat.type in ("supergroup","private"):
        headers = {'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36'}
    a=['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',' ',"'",'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']
    empty=[]
    n=''
    for i in message.text.split()[1]:
        bb=False
        for j in a:
            if i==j:
                bb=True
        if bb:
            n+=i.lower()
    n=n.replace(' ','+').replace("'","%27")
    url="https://search.azlyrics.com/search.php?q="+n+"&x=9edbdb599086cfb43be7941b396a8045c2321d488f5a9439d85c879caeb7d340"
    r=requests.get(url,headers=headers)
    soup=BeautifulSoup(r.text, "html.parser")
    s=soup.find_all("a")
    for i in s:
        ii=i.find_all("span")
        iii=str(ii)
        if(iii!='[]'):
            k=iii.split('"</b></span>]')
            kk=k[0].split('[<span><b>"')
            if(len(kk)>=2):
                kkk=kk[1]
                if(kkk[-8:]=="</span>]"):
                    kkk=kkk[:-9]
                for j in range(len(kkk)-4):
                    if(kkk[j:j+5]=='"</b>'):
                        kkk=kkk[:j]+kkk[j+5:]
                        break
                bot.reply_to(message, kkk) 
# @bot.message_handler(commands=['mp3'])
# def send_mp3 (message):
#     if message.chat.type in ("supergroup","private"):
#