惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Y
Y Combinator Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
V2EX
博客园 - 三生石上(FineUI控件)
Hugging Face - Blog
Hugging Face - Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
罗磊的独立博客
博客园_首页
量子位
雷峰网
雷峰网
GbyAI
GbyAI
小众软件
小众软件
酷 壳 – CoolShell
酷 壳 – CoolShell
D
DataBreaches.Net
H
Hackread – Cybersecurity News, Data Breaches, AI and More
The Cloudflare Blog
IT之家
IT之家
WordPress大学
WordPress大学
人人都是产品经理
人人都是产品经理
Apple Machine Learning Research
Apple Machine Learning Research
P
Proofpoint News Feed
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 聂微东

herrkaefer

"Vibe planning \u003e vibe coding" "Anything to Markdown" "Built anocus: anonymous commenting for static sites" "日记与小说 -- AI 续写小说欣赏" "Any-podcast: from newsletters to a podcast" "Made MicPipe: a simple voice input tool using ChatGPT dictation" "关于 Tools 和 Skills 的一点感想" "Realtime monitoring of ComEd hourly price" "Introducing SwiftEdgeTTS" "Thoughts on the philosophy of building AI-native apps" "jelly鼻屎" "等饭的人" "Use home assistant to motivate my kid to brush teeth" "Migrated Blog to Hugo and Cloudflare Pages" "Easy Aspen monitoring for Chicago parents" "Introducing HabitBuilder: A simple Telegram bot for habit tracking" "鼓捣" "Open folder or file with Sublime Text from Finder toolbar" "Python dev workflow on macOS" "Create new text file from Finder toolbar" "Uno reinvented for 3-year-old kids" "Uno变身儿童数字游戏" "Handle annoying operations of objects in Realm DB" "Move Jekyll blog to Ubuntu VPS" "Introducing Mole" "Note taking without note taking app" "Deploy Python web application on Ubuntu server" "Setup Shadowsocks / VPN on Ubuntu Server" "Linode Notes - Basic Setup" "CLASS Style Adapted for Embedded Systems"
"自动转发Twitter到微博"
"herrkaefer" · 2017-11-06 · via herrkaefer

在Twitter上关注了几个图片账号, 比如@HistoryInPix, 经常想把一些有趣的图文转发到微博, 但操作起来却麻烦. 闲来无聊, 就做了个小机器人来自动把感兴趣的账号发的推全部转发到微博的某个账号(@图中看史)里, 这样在微博里就可以关注和转发了. 简单记录一下过程.

Open source: GitHub: twitter2weibo

准备工作

首先, 在Twitter和微博的开发平台上注册成为开发者, 并分别创建应用.

Twitter上很简单, 创建即生效, 直接拿到四个值: consumer_key, consumer_secret, access_token, access_token_secret.

微博上就有点麻烦, 因为需要官方审核. 因此需要做一个简单网页, 放上微博组件, 审核通过后才可以让第三方账号(开发者之外的账号)使用.

在微博开发平台的"基本信息"里, 得到App KeyApp Secret. 在"高级信息"的授权回调页可以填: http://127.0.0.1/callback.

接下来, 可以在iPython里顺序执行下面一次性授权工作, 得到token:




from weibo import Client



REDIRECT_URI = "http://127.0.0.1/callback"



c = Client(API_KEY, API_SECRET, REDIRECT_URI)



c.authorize_url

在浏览器打开授权页, 在地址栏里拷贝得到code.




code = 'xxxxxxxxx'



c.set_code(code)



print(c.token)

得到token后便可以保存到script中以供使用.

然后还需要注册一个微博"接收账号", 用于发布微博.

在VPS上配置Python环境

使用第三方库tweepyweibo, 来分别调用Twitter和微博的API.

脚本

基本流程: 从多个关注的Twitter账号抓取新的推文及图片, 然后发到微博账号.

建立一个appcofig.py私有文件, 存放Twitter和微博应用的key, secret等参数.

使用pickle存储每个Twitter账号最后转发的tweet的时间.

定期执行

在VPS上, 使用Cron来做即可.

添加任务:




10 * * * * python /home/herrkaefer/twitter2weibo/twitter2weibo.py >> /home/herrkaefer/twitter2weibo/log.txt

含义是每小时的10分执行一次, 并log到文件中.

在本次执行时, 应该检测脚本是否已经在执行中, 如果已经在执行则退出. 为了实现这一点, 可借用一个临时文件来判断. 在每次执行前:




pidfile = "/tmp/twitter2weibo.pid"



if os.path.isfile(pidfile):



sys.exit()



file(pidfile, 'w').write(str(os.getpid()))

正常执行后:




os.unlink(pidfile)



sys.exit()

限制

  • 微博每天有user rate limit, 不能太频繁, 但具体限制是多少, 官方文档中语焉不详. 目前设为3分钟发一条, 转发的Twitter账号数量很少, 看起来没问题.

  • 微博API只允许发一张图片, 因此推文含多图的, 只能转发第一张.