简体   繁体   English

我的Python脚本没有给我错误或显示任何输出

[英]My Python script doesn't give me an error or shows any output

I'm creating a simple transit twitter-bot which posts a tweet to my API, then grabs the result to later on reply with an answer on travel times and such. 我正在创建一个简单的公交twitter-bot,该机器人将推文发布到我的API,然后获取结果以供稍后答复,并提供旅行时间等信息。 All the magic is on the server-side , and this code should work just fine. 所有的魔术都在服务器端,并且此代码应该可以正常工作。 Here's how: 这是如何做:

A user composes like the tweet below: 用户撰写如下推文:

@kollektiven Sundsvall Navet - Ljustadalen

My script removes the @kollektiven from the tweet, send the rest Sundsvall Navet - Ljustadalen to our API. 我的脚本从推文中删除了@kollektiven ,将其余的Sundsvall Navet - Ljustadalen发送给我们的API。 Then a JSON should be given to the script. 然后应为脚本提供JSON。 The script should later on reply you with an answer like this: 该脚本稍后应以如下答案答复您:

@jackbillstrom Sundsvall busstation Navet (2014-01-08 20:45) till Ljustadalen centrum (Sundsvall kn) (2014-01-08 20:59)

But it doesn't. 但事实并非如此。 I'm using this code from github called spritzbot . 我正在使用来自github的名为spritzbot的代码。 I edited the extensions/hello.py to look like the one below: 我将扩展名/hello.py编辑为如下所示:

# -*- coding: utf-8 -*-

import json, urllib2, os

os.system("clear")

def process_mention(status, settings):
    print status.user.screen_name,':', status.text.encode('utf-8')
    urlencode = status.text.lower().replace(" ","%20") # URL-encoding
    tweet = urlencode.strip('@kollektiven ')
    try:
        call = "http://xn--datorkraftfrvrlden-xtb17a.se/kollektiven/proxy.php?input="+tweet # Endpoint
        endpoint = urllib2.urlopen(call) # GET-Request to API endpoint
        data = json.load(endpoint) # Load JSON
        answer = data['proxyOutput'] # The answer from the API
        return dict(response=str(answer)) # Posts answer tweet 

    except:
        return dict(response="Error, kontakta @jackbillstrom") # Error-meddelande

What is causing this problem? 是什么导致此问题? And why? 又为什么呢? I made some changes before I came to this revision, and it worked back then. 在进行此修订之前,我进行了一些更改,然后该版本可以正常工作。

You need: 你需要:

if __name__ == '__main__':
    process_mention(...)
    ...

您无需在任何地方调用process_mention ,只需对其进行定义即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM