2010-10-26 43 views
0

我想將一些信息添加到將信息發佈到twitter的python腳本中。這是迄今爲止代碼:(Python)如何將變量打印到Tweepy「API.update_status()」命令?

#!/usr/bin/python 
import os 
import subprocess 
import psutil 
import sys 
import tweepy 

#---------------------------------------- 
# Gives a human-readable uptime string 
def uptime(): 
    try: 
    f = open("/proc/uptime") 
    contents = f.read().split() 
    f.close() 
    except: 
    return "Cannot open uptime file: /proc/uptime" 
    total_seconds = float(contents[0]) 
    # Helper vars: 
    MINUTE = 60 
    HOUR = MINUTE * 60 
    DAY = HOUR * 24 
    # Get the days, hours, etc: 
    days = int(total_seconds/DAY) 
    hours = int((total_seconds % DAY)/HOUR) 
    minutes = int((total_seconds % HOUR)/MINUTE) 
    seconds = int(total_seconds % MINUTE) 
    # Build up the pretty string (like this: "N days, N hours, N minutes, N seconds") 
    string = "" 
    if days> 0: 
    string += str(days) + " " + (days == 1 and "day" or "days") + ", " 
    if len(string)> 0 or hours> 0: 
    string += str(hours) + " " + (hours == 1 and "hour" or "hours") + ", " 
    if len(string)> 0 or minutes> 0: 
    string += str(minutes) + " " + (minutes == 1 and "minute" or "minutes") + ", " 
    string += str(seconds) + " " + (seconds == 1 and "second" or "seconds") 
    return string; 
uptime() = time 
psutil.Process(2360).get_memory_percent() = Mem 
##Twitter Auth Stuff## 

CONSUMER_KEY = 'blahblah' 
CONSUMER_SECRET = 'edited because' 
ACCESS_KEY = 'this is' 
ACCESS_SECRET = 'private' 

auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET) 
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET) 
api = tweepy.API(auth) 
api.update_status() 

我想有時間和MEM變量發佈他們輸出到api.update_status() 任何線索或其它方法來做到這一點?

謝謝!

回答

1

那麼問題是什麼?該tweepy文檔說可以傳遞一個字符串update_status()

api.update_status('uptime: %s/mem: %i%%' % (time, mem)) 

而且,我猜你的意思

time = uptime() 
mem = psutil.Process(2360).get_memory_percent() 
+0

這是我的第一個Python腳本,所以我不知道如何使一個串。謝謝! – Paul 2010-10-26 18:00:34