访问 Twitter API +获取直接消息Python 中的 Arduino
按照教程,我能够获取并解析我自己帐户的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从您的评论来看,听起来
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. Printlen(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 withapi.GetDirectMessages()
using thesince
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 ofif drip[0] == '#high'
.