用于基本社区管理任务自动化的 5 个 Python 脚本
脚本

> 
> 如果某些事情不得不重复做三遍，尝试使其自动化。
> 
> 
> 


![](/data/attachment/album/202101/27/000544aakdc4mnmn6tzj8y.jpg "shapes of people symbols")


我 [之前写过关于社区管理员的工作](http://drbacchus.com/what-does-a-community-manager-do/)，如果你问 10 位社区管理员，你会得到 12 个不同的答案。不过，大多数情况下，你做的是社区需要你在任何特定时刻做的事情。而且很多事情可能是重复的。


当我还是系统管理员时，我遵循一个规则：如果某些事必须做三遍，我会尝试使其自动化。当然，如今，使用诸如 Ansible 这样的强大工具，就有了一整套科学的方法。


我每天或每周要做的一些事情涉及在一些地方查找内容，然后生成信息的摘要或报告，然后发布到别处。这样的任务是自动化的理想选择。这些并不是什么 [难事](https://6dollarshirts.com/rocket-surgery)，当我与同事共享其中一些脚本时，总是至少有一个能证明这是有用的。


[在 GitHub](https://github.com/rbowen/centos-community-tools/tree/master/scripts) 上，我有几个每周都要使用的脚本。它们都不是很复杂，但每次都为我节省几分钟。其中一些是用 Perl 写的，因为我快 50 岁了（这些是我早些年写的）。有些是用 Python 写的，因为几年前，我决定要学习 Python。


以下是概述：


### tshirts.py


这个简单的 [tshirts.py 脚本](https://github.com/rbowen/centos-community-tools/blob/master/scripts/tshirts.py)会根据你要定制的活动 T 恤的数量，然后告诉你尺寸分布是什么。它将这些尺寸分布在一条正态曲线（也称为 “钟形曲线”）上，以我的经验，这和一个正常的会议观众的实际需求非常吻合。如果在美国使用，则可能需要将脚本中的值调整的稍大一些；如果在欧洲使用，则可能需要将脚本中的值稍稍缩小一些。你的情况也许不同。


用法：



```
[rbowen@sasha:community-tools/scripts]$ ./tshirts.py                                                                                                                                                          
How many shirts? 300
For a total of 300 shirts, order:

30.0 small
72.0 medium
96.0 large
72.0 xl
30.0 2xl

```

### followers.py


[followers.py 脚本](https://github.com/rbowen/centos-community-tools/blob/master/scripts/followers.py)为我提供了我关心的 Twitter 账号的关注者数量。


该脚本只有 14 行，没什么复杂的，但是它可能节省我十分钟的加载网页和查找数字的时间。


你需要编辑 `feed` 数组以添加你关心的帐户：



```
feeds = [
        'centosproject',
        'centos'
        ];

```

注意：如果你在英语国家以外的地方运行它，则可能无法正常工作，因为它只是一个简单的屏幕抓取脚本，它读取 HTML 并查找其中包含的特定信息。因此，当输出使用其他语言时，正则表达式可能不匹配。


用法：



```
[rbowen@sasha:community-tools/scripts]$ ./followers.py                                                                                                                                                                          
centosproject: 11,479 Followers
centos: 18,155 Followers

```

### get\_meetups


[get\_meetups 脚本](https://github.com/rbowen/centos-community-tools/blob/master/scripts/get_meetups)S 另一种类别的脚本 —— API脚本。这个特定的脚本使用 [meetup.com](http://meetup.com) 网站的 API 来寻找在特定区域和时间范围内特定主题的聚会，以便我可以将它报告给我的社区。你所依赖的许多服务都提供了 API，因此你的脚本可以查找信息，而无需手动查找网页。学习如何使用这些 API 既令人沮丧又耗时，但是最终将获得可以节省大量时间的技能。


*免责声明：[meetup.com](http://meetup.com) 已于 2019 年 8 月更改了他们的 API，我还没有将这个脚本更新到新的API，所以它现在实际上并没有工作。在接下来的几周内请关注此版本的修复版本。*


### centos-announcements.pl


[centos-announcements.pl 脚本](https://github.com/rbowen/centos-community-tools/blob/master/scripts/centos-announcements.pl)要复杂得多，而且对我的用例来说非常特殊，但你可能有类似的情况。在本例中该脚本查看邮件列表存档（`centos-announce` 邮件列表），并查找具有特定格式的邮件，然后生成这些邮件的报告。报告有两种不同的格式：一种用于我的每月新闻通讯，另一种用于安排 Twitter 信息（借助于 Hootsuite）。


我使用 Hootsuite 为 Twitter 安排内容，它们具有便捷的 CSV（逗号分隔值）格式，你可以一次批量安排整整一周的推文。从各种数据源（比如：邮件列表、博客、其他网页）自动生成 CSV 格式可以节省大量时间。但是请注意，这可能只应该用于初稿，然后你可以对其进行检查和编辑，以便最终不会自动发布你不想要内容的推文。


### reporting.pl


[reporting.pl 脚本](https://github.com/rbowen/centos-community-tools/blob/master/scripts/sig_reporting/reporting.pl)也是相当特定的，以满足我的特殊需求，但是这个概念本身是通用的。我每月向 [CentOS SIG](https://wiki.centos.org/SpecialInterestGroup)（特殊兴趣小组）发送邮件，这些邮件计划在给定的月份报告。该脚本只是告诉我本月有哪些 SIG，并记录需要发送给他们的电子邮件。


但是，因以下两个原因，实际上并未发送该电子邮件。第一，我希望在消息发送之前对其进行编辑。第二，虽然发送电子邮件的脚本在过去很有效，但如今，很可能被当做垃圾邮件而被过滤。


### 总结


在这个存储库中还有一些其他脚本，这些脚本或多或少是针对我的特定需求的，但是我希望其中至少有一个脚本对你有用，并且希望这些脚本的多样性能激励你去自动化一些你自己的东西。我也希望看到你的自动化脚本存储库；在评论中链接他们！




---


via: <https://opensource.com/article/20/3/automating-community-management-python>


作者：[Rich Bowen](https://opensource.com/users/rbowen) 选题：[lujun9972](https://github.com/lujun9972) 译者：[stevenzdg988](https://github.com/stevenzdg988) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
I've [written before about what a community manager does](http://drbacchus.com/what-does-a-community-manager-do/), and if you ask ten community managers, you'll get 12 different answers. Mostly, though, you do what the community needs for you to do at any given moment. And a lot of it can be repetitive.

Back when I was a sysadmin, I had a rule: if I had to do something three times, I'd try to automate it. And, of course, these days, with awesome tools like Ansible, there's a whole science to that.

Some of what I do on a daily or weekly basis involves looking something up in a few places and then generating some digest or report of that information to publish elsewhere. A task like that is a perfect candidate for automation. None of this is [rocket surgery](https://6dollarshirts.com/rocket-surgery), but when I've shared some of these scripts with colleagues, invariably, at least one of them turns out to be useful.


[On GitHub](https://github.com/rbowen/centos-community-tools/tree/master/scripts), I have several scripts that I use every week. None of them are complicated, but they save me a few minutes every time. Some of them are in Perl because I'm almost 50. Some of them are in Python because a few years ago, I decided I needed to learn Python. Here's an overview:

[tshirts.py](https://github.com/rbowen/centos-community-tools/blob/master/scripts/tshirts.py)

[tshirts.py](https://github.com/rbowen/centos-community-tools/blob/master/scripts/tshirts.py)

This simple script takes a number of Tshirts that you're going to order for an event and tells you what the size distribution should be. It spreads them on a normal curve (also called a bell curve), and, in my experience, this coincides pretty well with what you'll actually need for a normal conference audience. You might want to adjust the script to slightly larger if you're using it in the USA, slightly smaller if you're using it in Europe. YMMV.

Usage:

```
[rbowen@sasha:community-tools/scripts]$ ./tshirts.py
How many shirts? 300
For a total of 300 shirts, order:
30.0 small
72.0 medium
96.0 large
72.0 xl
30.0 2xl
```


[followers.py](https://github.com/rbowen/centos-community-tools/blob/master/scripts/followers.py)

[followers.py](https://github.com/rbowen/centos-community-tools/blob/master/scripts/followers.py)

This script provides me with the follower count for Twitter handles I care about.

This script is only 14 lines long and isn't exciting, but it saves me perhaps ten minutes of loading web pages and looking for a number.

You'll need to edit the feeds array to add the accounts you care about:

```
feeds = [
'centosproject',
'centos'
];
```


NB: It probably won't work if you're running it outside of English-speaking countries, because it's just a simple screen-scraping script that reads HTML and looks for particular information buried within it. So when the output is in a different language, the regular expressions won't match.

Usage:

```
[rbowen@sasha:community-tools/scripts]$ ./followers.py
centosproject: 11,479 Followers
centos: 18,155 Followers
```


[get_meetups](https://github.com/rbowen/centos-community-tools/blob/master/scripts/get_meetups)

[get_meetups](https://github.com/rbowen/centos-community-tools/blob/master/scripts/get_meetups)

This script fits into another category—API scripts. This particular script uses the [meetup.com](http://meetup.com) API to look for meetups on a particular topic in a particular area and time range so that I can report them to my community. Many of the services you rely on provide an API so that your scripts can look up information without having to manually look through web pages. Learning how to use those APIs can be frustrating and time-consuming, but you'll end up with skills that will save you a LOT of time.

*Disclaimer: meetup.com changed their API in August of 2019, and I have not yet updated this script to the new API, so it doesn't actually work right now. Watch this repo for a fixed version in the coming weeks.*

[centos-announcements.pl](https://github.com/rbowen/centos-community-tools/blob/master/scripts/centos-announcements.pl)

[centos-announcements.pl](https://github.com/rbowen/centos-community-tools/blob/master/scripts/centos-announcements.pl)

This script is considerably more complicated and extremely specific to my use case, but you probably have a similar situation. This script looks at a mailing list archive—in this case, the centos-announce mailing list—and finds messages that are in a particular format, then builds a report of those messages. Reports come in a couple of different formats—one for my monthly newsletter and one for scheduling messages (via Hootsuite) for Twitter.

I use Hootsuite to schedule content for Twitter, and they have a convenient CSV (comma-separated value) format that lets you bulk-schedule a whole week of tweets in one go. Auto-generating that CSV from various data sources (i.e., mailing lists, blogs, other web pages) can save you a lot of time. Do note, however, that this should probably only be used for a first draft, which you then examine and edit yourself so that you don't end up auto-tweeting something you didn't intend to.

[reporting.pl](https://github.com/rbowen/centos-community-tools/blob/master/scripts/sig_reporting/reporting.pl)

[reporting.pl](https://github.com/rbowen/centos-community-tools/blob/master/scripts/sig_reporting/reporting.pl)

This script is also fairly specific to my particular needs, but the concept itself is universal. I send out a monthly mailing to the [CentOS SIGs](https://wiki.centos.org/SpecialInterestGroup) (Special Interest Groups), which are scheduled to report in that given month. This script simply tells me which SIGs those are this month, and writes the email that needs to go to them.

It does not actually send that email, however, for a couple of reasons. One, I may wish to edit those messages before they go out. Two, while scripts sending email worked great in the old days, these days, they're likely to result in getting spam-filtered.

## In conclusion

There are some other scripts in that repo that are more or less specific to my particular needs, but I hope at least one of them is useful to you, and that the variety of what's there inspires you to automate something of your own. I'd love to see your handy automation script repos, too; link to them in the comments!

## 5 Comments