Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python JSON解码器错误_Python_Json_Irc_Twitch - Fatal编程技术网

Python JSON解码器错误

Python JSON解码器错误,python,json,irc,twitch,Python,Json,Irc,Twitch,我目前正在使用一个脚本,当指定的Twitch电视频道播出时,它会向IRC广播 但由于某种原因,当它上线时,我不断地收到一个错误。但当它离线时,它工作得非常好 Python代码: # Twitch IRC status autoupdate # Create an empty file called config.yml in the same directory, containing: # host: irc.quakenet.org # port: 6667 # channels: ["#c

我目前正在使用一个脚本,当指定的Twitch电视频道播出时,它会向IRC广播

但由于某种原因,当它上线时,我不断地收到一个错误。但当它离线时,它工作得非常好

Python代码:

# Twitch IRC status autoupdate
# Create an empty file called config.yml in the same directory, containing:
# host: irc.quakenet.org
# port: 6667
# channels: ["#channel", "#anotherchannel"]
# nspass: "nickserv/Q password"
# password: "server password for BNC or private server"
# twitch: "channelname"

import requests
import yaml
from HTMLParser import HTMLParser
from sys import stdout

from twisted.internet import reactor, task, protocol
from twisted.python import log
from twisted.words.protocols import irc
from twisted.application import internet, service

with open('config.yml') as f:
    config = yaml.load(f.read())
HOST, PORT = config['host'], config['port']


def munge(inp):
    # Prevents highlight notification in most clients
    return inp[0] + u"\u200b" + inp[1:]


class TwitchProtocol(irc.IRCClient):
    password = config["password"] if "password" in config else None
    nickname = 'Twitch'
    username = 'Twitch'
    versionName = 'Twitch'
    versionNum = 'v1.0'
    realname = 'by blha303. https://gist.github.com/blha303'
    loopcall = None
    status = False


    def signedOn(self):
        for channel in self.factory.channels:
            self.join(channel)
        #Quakenet
        self._send_message("auth %s %s" % (self.nickname, config["nspass"]), "Q")
        #Nickserv
        self._send_message("identify %s %s" % (self.nickname, config["nspass"]), "NickServ")
        #Nickserv that doesn't support specifying a nickname
        self._send_message("identify %s" % config["nspass"], "NickServ")

        def restartloop(reason):
            reason.printTraceback()
            print "Loop crashed: " + reason.getErrorMessage()
            self.loopcall.start(5.0).addErrback(restartloop)
        self.loopcall = task.LoopingCall(self.getTwitchStatus)
        self.loopcall.start(5.0).addErrback(restartloop)

    def getTwitchStatus(self):
        channel = config["twitch"]
        data = requests.get("http://api.justin.tv/api/stream/list.json?channel=" + channel).json()
        if data and not self.status:
            fmt = "{}: {} {} ({})"
            self.status = True
            title = data[0]['title']
            playing = ("playing " + data[0]['meta_game']) if "meta_game" in data[0] else ""
            viewers = "\x033\x02Online now!\x02\x0f " + str(data[0]["channel_count"]) + " viewer"
            print viewers
            viewers = viewers + "s" if not " 1 view" in viewers else viewers
            print viewers
            h = HTMLParser()
            for ch in self.factory.channels:
                self._send_message(h.unescape(fmt.format(title, munge(channel), playing, viewers)), ch)
        elif not data and self.status:
            self.status = False
            for ch in self.factory.channels:
                self._send_message("%s is now offline." % channel, ch)


    def privmsg(self, user, channel, message):
        nick, _, host = user.partition('!')
        print "<%s> %s" % (nick, message)


    def _send_message(self, msg, target, nick=None):
        if nick:
            msg = '%s, %s' % (nick, msg)
        self.msg(target, msg)
        print "<%s> %s" % (self.nickname, msg)


class TwitchFactory(protocol.ReconnectingClientFactory):
    protocol = TwitchProtocol
    channels = config["channels"]

if __name__ == '__main__':
    reactor.connectTCP(HOST, PORT, TwitchFactory())
    log.startLogging(stdout)
    reactor.run()

elif __name__ == '__builtin__':
    application = service.Application('Twitch')
    ircService = internet.TCPClient(HOST, PORT, TwitchFactory())
    ircService.setServiceParent(application)
#Twitch IRC状态自动更新
#在同一目录中创建名为config.yml的空文件,其中包含:
#主持人:irc.quakenet.org
#港口:6667
#频道:[“#频道”,“另一频道”]
#nspass:“nickserv/Q密码”
#密码:“BNC或专用服务器的服务器密码”
#twitch:“频道名称”
导入请求
进口yaml
从HTMLPasser导入HTMLPasser
从系统导入标准输出
从twisted.internet导入反应器、任务、协议
从twisted.python导入日志
从twisted.words.protocols导入irc
从twisted.application导入internet、服务
将open('config.yml')作为f:
config=yaml.load(f.read())
主机,端口=config['HOST'],config['PORT']
def munge(inp):
#防止在大多数客户端中突出显示通知
返回inp[0]+u“\u200b”+inp[1:]
TwitchProtocol类(irc.IRCClient):
password=config[“password”]如果config else中的“password”为无
昵称='Twitch'
用户名='Twitch'
versionName='Twitch'
versionNum='v1.0'
blha303的realname='。https://gist.github.com/blha303'
loopcall=None
状态=错误
def签名(自我):
对于self.factory.channels中的通道:
加入(频道)
#夸克内特
self.\u发送消息(“身份验证%s%s”%(self.昵称,配置[“nspass”],“Q”)
#尼克瑟夫
self.\u发送消息(“标识%s%s”%(self.昵称,配置[“nspass”]),“NickServ”)
#不支持指定昵称的Nickserv
self.\u发送消息(“标识%s”%config[“nspass”],“NickServ”)
def重新启动循环(原因):
原因。打印回溯()
打印“循环崩溃:”+reason.getErrorMessage()
self.loopcall.start(5.0).addErrback(restartloop)
self.loopcall=task.LoopingCall(self.getWitchStatus)
self.loopcall.start(5.0).addErrback(restartloop)
def状态(自我):
通道=配置[“twitch”]
数据=请求。获取(“http://api.justin.tv/api/stream/list.json?channel=“+channel.json()
如果是数据而不是自我状态:
fmt=“{}:{}{}({})”
self.status=True
title=数据[0]['title']
playing=(“playing”+数据[0]['meta_game'])如果数据[0]中的“meta_game”为else“”
viewers=“\x033\x02立即联机!\x02\x0f”+str(数据[0][“频道计数”])+“查看器”
打印查看器
viewers=viewers+“s”如果不是viewers else viewers中的“1视图”
打印查看器
h=HTMLParser()
对于ch in self.factory.channels:
self.\u发送消息(h.unescape(fmt.格式(标题、频道、播放、观众)),ch)
elif非数据和自我状态:
self.status=False
对于ch in self.factory.channels:
self.\u发送消息(“%s现在脱机。”%channel,ch)
def privmsg(自身、用户、频道、消息):
尼克,主机=user.partition(“!”)
打印“%s%”(尼克,消息)
定义发送消息(self、msg、target、nick=None):
如果尼克:
消息=“%s,%s%”(尼克,消息)
self.msg(target,msg)
打印“%s%”(self.昵称,msg)
TwitchFactory类(协议重新连接ClientFactory):
协议=TwitchProtocol
通道=配置[“通道”]
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
connectTCP(主机、端口、TwitchFactory())
日志记录(标准输出)
反应堆运行()
elif uuuu name uuuuuuu='.'内置':
应用程序=服务。应用程序('Twitch')
ircService=internet.TCPClient(主机、端口、TwitchFactory())
ircService.setServiceParent(应用程序)
但当它发现twitch电视频道正在播放时,它会给出一个错误

Error:
2013-12-08 22:42:12+0000 [-] Traceback (most recent call last):
2013-12-08 22:42:12+0000 [-]   File "/usr/lib/python2.7/dist-packages/twisted/internet/base.py", line 1192, in run
2013-12-08 22:42:12+0000 [-]     self.mainLoop()
2013-12-08 22:42:12+0000 [-]   File "/usr/lib/python2.7/dist-packages/twisted/internet/base.py", line 1201, in mainLoop
2013-12-08 22:42:12+0000 [-]     self.runUntilCurrent()
2013-12-08 22:42:12+0000 [-]   File "/usr/lib/python2.7/dist-packages/twisted/internet/base.py", line 824, in runUntilCurrent
2013-12-08 22:42:12+0000 [-]     call.func(*call.args, **call.kw)
2013-12-08 22:42:12+0000 [-]   File "/usr/lib/python2.7/dist-packages/twisted/internet/task.py", line 218, in __call__
2013-12-08 22:42:12+0000 [-]     d = defer.maybeDeferred(self.f, *self.a, **self.kw)
2013-12-08 22:42:12+0000 [-] --- <exception caught here> ---
2013-12-08 22:42:12+0000 [-]   File "/usr/lib/python2.7/dist-packages/twisted/internet/defer.py", line 137, in maybeDeferred
2013-12-08 22:42:12+0000 [-]     result = f(*args, **kw)
2013-12-08 22:42:12+0000 [-]   File "irctwitch.py", line 60, in getTwitchStatus
2013-12-08 22:42:12+0000 [-]     data = requests.get("http://api.justin.tv/api/stream/list.json?channel=" + channel).json()
2013-12-08 22:42:12+0000 [-]   File "/usr/lib/python2.7/dist-packages/requests/models.py", line 651, in json
2013-12-08 22:42:12+0000 [-]     return json.loads(self.text or self.content, **kwargs)
2013-12-08 22:42:12+0000 [-]   File "/usr/lib/python2.7/json/__init__.py", line 338, in loads
2013-12-08 22:42:12+0000 [-]     return _default_decoder.decode(s)
2013-12-08 22:42:12+0000 [-]   File "/usr/lib/python2.7/json/decoder.py", line 365, in decode
2013-12-08 22:42:12+0000 [-]     obj, end = self.raw_decode(s, idx=_w(s, 0).end())
2013-12-08 22:42:12+0000 [-]   File "/usr/lib/python2.7/json/decoder.py", line 383, in raw_decode
2013-12-08 22:42:12+0000 [-]     raise ValueError("No JSON object could be decoded")
2013-12-08 22:42:12+0000 [-] exceptions.ValueError: No JSON object could be decoded
2013-12-08 22:42:12+0000 [-] Loop crashed: No JSON object could be decoded
错误:
2013-12-08 22:42:12+0000[-]回溯(最近一次呼叫最后一次):
2013-12-08 22:42:12+0000[-]文件“/usr/lib/python2.7/dist-packages/twisted/internet/base.py”,第1192行,运行中
2013-12-08 22:42:12+0000[-]self.mainLoop()
2013-12-08 22:42:12+0000[-]文件“/usr/lib/python2.7/dist-packages/twisted/internet/base.py”,第1201行,在mainLoop中
2013-12-08 22:42:12+0000[-]自运行电流()
2013-12-08 22:42:12+0000[-]文件“/usr/lib/python2.7/dist-packages/twisted/internet/base.py”,第824行,在rununtillcurrent中
2013-12-08 22:42:12+0000[-]call.func(*call.args,**call.kw)
2013-12-08 22:42:12+0000[-]文件“/usr/lib/python2.7/dist-packages/twisted/internet/task.py”,第218行,在调用中__
2013-12-08 22:42:12+0000[-]d=延迟可能会出错(self.f、*self.a、**self.kw)
2013-12-08 22:42:12+0000 [-] ---  ---
2013-12-08 22:42:12+0000[-]文件“/usr/lib/python2.7/dist-packages/twisted/internet/defer.py”,第137行,格式为maybeDeferred
2013-12-08 22:42:12+0000[-]结果=f(*args,**kw)
2013-12-08 22:42:12+0000[-]文件“irctwitch.py”,第60行,处于GetSwitch状态
2013-12-08 22:42:12+0000[-]数据=请求。获取(“http://api.justin.tv/api/stream/list.json?channel=“+channel.json()
2013-12-08 22:42:12+0000[-]文件“/usr/lib/python2.7/dist-packages/requests/models.py”,第651行,json格式
2013-12-08 22:42:12+0000[-]返回json.loads(self.text或self.content,**kwargs)
2013-12-08 22:42:12+0000[-]文件“/usr/lib/python2.7/json/_init__.py”,第338行,加载
2013-12-08 22:42:12+0000[-]返回默认解码器解码
2013-12-08 22:42:12+0000[-]文件“/usr/lib/python2.7/json/decoder.py”,第365行,解码中
2013-12-08 22:42:12+0000[-]obj,end=self.raw_decode(s,idx=_w(s,0.end())
2013-12-08 22:42:12+0000[-]文件“/usr/lib/python2.7/json/decoder.py”,第383行,原始解码
2013-12-08 22:42:12+0000[-]raise VALUERROR(“无法解码JSON对象”)
2013-12-08 22:42:12+0000[-]异常。ValueError:无法解码任何JSON对象
2013-12-
2013-12-08 22:42:12+0000 [-]   File "/usr/lib/python2.7/json/decoder.py", line 383, in raw_decode
2013-12-08 22:42:12+0000 [-]     raise ValueError("No JSON object could be decoded")
2013-12-08 22:42:12+0000 [-] exceptions.ValueError: No JSON object could be decoded
2013-12-08 22:42:12+0000 [-]   File "irctwitch.py", line 60, in getTwitchStatus
2013-12-08 22:42:12+0000 [-]     data = requests.get("http://api.justin.tv/api/stream/list.json?channel=" + channel).json()