首页 > 解决方案 > 我有错误 TypeError: Object of type method is not JSON serializable

问题描述

所以,我正在制作这个脚本,使用 Last.FM API 检查一周中播放次数最多的歌曲。我用 Python 制作了这个脚本:

import discord
import datetime
from discord import member
from discord.ext import commands, tasks
from discord.utils import get
from datetime import datetime
import threading
import requests
import json
from requests.api import request

class MostPlayer(commands.Cog):
    def __init__(self, bot):
        self.bot = bot
        self.lastfmmostplayed.start()

    @tasks.loop(seconds=1)
    async def lastfmmostplayed(self):
        await self.bot.wait_until_ready()
        now = datetime.now()
        dt_string = now.strftime("%a %H:%M:%S")
        if(dt_string == 'Sat 17:47:00'):
            print("Time for a new song suggestion!")
            tokens = open('./hidden/tokens.json')
            tokenimport = json.load(tokens)
            lastfmtoken = tokenimport['lastfmtoken']
            # Album Chart - No. 1
            requesturl_no1album = requests.get('http://ws.audioscrobbler.com/2.0/?method=user.getweeklyalbumchart&user=QuinnTT&api_key=' + lastfmtoken + '&format=json')
            parsed = requesturl_no1album.json()
            album = parsed['weeklyalbumchart']['album']
            sorted_album = sorted(album, key = lambda song:song["playcount"])
            most_listened_song = sorted_album[-1] # last element is the one with the biggest playcount
            album_chart_artist = most_listened_song['artist']['#text']
            album_chart_name = most_listened_song['name']
            album_chart_playcount = most_listened_song['playcount']
            album_chart_url = most_listened_song['url']
            *_, album_chart_artist_url, album_chart_name_url = album_chart_url.split("/")
            no1_info_url = ('http://ws.audioscrobbler.com/2.0/?method=album.getinfo&api_key=' + lastfmtoken + '&artist=' + album_chart_artist_url + '&album=' + album_chart_name_url + '&format=json')
            request_no_info = (requests.get(no1_info_url)).json
            album_data = json.dumps(request_no_info)
            images = album_data['album']['image']
            mega_images = [i for i in images if i['size'] == 'mega']
            try:
                mega_image_url = mega_images[0]['#text']
            except IndexError:
                mega_image_url = ""
            embed_album=discord.Embed(title="Quinn's Album of the Week:", description=f"{album_chart_name} by {album_chart_artist}", color=0x9d30ab)
            embed_album.set_footer(text=f"Quinn listened {album_chart_playcount}x to this album last week!")
            embed_album.set_image(url=mega_image_url)
            await self.bot.get_channel(850308124493086730).send(embed=embed_album)

        else:
            return

def setup(bot):
    bot.add_cog(MostPlayer(bot))

但是,当我运行它时,我收到以下错误:

TypeError:类型方法的对象不是 JSON 可序列化的

我不知道错误是什么,所以这就是我在这里问的原因。以防万一,这里是从 Last.FM 获取的 JSON(第 38 行)。代码应该获取专辑图像的链接,以便我稍后可以在 Discord.py 嵌入中使用它。

{
    "album": {
        "name": "Significant Other",
        "artist": "Limp Bizkit",
        "mbid": "be3e00aa-368a-3f09-ac96-cd094e9a7151",
        "url": "https://www.last.fm/music/Limp+Bizkit/Significant+Other",
        "image": [{
            "#text": "https://lastfm.freetls.fastly.net/i/u/34s/1c00f7b9cd94c2b6fbd7f12fc00bd8d2.png",
            "size": "small"
        }, {
            "#text": "https://lastfm.freetls.fastly.net/i/u/64s/1c00f7b9cd94c2b6fbd7f12fc00bd8d2.png",
            "size": "medium"
        }, {
            "#text": "https://lastfm.freetls.fastly.net/i/u/174s/1c00f7b9cd94c2b6fbd7f12fc00bd8d2.png",
            "size": "large"
        }, {
            "#text": "https://lastfm.freetls.fastly.net/i/u/300x300/1c00f7b9cd94c2b6fbd7f12fc00bd8d2.png",
            "size": "extralarge"
        }, {
            "#text": "https://lastfm.freetls.fastly.net/i/u/300x300/1c00f7b9cd94c2b6fbd7f12fc00bd8d2.png",
            "size": "mega"
        }, {
            "#text": "https://lastfm.freetls.fastly.net/i/u/300x300/1c00f7b9cd94c2b6fbd7f12fc00bd8d2.png",
            "size": ""
        }],
        "listeners": "774409",
        "playcount": "10274552",
        "tracks": {
            "track": [{
                "name": "Intro",
                "url": "https://www.last.fm/music/Limp+Bizkit/_/Intro",
                "duration": "78",
                "@attr": {
                    "rank": "1"
                },
                "streamable": {
                    "#text": "0",
                    "fulltrack": "0"
                },
                "artist": {
                    "name": "Limp Bizkit",
                    "mbid": "8f9d6bb2-dba4-4cca-9967-cc02b9f4820c",
                    "url": "https://www.last.fm/music/Limp+Bizkit"
                }
            }, {
                "name": "Just Like This",
                "url": "https://www.last.fm/music/Limp+Bizkit/_/Just+Like+This",
                "duration": "215",
                "@attr": {
                    "rank": "2"
                },
                "streamable": {
                    "#text": "0",
                    "fulltrack": "0"
                },
                "artist": {
                    "name": "Limp Bizkit",
                    "mbid": "8f9d6bb2-dba4-4cca-9967-cc02b9f4820c",
                    "url": "https://www.last.fm/music/Limp+Bizkit"
                }
            }, {
                "name": "Nookie",
                "url": "https://www.last.fm/music/Limp+Bizkit/_/Nookie",
                "duration": "289",
                "@attr": {
                    "rank": "3"
                },
                "streamable": {
                    "#text": "0",
                    "fulltrack": "0"
                },
                "artist": {
                    "name": "Limp Bizkit",
                    "mbid": "8f9d6bb2-dba4-4cca-9967-cc02b9f4820c",
                    "url": "https://www.last.fm/music/Limp+Bizkit"
                }
            }, {
                "name": "Break Stuff",
                "url": "https://www.last.fm/music/Limp+Bizkit/_/Break+Stuff",
                "duration": "166",
                "@attr": {
                    "rank": "4"
                },
                "streamable": {
                    "#text": "0",
                    "fulltrack": "0"
                },
                "artist": {
                    "name": "Limp Bizkit",
                    "mbid": "8f9d6bb2-dba4-4cca-9967-cc02b9f4820c",
                    "url": "https://www.last.fm/music/Limp+Bizkit"
                }
            }, {
                "name": "Re-Arranged",
                "url": "https://www.last.fm/music/Limp+Bizkit/_/Re-Arranged",
                "duration": "354",
                "@attr": {
                    "rank": "5"
                },
                "streamable": {
                    "#text": "0",
                    "fulltrack": "0"
                },
                "artist": {
                    "name": "Limp Bizkit",
                    "mbid": "8f9d6bb2-dba4-4cca-9967-cc02b9f4820c",
                    "url": "https://www.last.fm/music/Limp+Bizkit"
                }
            }, {
                "name": "I'm Broke",
                "url": "https://www.last.fm/music/Limp+Bizkit/_/I%27m+Broke",
                "duration": "239",
                "@attr": {
                    "rank": "6"
                },
                "streamable": {
                    "#text": "0",
                    "fulltrack": "0"
                },
                "artist": {
                    "name": "Limp Bizkit",
                    "mbid": "8f9d6bb2-dba4-4cca-9967-cc02b9f4820c",
                    "url": "https://www.last.fm/music/Limp+Bizkit"
                }
            }, {
                "name": "Nobody Like You",
                "url": "https://www.last.fm/music/Limp+Bizkit/_/Nobody+Like+You",
                "duration": "260",
                "@attr": {
                    "rank": "7"
                },
                "streamable": {
                    "#text": "0",
                    "fulltrack": "0"
                },
                "artist": {
                    "name": "Limp Bizkit",
                    "mbid": "8f9d6bb2-dba4-4cca-9967-cc02b9f4820c",
                    "url": "https://www.last.fm/music/Limp+Bizkit"
                }
            }, {
                "name": "Don't Go Off Wandering",
                "url": "https://www.last.fm/music/Limp+Bizkit/_/Don%27t+Go+Off+Wandering",
                "duration": "239",
                "@attr": {
                    "rank": "8"
                },
                "streamable": {
                    "#text": "0",
                    "fulltrack": "0"
                },
                "artist": {
                    "name": "Limp Bizkit",
                    "mbid": "8f9d6bb2-dba4-4cca-9967-cc02b9f4820c",
                    "url": "https://www.last.fm/music/Limp+Bizkit"
                }
            }, {
                "name": "9 Teen 90 Nine",
                "url": "https://www.last.fm/music/Limp+Bizkit/_/9+Teen+90+Nine",
                "duration": "276",
                "@attr": {
                    "rank": "9"
                },
                "streamable": {
                    "#text": "0",
                    "fulltrack": "0"
                },
                "artist": {
                    "name": "Limp Bizkit",
                    "mbid": "8f9d6bb2-dba4-4cca-9967-cc02b9f4820c",
                    "url": "https://www.last.fm/music/Limp+Bizkit"
                }
            }, {
                "name": "N 2 Gether Now",
                "url": "https://www.last.fm/music/Limp+Bizkit/_/N+2+Gether+Now",
                "duration": "289",
                "@attr": {
                    "rank": "10"
                },
                "streamable": {
                    "#text": "0",
                    "fulltrack": "0"
                },
                "artist": {
                    "name": "Limp Bizkit",
                    "mbid": "8f9d6bb2-dba4-4cca-9967-cc02b9f4820c",
                    "url": "https://www.last.fm/music/Limp+Bizkit"
                }
            }, {
                "name": "Trust?",
                "url": "https://www.last.fm/music/Limp+Bizkit/_/Trust%3F",
                "duration": "299",
                "@attr": {
                    "rank": "11"
                },
                "streamable": {
                    "#text": "0",
                    "fulltrack": "0"
                },
                "artist": {
                    "name": "Limp Bizkit",
                    "mbid": "8f9d6bb2-dba4-4cca-9967-cc02b9f4820c",
                    "url": "https://www.last.fm/music/Limp+Bizkit"
                }
            }, {
                "name": "No Sex",
                "url": "https://www.last.fm/music/Limp+Bizkit/_/No+Sex",
                "duration": "234",
                "@attr": {
                    "rank": "12"
                },
                "streamable": {
                    "#text": "0",
                    "fulltrack": "0"
                },
                "artist": {
                    "name": "Limp Bizkit",
                    "mbid": "8f9d6bb2-dba4-4cca-9967-cc02b9f4820c",
                    "url": "https://www.last.fm/music/Limp+Bizkit"
                }
            }, {
                "name": "Show Me What You Got",
                "url": "https://www.last.fm/music/Limp+Bizkit/_/Show+Me+What+You+Got",
                "duration": "266",
                "@attr": {
                    "rank": "13"
                },
                "streamable": {
                    "#text": "0",
                    "fulltrack": "0"
                },
                "artist": {
                    "name": "Limp Bizkit",
                    "mbid": "8f9d6bb2-dba4-4cca-9967-cc02b9f4820c",
                    "url": "https://www.last.fm/music/Limp+Bizkit"
                }
            }, {
                "name": "A Lesson Learned",
                "url": "https://www.last.fm/music/Limp+Bizkit/_/A+Lesson+Learned",
                "duration": "160",
                "@attr": {
                    "rank": "14"
                },
                "streamable": {
                    "#text": "0",
                    "fulltrack": "0"
                },
                "artist": {
                    "name": "Limp Bizkit",
                    "mbid": "8f9d6bb2-dba4-4cca-9967-cc02b9f4820c",
                    "url": "https://www.last.fm/music/Limp+Bizkit"
                }
            }, {
                "name": "Outro / Radio Sucks / The Mind of Les",
                "url": "https://www.last.fm/music/Limp+Bizkit/_/Outro+%2F+Radio+Sucks+%2F+The+Mind+of+Les",
                "duration": "438",
                "@attr": {
                    "rank": "15"
                },
                "streamable": {
                    "#text": "0",
                    "fulltrack": "0"
                },
                "artist": {
                    "name": "Limp Bizkit",
                    "mbid": "8f9d6bb2-dba4-4cca-9967-cc02b9f4820c",
                    "url": "https://www.last.fm/music/Limp+Bizkit"
                }
            }, {
                "name": "[silence]",
                "url": "https://www.last.fm/music/Limp+Bizkit/_/%5Bsilence%5D",
                "duration": "4",
                "@attr": {
                    "rank": "16"
                },
                "streamable": {
                    "#text": "0",
                    "fulltrack": "0"
                },
                "artist": {
                    "name": "Limp Bizkit",
                    "mbid": "8f9d6bb2-dba4-4cca-9967-cc02b9f4820c",
                    "url": "https://www.last.fm/music/Limp+Bizkit"
                }
            }]
        },
        "tags": {
            "tag": [{
                "name": "albums I own",
                "url": "https://www.last.fm/tag/albums+I+own"
            }, {
                "name": "Nu Metal",
                "url": "https://www.last.fm/tag/Nu+Metal"
            }, {
                "name": "rapcore",
                "url": "https://www.last.fm/tag/rapcore"
            }, {
                "name": "rock",
                "url": "https://www.last.fm/tag/rock"
            }, {
                "name": "limp bizkit",
                "url": "https://www.last.fm/tag/limp+bizkit"
            }]
        },
        "wiki": {
            "published": "11 Jul 2016, 20:19",
            "summary": "Significant Other is the second album by American rap rock/nu metal band Limp Bizkit. Released in 1999 by Flip/Interscope Records, the album saw the band expanding its sound from that of its debut album Three Dollar Bill, Yall, to incorporate further metal and hip hop influences. Significant Other was co-produced by Terry Date and Limp Bizkit. The album has sold at least 16 million copies worldwide. <a href=\"http://www.last.fm/music/Limp+Bizkit/Significant+Other\">Read more on Last.fm</a>.",
            "content": "Significant Other is the second album by American rap rock/nu metal band Limp Bizkit. Released in 1999 by Flip/Interscope Records, the album saw the band expanding its sound from that of its debut album Three Dollar Bill, Yall, to incorporate further metal and hip hop influences. Significant Other was co-produced by Terry Date and Limp Bizkit. The album has sold at least 16 million copies worldwide. <a href=\"http://www.last.fm/music/Limp+Bizkit/Significant+Other\">Read more on Last.fm</a>. User-contributed text is available under the Creative Commons By-SA License; additional terms may apply."
        }
    }
}

标签: pythonjsondiscord.pytypeerrorlast.fm

解决方案


推荐阅读