损坏的Python脚本通过Twi触发Arduino

2024-05-29 11:14:43 发布

您现在位置:Python中文网/ 问答频道 /正文

自从Twitter改变了他们的APi,我必须控制的一个原型自动售货机的脚本不再工作了,编写脚本的开发人员已经转向了更环保的牧场。在

该脚本每15秒扫描一次Twitter,搜索包含指定标签(当前设置为#sunshine)的最新tweet,并过滤掉任何转发。在

当它识别出一个新的tweet时,它会向一个Arduino发送一个信号,触发一个螺线管来分发免费的产品样本(目前是防晒霜)

这行代码似乎有问题/过时:

j =json.loads(urllib.urlopen('http://search.twitter.com/search.json?q='+searchTerm+'&result_type=recent&rpp=1&filter:retweets').read())

我已经在Twitter上注册了一个开发者帐户,所以我有了消费者的秘密和令牌代码等等,但是我仍然不知道如何用这些OAuth代码修改旧代码,使其重新工作。我已经在下面完整地复制了代码。有谁能帮我,告诉我如何让这个脚本再次运行。在

^{pr2}$

Tags: 代码脚本apijsonsearch信号开发人员twitter
1条回答
网友
1楼 · 发布于 2024-05-29 11:14:43

您发布的代码甚至没有使用twitter库。下面的代码已经过修改,实际上使用了twitter库,但是您仍然需要将twitter键放入代码中。在

from twitter import *
import time
from arduino import Arduino

##################SETUP AS REQUIRED###########################
##############################################################
#Change to suit the sample, currently at 0.2 of a second     #
vendtime = 0.2                                               #
                                                             #
#Delay Time Between each Search (never below 15 seconds)     #
delayTime = 15                                               #
#This is the search term                                     # 
searchTerm = "#sunshine"                                     #
                                                             #
A = Arduino("COM3") #This will need to be COM3               #
A.output([12]) #Output on pin 12                             #
A.output([13]) #to keep serial in use                        #
##############################################################

# Twitter keys
OAUTH_TOKEN = ""     # Access token
OAUTH_SECRET = ""    # Access token secret
CONSUMER_KEY = ""    # Consumer key
CONSUMER_SECRET = "" # Consumer secret

#to collect the first tweet without vending
first_tweet = True
#To test Twitter for consistancy 
tweet= 0
notweet= 0

# Start Twitter session
t = Twitter\
(
    auth = OAuth(OAUTH_TOKEN, OAUTH_SECRET, CONSUMER_KEY, CONSUMER_SECRET)
)

#the infinite loop
while True:
    # Print stats
    print("Number of Tweets: %d" % (tweet + notweet))
    print("Working JSON: %d" % tweet)
    print("Broken JSON: %d" % notweet)

    # Perform search
    search_results = t.search.tweets(q = searchTerm, _timeout = 60)

    #find the text and the tweet id
    tweet_failed = True
    if search_results:
        if search_results.has_key("statuses"):
            statuses = search_results["statuses"]
            if statuses:
                # Select first result
                status = statuses[0]
                if not bool(set(["id", "text"]) - set(status.keys())):
                    tweet_failed = False
                    tweet_text = status["text"]
                    tweet_id = status["id"]
                    #how many times the Json is complete
                    tweet+= 1
    if tweet_failed:
        #How many times the Json is incomplete (sometimes twitter malfunctions. About 0.1 in 100 are broken)
        notweet += 1
        continue
    else:
        if first_tweet:
            first_tweet = False
            print("First loop complete")
        else:
            #if last_id is not equal to tweet_id
            if last_id != tweet_id:
                #Tell Arduino to Vend
                #pin 12 HIGH
                A.setHigh(12)
                #Sleep for the time specified in vendtime
                time.sleep(vendtime)
                #pin 12 LOW
                A.setLow(12)

        #Make last_id equal to ID so that next time we can compare it 
        last_id = tweet_id
        #Display the tweet that triggered the vend
        print("Tweet: %s" % tweet_text)
        print("Id: %d" % tweet_id)

    print("waiting")
    A.setHigh(13)
    time.sleep(delayTime)
    A.setLow(13)

相关问题 更多 >

    热门问题