这是一个twitter,每两小时调用一次,从文件夹中发布图片,文件被连续编号,当前编号存储在文本文件中,以便在运行之间保持不变。图像文件类型在.jpg和.gif之间有所不同,我不知道如何在我的代码的图片()函数中解释这一点。
import os
from twython import Twython
from twython import TwythonStreamer
APP_KEY = ''
APP_SECRET = ''
OAUTH_TOKEN = ''
OAUTH_TOKEN_SECRET = ''
f = open('pictures.txt', 'r+')
z = f.read()
def picture():
picture = open('/0/' + 'picture' + str(z))
f.write(str(z)+'\n')
global z
z += 1
promote(picture)
f.write(z)
f.close
def promote(photo):
twitter = Twython(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
twitter.update_status_with_media(status='', media=photo)
picture()发布于 2014-06-28 13:41:09
既然你的上一个问题被搁置了,我再发一次这个答案。
使用格罗布查找与前缀匹配的文件,使用imghdr检查文件类型(twitter不支持所有图像文件),并确保在读取图像序列号时将其转换为int,并在更新文件时将其转换为字符串。文件更新要求首先查找文件的开头,这假定序列号将始终增加。
import imghdr
from glob import glob
SUPPORTED_IMG_TYPES = 'gif jpeg png'.split()
IMG_SEQ_FILE = '/0/pictures.txt'
GLOB_PATTERN = '/0/picture%d.*'
def send_to_twitter(filename):
print "sent %s to twitter" % filename
return True
with open(IMG_SEQ_FILE, 'r+') as f:
seq = int(f.readline().strip())
for name in glob(GLOB_PATTERN % seq):
img_type = imghdr.what(name)
if img_type in SUPPORTED_IMG_TYPES:
if send_to_twitter(name):
f.seek(0)
seq += 1
f.write(str(seq))
break
else:
if not img_type:
print "%s is not an image file" % name
else:
print "%s unsupported image type: %s" % (name, img_type)您所需要做的就是添加代码将图像文件数据发送到twitter。
https://stackoverflow.com/questions/24466832
复制相似问题