在Twitter的用户页面或搜索页面上查看推文。
项目描述
Twittcher(适用于twitter-watcher)是一个Python模块,用于创建将监视Twitter用户页面或搜索页面并对其找到的推文做出反应的机器人。
它很简单,很小(目前大约150行代码),并且不需要在Twitter或dev.twitter.com上进行任何注册,因为它不依赖于Twitter API(而是解析HTML)。
Twittcher是开源软件,最初由Zulko编写,并使用MIT许可证发布。该项目托管在Github上,您可以在那里报告错误、提出改进等。
安装
如果您有pip,请在终端中输入以下内容以安装twittcher
(sudo) pip install twittcher
否则,从Github或PyPI下载源代码,并在与setup.py文件相同的目录中,在终端中输入以下内容
(sudo) python setup.py install
Twittcher需要Python包BeautifulSoup(也称为bs4),当安装twittcher时将自动安装。
使用示例
目前没有Twittcher的文档,但以下示例应该可以向您展示您需要了解的所有内容。
1. 打印特定用户的推文
每120秒打印John D. Cook的所有新推文
from twittcher import UserWatcher UserWatcher("JohnDCook").watch_every(120)
结果
Kicking off some simulations before I quit work for the day. #dejavu Author: JohnDCook Date: 15:43 - 24 juil. 2014 Link: https://twitter.com/JohnDCook/status/492440083073859585 “Too often we enjoy the comfort of opinion without the discomfort of thought." -- John F. Kennedy, Author: JerryWeinberg Date: 13:18 - 24 juil. 2014 Link: https://twitter.com/JerryWeinberg/status/492403371975114752
UserWatcher的默认操作是打印推文,但您可以要求任何其他操作。例如,以下是如何仅打印John D. Cook实际撰写的推文(而不是他转发的推文)
from twittcher import UserWatcher def my_action(tweet): if tweet.username == "JohnDCook": print(tweet) UserWatcher("JohnDCook", action=my_action).watch_every(120)
2. 通过Twitter控制远程机器!
每60秒,对我的任何新推文进行形式为cmd: my_command的运行,在终端中执行my_command。使用简单的推文,我可以控制运行此脚本的任何远程计算机。
import subprocess from twittcher import UserWatcher def my_action(tweet): """ Execute the tweet's command, if any. """ if tweet.text.startswith("cmd: "): subprocess.Popen( tweet.text[5:], shell=True ) # Watch my account and react to my tweets bot = UserWatcher("Zulko___", action=my_action) bot.watch_every(60)
例如,推文 cmd: firefox 将在计算机上打开 Firefox,而推文 cmd: echo "Hello" 将使计算机在终端打印 Hello。
3. 监视搜索结果并发送警报邮件
每20秒,发送所有关于 巧克力牛奶 的 Twitter 搜索结果中的新推文给我。
from twittcher import TweetSender, SearchWatcher sender = TweetSender(smtp="smtp.gmail.com", port=587, login="tintin.zulko@gmail.com", password="fibo112358", # be nice, don't try. to_addrs="tintin.zulko@gmail.com", # where to send sender_id = "chocolate milk") bot = SearchWatcher("chocolate milk", action=sender.send) bot.watch_every(20)
4. 多机器人监视
如果您想同时运行多个机器人,请确保在不同机器人的请求之间留出几秒钟的时间。以下是打印 John D. Cook、Mathbabe 和 Eolas 的新推文的方式。每个机器人每分钟被监视一次,两个机器人的请求之间有20秒的间隔。
import time import itertools from twittcher import UserWatcher bots = [ UserWatcher(user) for user in ["JohnDCook", "mathbabedotorg", "Maitre_Eolas"]] for bot in itertools.cycle(bots): bot.watch() time.sleep(20)
5. 保存推文
机器人可以将它已经看到的推文保存到文件中,这样在未来的会话中,它将记住不再处理这些推文,以防它们仍然出现在监视的页面上。
from twittcher import SearchWatcher bot = SearchWatcher("chocolate milk", database="choco.db") bot.watch_every(20)
项目详情
twittcher-0.1.06.tar.gz 的散列值
算法 | 散列摘要 | |
---|---|---|
SHA256 | 55d296604696ce327eaba3b860005a3b57594312f172e33abcbb6d7c8f57f6e2 |
|
MD5 | dc577cd58868c8c7b96908ee18536b88 |
|
BLAKE2b-256 | dae57d8b933f3d26abeb8ed8063e6a28a206fcc58780d5ba3fb44644942dd2ef |