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

推荐订阅源

Microsoft Azure Blog
Microsoft Azure Blog
Engineering at Meta
Engineering at Meta
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
F
Fortinet All Blogs
博客园 - 叶小钗
T
Tailwind CSS Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
T
The Exploit Database - CXSecurity.com
Blog — PlanetScale
Blog — PlanetScale
T
Tenable Blog
人人都是产品经理
人人都是产品经理
D
DataBreaches.Net
A
Arctic Wolf
P
Proofpoint News Feed
S
SegmentFault 最新的问题
C
CERT Recently Published Vulnerability Notes
T
Threatpost
Y
Y Combinator Blog
WordPress大学
WordPress大学
L
LINUX DO - 热门话题
酷 壳 – CoolShell
酷 壳 – CoolShell
V
Vulnerabilities – Threatpost
AWS News Blog
AWS News Blog
小众软件
小众软件
U
Unit 42
云风的 BLOG
云风的 BLOG
美团技术团队
S
Securelist
C
Cybersecurity and Infrastructure Security Agency CISA
L
LangChain Blog
G
GRAHAM CLULEY
P
Proofpoint News Feed
I
Intezer
Security Latest
Security Latest
L
Lohrmann on Cybersecurity
NISL@THU
NISL@THU
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
The Hacker News
The Hacker News
Cisco Talos Blog
Cisco Talos Blog
T
The Blog of Author Tim Ferriss
H
Heimdal Security Blog
T
Tor Project blog
MongoDB | Blog
MongoDB | Blog
The Cloudflare Blog
T
Troy Hunt's Blog
Know Your Adversary
Know Your Adversary
博客园 - 司徒正美
Google Online Security Blog
Google Online Security Blog
V
V2EX
Application and Cybersecurity Blog
Application and Cybersecurity Blog

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" "psycopgr Tutorial" "pgRouting Notes" "PostgreSQL Notes" "阿城三王" "这一年,这一把日子" "另一面的发现——读《坟》" "定理" "封面与腰封" "Google book下载" "lulu最新写真出炉" "The Big Bang Theory第三季" "自拍婚纱照1" "日全食" "期待动画片" "《麦兜响当当》动画电影主题曲" "转:饶毅--“杂志拜物教”:何时发Cell Nature Science 论文害你" "转:饶毅--提醒年轻人:何时SCI害你?" "西安" "3d打印机" "Dropbox" "刷牙" "贴几张照片" "6156167" "永久和凤凰" "老板的想法" "春" "原来奥巴马也是个朗读者" "应邀发Freeware List 2.0" "史上最能睡的淘宝老板" "至少出名的效果是达到了" "错怪了msn" "独立游戏节2009" "114" "馒头" "Crayon Physics Deluxe" "2008,2009" "盖章记" "小虎队附身许巍" "怎么给word文档加索引:排序问题" "怎么给word文档加索引Q\u0026amp;A" "怎么给Word文档加索引" "教我如何不疯掉" "二则" "P" "哦!报告" "蓝天" "萧翰" "lm" "故宫印象" "转:美国历任总统像" "time can kill itself" "嗯" "建议,只是建议哦" "奥地利行记3" "奥地利行记2" "奥地利行记1" "叶子" "GayBoy" "天使教你扔frisbie" "门徒因何面容愁?" "手机教堂" "丝竹管弦之盛" "残奥" "争座位" "秋意浅" "总理府" "流觞曲水" "映带左右引以为" "咚咚咚 续" "茂林修竹又有"
"自动转发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只允许发一张图片, 因此推文含多图的, 只能转发第一张.