Python スキルを練習するためのプロジェクトがあります。
- Tweepy Stream でいくつかのツイート座標を抽出するには
- それらをGoogleスプレッドシートに入れるには
- 次に、Google スプレッドシートを使用してCartoDBでマップを作成します。
私はすでにこれらすべてを独立して行うことができます。今、課題はすべてを連携させることです!:)
Google スプレッドシートを更新するには、gspreadを使用しています。
ただし、セルを更新するには、次のようにセルの行と列を示す必要があります。
worksheet.update_acell('B1', 'Bingo!')
ツイートを抽出するスクリプトにカウンターを入れようとしています。目標は、ツイートが見つかるたびに、B1 を B2、B3、B4 の順に変更することです。
しかし、それは機能していません... 座標はターミナルに出力されますが、それだけです。
想定どおりにクラスを使用していないと思います。しかし、どこが間違っているのかわかりません!
ヘルプ?
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import tweepy
import gspread
import time
CONSUMER_KEY, CONSUMER_SECRET = 'SECRET', 'SECRET'
USER_KEY, USER_SECRET = 'SECRET', 'SECRET'
class MyStream(tweepy.StreamListener):
def __init__(self):
tweepy.StreamListener.__init__(self)
# I added this to have a counter.
self.n = 2
def on_status(self, tweet):
try:
longitude = str(tweet.coordinates['coordinates'][0])
latitude = str(tweet.coordinates['coordinates'][1])
print longitude
print latitude
# I added this to update my google spreadsheet with the coordinates
self.wks.update_acell(('A' + str(n)), longitude)
self.wks.update_acell(('B' + str(n)), latitude)
print "Spreadsheet updated!"
# This is for my counter
self.n += 1
except:
pass
def main():
#I added these two lines to connect to my google spreadsheet
gc = gspread.login('EMAIL', 'PASSWORD')
wks = gc.open('SPREADSHEET_NAME').sheet1
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(USER_KEY, USER_SECRET)
stream = tweepy.Stream(auth, MyStream(), timeout=50)
stream.filter(locations=[-74.00,45.40,-73.41,45.72])
if __name__ == "__main__":
main()