簡體   English   中英

歌詞天才歌詞有時以“EmbedShare URLCopyEmbedCopy”結尾

[英]lyricsgenius lyrics sometimes end with "EmbedShare URLCopyEmbedCopy"

我正在制作一個 Discord 歌詞機器人並接收歌詞。 我正在使用天才 API( lyricsgenius API 包裝器)。 但是當我收到歌詞時,它以這樣的結尾:

“離開”是這首歌的最后一個詞,但它伴隨着EmbedShare URLCopyEmbedCopy 有時它只是沒有EmbedShare文本的簡單歌詞。

用同一首歌:

反正有什么可以防止的嗎?

lyrics命令的源代碼:

@commands.command(help="Gives the lyrics of the song XD! format //lyrics (author) (song name)")
async def lyrics(self, ctx, arg1, arg2):
    song = genius.search_song(arg1, arg2)
    print(song.lyrics)
    name = ("Lyrics for " + arg2.capitalize() + " by " + arg1.capitalize())
    gembed = discord.Embed(title=name.capitalize(), description=song.lyrics)
    await ctx.send(embed=gembed)

當您創建一個向您發送歌詞的命令時,一些 Random API很容易處理。

這是如何使用一些隨機api來做到這一點,

# these imports are used for this particular lyrics command. the essential import here is aiohttp, which will be used to fetch the lyrics from the API
import textwrap
import urllib
import aiohttp
import datetime

@bot.command(aliases = ['l', 'lyrc', 'lyric']) # adding aliases to the command so they they can be triggered with other names
async def lyrics(ctx, *, search = None):
    """A command to find lyrics easily!"""
    if not search: # if user hasnt given an argument, throw a error and come out of the command
        embed = discord.Embed(
            title = "No search argument!",
            description = "You havent entered anything, so i couldnt find lyrics!"
        )
        return await ctx.reply(embed = embed)
        # ctx.reply is available only on discord.py version 1.6.0, if you have a version lower than that use ctx.send
    
    song = urllib.parse.quote(search) # url-encode the song provided so it can be passed on to the API
    
    async with aiohttp.ClientSession() as lyricsSession:
        async with lyricsSession.get(f'https://some-random-api.ml/lyrics?title={song}') as jsondata: # define jsondata and fetch from API
            if not 300 > jsondata.status >= 200: # if an unexpected HTTP status code is recieved from the website, throw an error and come out of the command
                return await ctx.send(f'Recieved poor status code of {jsondata.status}')

            lyricsData = await jsondata.json() # load the json data into its json form

    error = lyricsData.get('error')
    if error: # checking if there is an error recieved by the API, and if there is then throwing an error message and returning out of the command
        return await ctx.send(f'Recieved unexpected error: {error}')

    songLyrics = lyricsData['lyrics'] # the lyrics
    songArtist = lyricsData['author'] # the author's name
    songTitle = lyricsData['title'] # the song's title
    songThumbnail = lyricsData['thumbnail']['genius'] # the song's picture/thumbnail

    # sometimes the song's lyrics can be above 4096 characters, and if it is then we will not be able to send it in one single message on Discord due to the character limit
    # this is why we split the song into chunks of 4096 characters and send each part individually
    for chunk in textwrap.wrap(songLyrics, 4096, replace_whitespace = False):
        embed = discord.Embed(
            title = songTitle,
            description = chunk,
            color = discord.Color.blurple(),
            timestamp = datetime.datetime.utcnow()
        )
        embed.set_thumbnail(url = songThumbnail)
        await ctx.send(embed = embed)

這是lyricsgenius一個已知錯誤,並且有一個公開的PR 來解決這個問題: https : //github.com/johnwmillr/LyricsGenius/pull/215

這是因為lyricsgenius web 從Genius 的網站上lyricsgenius了歌詞,這意味着如果他們的網站更新, lyricsgenius將無法獲取歌詞。 這個庫已經 6 個月沒有更新了; 本身是一個網絡抓取庫意味着那種不活動會使庫嚴重不穩定。 由於該庫是根據 MIT 許可的,您可以分叉該庫並為您的項目/機器人維護最新版本。 但是,最好使用專用的 API 來獲取歌曲歌詞以保證穩定性。

此外, lyricsgenius使用同步requests庫,這意味着它會在獲取歌詞時“阻止”您的異步機器人。 這對於 Discord Bot 來說絕對是不可取的,因為您的機器人在獲取歌詞時會完全沒有響應。 考慮使用aiohttp重寫它或在調用阻塞函數時使用run_in_executor

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM