2013-09-25 33 views
-2

我正在嘗試放置一個腳本,該腳本會查看某些關鍵字的Twitter,拍攝一張照片,然後回覆圖片的初始推文。爲了保持一切正常,我想使用初始推文的唯一ID作爲圖片的文件名。我認爲我很接近,但我無法弄清楚如何使它工作。下面是代碼:使用推文ID作爲文件名

import sys 
import tweepy 
import time 
import threading 
import subprocess 

consumer_key="X" 
consumer_secret="X" 
access_key = "X" 
access_secret = "X" 

auth = tweepy.OAuthHandler(consumer_key, consumer_secret) 
auth.set_access_token(access_key, access_secret) 
api = tweepy.API(auth) 

class Timer(threading.Thread): 
    def __init__(self, seconds): 
     self.runTime = seconds 
     threading.Thread.__init__(self) 
    def run(self): 
     time.sleep(self.runTime) 

class CountDownTimer(Timer): 
    def run(self): 
     counter = self.runTime 
     for sec in range(self.runTime): 
      print counter 
      time.sleep(1.0) 
      counter -= 1 

class CountDownExec(CountDownTimer): 
    def __init__(self, seconds, action): 
     self.action = action 
     CountDownTimer.__init__(self, seconds) 
    def run(self): 
     CountDownTimer.run(self) 
     self.action() 

def takePicture(): 
    new_tweet = CustomStreamListener(status_info) 
    subprocess.Popen(['raspistill', '-o', '{}.jpg'.format(new_tweet.id), '-t', '0']) 

c = CountDownExec(5, takePicture) 

class CustomStreamListener(tweepy.StreamListener): 
    def __init__(self,status): 
     self.id = status.id 
    def on_status(self, status): 
     print status.user.id 
     print status.user.screen_name 
     print status.id 
     print status.text 
     c.start() 

    def on_error(self, status_code): 
     print >> sys.stderr, 'Encountered error with status code:', status_code 
     return True # Don't kill the stream 

    def on_timeout(self): 
     print >> sys.stderr, 'Timeout...' 
     return True # Don't kill the stream 

sapi = tweepy.streaming.Stream(auth, CustomStreamListener()) 
sapi.filter(track=['#hashtag @username']) 
+0

它在做什麼呢?是否有例外?它是否創建了一個不同名稱的文件?無名? –

+0

我得到這個錯誤,我不知道如何通過CustomStreamListener()另一個參數。回溯(最近通話最後一個): 文件 「/home/pi/twitterwatcherwtimertakepicwrightname.py」 63行,在<模塊> SAPI = tweepy.streaming.Stream(AUTH,CustomStreamListener()) 類型錯誤:__init __()接受恰好有2個參數(1個給出) – user2815851

+0

如果您在常見問題解答中錯過了它,當提問*總是*包括您預期發生的事情,以及發生了什麼事情。我們中的大多數人不是很精通;) –

回答

1

CustomStreamListener類有一個__init__方法,它需要一個status說法,但在該行

sapi = tweepy.streaming.Stream(auth, CustomStreamListener()) 

你化妝的CustomStreamListener不經過這樣的說法,因此引發錯誤

__init__() takes exactly 2 arguments (1 given) 

實例這意味着__init__得到了self的說法,但不是另一個(status)。

要解決這個問題,你必須傳遞一些信息作爲status關於類的instatiation的參數!

+0

它看起來像我可能已經把自己置於我的頭上......我怎麼能通過狀態參數,以便它可以按需要工作? – user2815851

+0

因爲我沒有使用'tweepy'我不能給你一個很好的答案,但是可能你可以刪除整個'__init__'函數,因爲你從不在類中使用'self.id'。或者如果你有東西要通過,例如''''你可以這樣做:'sapi = tweepy.streaming.Stream(auth,CustomStreamListener(s))' – TobiMarg

0

關閉...

subprocess.Popen(['raspistill', '-o', '{0}.jpg'.format(new_tweet.id), '-t', '0'])

使用的String.Format()時,您需要在大括號內提供位置數。

+0

其實,不,你沒有。 '>>>'{} {}'。format(3,4)'是完全有效的語法。位置或關鍵字參數的唯一必要性是用於複製或改變顯示順序,例如, ''{0} {1} {0}'。格式('x',23)' –

+0

我很困惑。我應該說,我使用Python 2.6。當我這樣做時(>>>> {} {}'。格式(3,4)' 然後它會拋出 '追蹤(最近呼叫最後): 文件「」,第1行,在 '{} {}'。format(3,4) ValueError:格式爲零的字段名稱我在文檔中看到,從2.6開始,它添加了str.format(),甚至在3中。 *他們仍然在說以下行:'每個替換字段包含位置參數的數字索引或關鍵字參數的名稱。「http://docs.python.org/2.6/library/stdtypes.html# str.format –

+0

這聽起來像是它爲原始用戶(和你)工作,所以我假設它是我的用戶錯誤。我哪裏做錯了? –

相關問題