轻松安排Twitter更新
coo的Python项目详细描述
coo是一个易于使用的python库,用于安排twitter更新。要使用它,你需要 首先在 Twitter Developers Platform并生成密钥和 访问令牌。
pipinstallcoo
初始化
fromcooimportCooat=Coo("consumer_key","consumer_secret","access_token","access_token_secret",preview=False,)
或者,您可以设置preview=True并在终端中打印您的tweets 在推特上发布。
安排Twitter更新:
fromcooimportCooat=Coo("consumer_key","consumer_secret","access_token","access_token_secret")tweets=[("2030-12-05 16:30",template,"Awesome Twitter update."),("2030-10-28 18:50",template,"Another awesome Twitter update."),("2030-10-29 18:15",template2,"One more update."),("2030-11-01 13:45",None,"Twitter update without a template."),at.schedule(tweets,time_zone="America/Santiago")
或者可以使用字符串列表并添加delay、interval和template:
tweets=["My first awesome Twitter Update","My second awesome Twitter Update","My third awesome Twitter Update","My fourth awesome Twitter Update","My fifth awesome Twitter Update","My sixth awesome Twitter Update",]at.tweet(tweets,delay="13:45",interval="four_hours",template=my_template)
安排Twitter更新
使用datetime
字符串或整数安排更新,并在需要时使用自定义aTemplate。
Coo.schedule(updates,time_zone)
完整示例:
fromcooimportCooat=Coo("consumer_key","consumer_secret","access_token","access_token_secret")tweets=[# datetime with and without templates("2030-10-28 18:50",template,"My Twitter update with a template."),("2030-10-29 18:15",template2,"Update with a different template."),("2030-11-01 13:45",None,"Twitter update without a template."),# date with and without templates("2030-12-25",template3,"Merry christmas!"),("2031-01-01",None,"And a happy new year!"),# time with and without templates("18:46",template2,"Will be post today at 18:46."),("23:00",None,"A tweet for today at 23:00."),# integer (seconds) with and without templates(3600,template,"This tweet will be posted in an hour."),(86400,None,"This one, tomorrow at the same hour."),]at.schedule(tweets,time_zone="America/Santiago")
分析日期时间字符串
- 如果未指定时区,则将其设置为
local
。 - 如果未指定,时间将设置为00:00:00。
- 当只传递时间信息时,日期将默认为今天。
- 需要将来的日期,否则将引发
ScheduleError
。
在这里你可以找到所有 Time Zones。
媒体文件
有两种方法可以将媒体文件添加到您的tweets中。第一个也是最简单的方法是对所有更新使用一个全局文件:
at.schedule(tweets,time_zone="America/Santiago",media="path/to/file.png")
此外,还可以为每个更新设置一个单独的文件:
tweets=[("2030-10-28 18:50",template,"Update with an image.","pics/owl.png"),("2030-10-29 18:15",template,"Update with other media.","videos/funny_video.mp4"),("2030-11-01 13:45",template,"Tweet without media."),]
最后,可以将这些方法结合起来。例如,如果大多数tweets将使用相同的媒体,而只有少数tweets将使用不同的媒体或没有媒体:
tweets=[("2030-11-01 13:45",template,"Tweet with global media."),("2030-11-02 13:45",template,"Tweet with global media."),("2030-11-03 13:45",template,"Tweet with global media."),("2030-11-04 13:45",template,"Tweet with global media."),("2030-11-05 13:45",template,"Tweet with global media."),("2030-11-06 13:45",template,"Tweet with global media."),("2030-11-07 13:45",template,"Tweet with global media."),("2030-11-08 13:45",template,"Tweet without media.",None),("2030-11-09 13:45",template,"Tweet without media.",None),("2030-12-10 18:50",template,"Update with an image.","pics/owl.png"),("2030-12-11 18:15",template,"Update with other media.","videos/funny_video.mp4"),]at.schedule(tweets,time_zone="America/Santiago",media="path/to/global_media.png")
按顺序发送字符串列表
如果需要,可以使用Delay、Interval和Template发布顺序更新。
Coo.tweet(updates,delay,interval,template,time_zone)
fromcooimportCooat=Coo("consumer_key","consumer_secret","access_token","access_token_secret")tweets=["My first awesome Twitter Update","My second awesome Twitter Update","My third awesome Twitter Update","My fourth awesome Twitter Update","My fifth awesome Twitter Update","My sixth awesome Twitter Update",]# post the twitter updatesat.tweet(tweets)
延迟
您可以使用datetime、date和time字符串,将整数用作秒和一些 Keywords:half_hour、one_hour、one_day和one_week之间 推迟发布第一次更新。
# datetime, date and time stringsat.tweet(tweets,delay="2030-11-24 13:45",time_zone="America/Santiago")at.tweet(tweets,delay="2030-11-24",time_zone="Australia/Sydney")at.tweet(tweets,delay="13:45",time_zone="America/New_York")# "keywords"at.tweet(tweets,delay="one_week")# integerat.tweet(tweets,delay=604800)
分析日期时间字符串时:
- 如果未指定时区,则将其设置为
local
。 - 如果未指定,时间将设置为00:00:00。
- 当只传递时间信息时,日期将默认为今天。
- 需要将来的日期,否则将引发
ScheduleError
。
在这里你可以找到所有的Time Zones。
间隔
将整数用作秒或某些字符串用作Keywords:half_hour,one_hour, one_day和one_week之间。
# "keywords"at.tweet(tweets,interval="four_hours")# integersat.tweet(tweets,interval=14400)
媒体文件
所有更新使用一个媒体文件:
at.tweet(tweets,media="path/to/media.jpeg")
随机更新
随机发送更新:
at.tweet(tweets,aleatory=True)
关键词
Keyword | Seconds |
---|---|
now | 0 |
half_hour | 1800 |
one_hour | 3600 |
two_hours | 7200 |
four_hours | 14400 |
six_hours | 21600 |
eight_hours | 28800 |
ten_hours | 36000 |
twelve_hours | 43200 |
fourteen_hours | 50400 |
sixteen_hours | 57600 |
eighteen_hours | 64800 |
twenty_hours | 72000 |
twenty_two_hours | 79200 |
one_day | 86400 |
two_days | 172800 |
three_days | 259200 |
four_days | 345600 |
five_days | 432000 |
six_days | 518400 |
one_week | 604800 |
模板
模板非常简单,只需使用多行字符串并添加$message 您希望邮件出现的位置。
template="""My awesome header $message #python #coding #coo """
twitter api
coo是使用Python Twitter编写的
包装器,通过Coo.api
您可以访问他的所有模型:
# get your followersfollowers=at.api.GetFollowers()# get your direct messagesd_messages=at.api.GetDirectMessages()# favorited tweetsfavorites=at.api.GetFavorites()# mentionsmentions=at.api.GetMentions()# retweetsretweets=at.api.GetRetweets()
还有更多。如果您感兴趣,请查看他们的documentation。
文档
文档可在readthedocs.org找到。