访问 Twitter API +获取直接消息Python 中的 Arduino

发布于 2024-11-02 12:47:53 字数 3728 浏览 4 评论 0原文

按照教程,我能够获取并解析我自己帐户的 Twitter 流,并通过串行连接发送此信息以与我的 Arduino 项目进行交互。但是,我需要接收直接发送给我的消息,而不是我的公共源。下面是获取公共源的代码,然后是我尝试获取直接消息的代码。

我怎样才能实现这个目标?

##Import Libraries

import twitter
import serial
import time

##authenticate yourself with twitter
api = twitter.Api(consumer_key='*************', consumer_secret='**************', access_token_key='************', access_token_secret='*************')

##set to your serial port
ser = serial.Serial('/dev/tty.usbmodem1a21', 19200)

## check serial port
def checkokay():
    ser.flushInput()
    time.sleep(3)
    line=ser.readline()
    time.sleep(3)

    if line == ' ':
            line=ser.readline()
    print 'here'

## Welcome message
print 'Starting Wooden Wave!'

def driptwit():
    status = []
    x = 0

    status = api.GetUserTimeline('wooden_wave') ##grab latest statuses

    checkIt = [s.text for s in status] ##put status in an array

    drip = checkIt[0].split() ##split first tweet into words

    ## check for match and write to serial if match
    if drip[0] == '#high':
        print 'Tweet Recieved, raise block high.'
        ser.write('1')
    elif drip[0] == '#low': ##break if done
        ser.write('0')
        print 'Tweet Recieved, raise block low.'
    elif drip[0] == '#light': ##break if done
        ser.write('2')
        print 'Tweet Recieved, turn on LED.'
    else:
    ser.write('0')
    print 'Awaiting Tweet'

while 1:
driptwit() ## call driptwit function
time.sleep(15) ## sleep for 15 seconds to avoid rate limiting

尝试获取直接消息:

##Import Libraries

import twitter
import serial
import time

##authenticate yourself with twitter
api = twitter.Api(consumer_key='***********', consumer_secret='***********', access_token_key='***********', access_token_secret='***********')

##set to your serial port
ser = serial.Serial('/dev/tty.usbmodem1a21', 19200)

## check serial port
def checkokay():
    ser.flushInput()
    time.sleep(3)
    line=ser.readline()
    time.sleep(3)

    if line == ' ':
            line=ser.readline()
    print 'here'

## Welcome message
print 'Starting Wooden Wave!'

def driptwit():
status = []
x = 0

status = api.GetDirectMessages() ##grab latest statuses

checkIt = [s.text for s in status] ##put status in an array

drip = checkIt[0].split() ##split first tweet into words

## check for match and write to serial if match
if drip[0] == '#high':
    print 'Tweet Recieved, raise block high.'
    ser.write('1')
elif drip[0] == '#low': ##break if done
    ser.write('0')
    print 'Tweet Recieved, raise block low.'
elif drip[0] == '#light': ##break if done
    ser.write('2')
    print 'Tweet Recieved, turn on LED.'
else:
    ser.write('0')
    print 'Awaiting Tweet'

while 1:
    driptwit() ## call driptwit function
    time.sleep(15) ## sleep for 15 seconds to avoid rate limiting

//// 更新 //

def driptwit():
    status = []
    x = 0

    status = api.GetMentions() ##grab latest statuses
    print [s.text for s in status]
    checkIt = [s.text for s in status] ##put status in an array

    drip = checkIt[0].split() ##split first tweet into words

    ## check for match and write to serial if match
    if drip[0] == '#high':
        print 'Tweet Recieved, raise block high.'
        ser.write('1')
    elif drip[0] == '#low': ##break if done
        ser.write('0')
        print 'Tweet Recieved, raise block low.'
    elif drip[0] == '#light': ##break if done
        ser.write('2')
        print 'Tweet Recieved, turn on LED.'
    else:
        ser.write('0')
        print 'Awaiting Tweet'

while 1:
    driptwit() ## call driptwit function
    time.sleep(15) ## sleep for 15 seconds to avoid rate limiting

Following a tutorial I was able to get and parse the Twitter stream for my own account and send this information through the serial connection to interact with my Arduino project. However I need to get messages sent directly to me and not my public feed. Below is the code to get the public feed then my attempt at trying to get my direct messages.

How can I achieve this?

##Import Libraries

import twitter
import serial
import time

##authenticate yourself with twitter
api = twitter.Api(consumer_key='*************', consumer_secret='**************', access_token_key='************', access_token_secret='*************')

##set to your serial port
ser = serial.Serial('/dev/tty.usbmodem1a21', 19200)

## check serial port
def checkokay():
    ser.flushInput()
    time.sleep(3)
    line=ser.readline()
    time.sleep(3)

    if line == ' ':
            line=ser.readline()
    print 'here'

## Welcome message
print 'Starting Wooden Wave!'

def driptwit():
    status = []
    x = 0

    status = api.GetUserTimeline('wooden_wave') ##grab latest statuses

    checkIt = [s.text for s in status] ##put status in an array

    drip = checkIt[0].split() ##split first tweet into words

    ## check for match and write to serial if match
    if drip[0] == '#high':
        print 'Tweet Recieved, raise block high.'
        ser.write('1')
    elif drip[0] == '#low': ##break if done
        ser.write('0')
        print 'Tweet Recieved, raise block low.'
    elif drip[0] == '#light': ##break if done
        ser.write('2')
        print 'Tweet Recieved, turn on LED.'
    else:
    ser.write('0')
    print 'Awaiting Tweet'

while 1:
driptwit() ## call driptwit function
time.sleep(15) ## sleep for 15 seconds to avoid rate limiting

Attempt at gaining direct messages:

##Import Libraries

import twitter
import serial
import time

##authenticate yourself with twitter
api = twitter.Api(consumer_key='***********', consumer_secret='***********', access_token_key='***********', access_token_secret='***********')

##set to your serial port
ser = serial.Serial('/dev/tty.usbmodem1a21', 19200)

## check serial port
def checkokay():
    ser.flushInput()
    time.sleep(3)
    line=ser.readline()
    time.sleep(3)

    if line == ' ':
            line=ser.readline()
    print 'here'

## Welcome message
print 'Starting Wooden Wave!'

def driptwit():
status = []
x = 0

status = api.GetDirectMessages() ##grab latest statuses

checkIt = [s.text for s in status] ##put status in an array

drip = checkIt[0].split() ##split first tweet into words

## check for match and write to serial if match
if drip[0] == '#high':
    print 'Tweet Recieved, raise block high.'
    ser.write('1')
elif drip[0] == '#low': ##break if done
    ser.write('0')
    print 'Tweet Recieved, raise block low.'
elif drip[0] == '#light': ##break if done
    ser.write('2')
    print 'Tweet Recieved, turn on LED.'
else:
    ser.write('0')
    print 'Awaiting Tweet'

while 1:
    driptwit() ## call driptwit function
    time.sleep(15) ## sleep for 15 seconds to avoid rate limiting

//// UPDATE //

def driptwit():
    status = []
    x = 0

    status = api.GetMentions() ##grab latest statuses
    print [s.text for s in status]
    checkIt = [s.text for s in status] ##put status in an array

    drip = checkIt[0].split() ##split first tweet into words

    ## check for match and write to serial if match
    if drip[0] == '#high':
        print 'Tweet Recieved, raise block high.'
        ser.write('1')
    elif drip[0] == '#low': ##break if done
        ser.write('0')
        print 'Tweet Recieved, raise block low.'
    elif drip[0] == '#light': ##break if done
        ser.write('2')
        print 'Tweet Recieved, turn on LED.'
    else:
        ser.write('0')
        print 'Awaiting Tweet'

while 1:
    driptwit() ## call driptwit function
    time.sleep(15) ## sleep for 15 seconds to avoid rate limiting

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乖乖 2024-11-09 12:47:53

从您的评论来看,听起来 api.GetDirectMessages() 返回了一个空列表。在 API 调用后直接打印 len(status) 以验证这一点。您确定有新的私信可供阅读吗?您是否尝试过使用 since 参数通过 api.GetDirectMessages() 设置特定日期?

不要只检查第一个标记中的哈希标签,而是检查整个字符串。换句话说,测试 if '#high' in Drop 而不是 if Drop[0] == '#high'

From your comment, it sounds like api.GetDirectMessages() returned an empty list. Print len(status) directly after the API call to verify that. Are you sure there are new direct messages available to read? Have you tried setting a specific date with api.GetDirectMessages() using the since parameter?

Instead of checking for hash tags in the first token only, check the entire string. In other words test if '#high' in drip instead of if drip[0] == '#high'.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文