# BattBot
<p align="center">
<img src="https://user-images.githubusercontent.com/74055102/197869338-9f7a41aa-65e8-42f4-b5cf-d8480d3e2ae4.png"/>
<img src="https://user-images.githubusercontent.com/74055102/198250858-e4e9063b-f941-45b6-9c89-34a8b95378fa.gif"/>
</p>
<div align="center">
[![Twitter](https://img.shields.io/twitter/url/https/twitter.com/battbot_.svg?style=social&label=Follow%20@battbot_)](https://twitter.com/battbot_)
[![PyBaMM Twitter bot](https://github.com/pybamm-team/BattBot/actions/workflows/ci.yml/badge.svg)](https://github.com/pybamm-team/BattBot/actions/workflows/ci.yml)
[![codecov](https://codecov.io/gh/pybamm-team/BattBot/branch/main/graph/badge.svg?token=6wEJ6AiiGG)](https://codecov.io/gh/pybamm-team/BattBot)
[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/pybamm-team/BattBot/blob/main/)
[![black_code_style](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/ambv/black)
</div>
An automated Battery Bot that tweets random battery configuration plots in the form of a GIF with the help of [PyBaMM](https://github.com/pybamm-team/PyBaMM). The bot focuses on comparing 2 or 3 different configurations and can also reply to a simulation request. All the random tweeted configurations are stored in [data.txt](https://github.com/pybamm-team/BattBot/blob/main/bot/data.txt) and the latest tweeted configuration is stored in [config.txt](https://github.com/pybamm-team/BattBot/blob/main/bot/config.txt) that can also be played with on Google Colab [here](https://colab.research.google.com/github/pybamm-team/BattBot/blob/main/bot/run-simulation.ipynb). Some examples for the simulation requests through a tweet are available here - [`REQUEST_EXAMPLES.md`](https://github.com/pybamm-team/BattBot/blob/main/REQUEST_EXAMPLES.md).
## Deployment (The CI/CD pipeline)
One half of the bot is deployed on `GitHub Actions` and the other half is deployed on `Heroku`. The GitHub Actions deployment tweets random configurations at 7 am and 7 pm UTC whereas the Heroku deployment runs the script every minute to look out for tweet requests.
`GitHub Actions` is also responsible for running the tests, updating the [stored random configurations](https://github.com/pybamm-team/BattBot/blob/main/bot/data.txt), and keeping the [run-simulations](https://colab.research.google.com/github/pybamm-team/BattBot/blob/main/bot/run-simulation.ipynb) notebook in working condition (by updating the latest tweeted configuration in [config.txt](https://github.com/pybamm-team/BattBot/blob/main/bot/config.txt)). The [`last_seen_id`](https://github.com/pybamm-team/BattBot/blob/main/bot/last_seen_id.txt) (ID of the tweet to which the bot replied last) is also [synced](https://github.com/pybamm-team/BattBot/blob/main/bot/twitter_api/sync_last_seen_id.py#L20) on scheduled runs to keep the `Heroku` and `GitHub Actions` deployment in sync with each other.
Once everything in the `Continuous Integration` (`GitHub Actions`) part of the pipeline passes, the `Continuous Deployment` phase starts and the bot is deployed on `Heroku`, where it is built again with the updated `last_seen_id` (`Heroku` does not store the locally updated files permanently because of its [ephemeral filesystem](https://devcenter.heroku.com/articles/active-storage-on-heroku#ephemeral-disk), another reason to update `last_seen_id` using `GitHub Actions`).
The files that keep `Heroku` deployment running -
- [requirements.txt](https://github.com/pybamm-team/BattBot/blob/main/requirements.txt)
- [runtime.txt](https://github.com/pybamm-team/BattBot/blob/main/runtime.txt)
- [Procfile](https://github.com/pybamm-team/BattBot/blob/main/Procfile)
The file that keeps `GitHub Actions` deployment running -
- [python-app.yml](https://github.com/pybamm-team/BattBot/blob/main/.github/workflows/python-app.yml)
## Random Tweets
- The random configuration is generated in [`config_generator.py`](https://github.com/pybamm-team/BattBot/blob/main/bot/plotting/config_generator.py), hence, all the new possible options in the future should be added in this file.
- This configuration is passed down to [`random_plot_generator`](https://github.com/pybamm-team/BattBot/blob/main/bot/plotting/random_plot_generator.py#L9) which then calls different scripts based on if degradation is present in the configuration.
- Comparison plots (which are `"model comparison"` and `"parameter comparison"`) with no degradation are generated in [`ComparisonGenerator`](https://github.com/pybamm-team/BattBot/blob/main/bot/plotting/comparison_generator.py#L7)
- The GIFs are created in [`create_gif.py`](https://github.com/pybamm-team/BattBot/blob/main/bot/plotting/create_gif.py#L9) and are resized using [`resize_gif.py`](https://github.com/pybamm-team/BattBot/blob/main/bot/utils/resize_gif.py#L5) to make them suitable for `Twitter API`.
- The GIFs are then finally tweeted out by the [`Tweet`](https://github.com/pybamm-team/BattBot/blob/main/bot/twitter_api/tweet_plot.py#L13) class.
## Requested Tweets
- The replying functionality is a hybrid of [`tweepy`](https://docs.tweepy.org/en/stable/) and inbuilt python libraries `requests` and `requests_oauthlib`.
- The [`Reply`](https://github.com/pybamm-team/BattBot/blob/main/bot/twitter_api/tweet_reply.py#L11) class is responsible for reading the tweet requests and for creating a configuration from that tweet's text.
- This configuration is then passed down to `random_plot_generator` which now simulates and solves it (as it does with the random configuration).
## Uploading and Replying (Twitter API)
- A GIF is uploaded and tweeted using the [`Upload`](https://github.com/pybamm-team/BattBot/blob/main/bot/twitter_api/upload.py#L13) class. It uploads a media file in chunks (keeping in mind `Twitter API`'s time out) to a `Twitter API`'s endpoint and then finally tweets it.
- `Tweet` and `Reply` classes inherit the `Upload` class to tweet random and user-requested GIFs.
## Tests and coverage
The tests and the workflows are designed in a way that protects the `Twitter API keys` from getting revealed. The tests that require `Twitter API keys` to run, never run on a PR coming from a forked repository, as that would expose the `API keys` and anyone with a malicious script on that PR will be able to print/store/read them. More information on the structure of the `test` directory can be found [here](https://github.com/pybamm-team/BattBot/blob/main/CONTRIBUTING.md#testing).
The bot uses a lot of multiprocessing calls (using a custom [`Process`](https://github.com/pybamm-team/BattBot/blob/main/bot/utils/custom_process.py#L6) class) which makes the code coverage a bit unusual. The [`sitecustomize.py`](https://github.com/pybamm-team/BattBot/blob/main/sitecustomize.py) and [`.coveragerc`](https://github.com/pybamm-team/BattBot/blob/main/.coveragerc) files make sure that coverage is used to run tests in subprocesses. Complete documentation for this can be found [here](https://coverage.readthedocs.io/en/coverage-5.5/subprocess.html).
The coverage report on a PR coming from a fork will show that the coverage goes down (even if everything is covered) as some tests won't run on that PR as described above.
## Citing PyBaMM
If you use PyBaMM in your work, please cite the paper
> Sulzer, V., Marquis, S. G., Timms, R., Robinson, M., & Chapman, S. J. (2021). Python Battery Mathematical Modelling (PyBaMM). _Journal of Open Research Software, 9(1)_.
You can use the bibtex
```
@article{Sulzer2021,
title = {{Python Battery Mathematical Modelling (PyBaMM)}},
author = {Sulzer, Valentin and Marquis, Scott G. and Timms, Robert and Robinson, Martin and Chapman, S. Jon},
doi = {10.5334/jors.309},
journal = {Journal of Open Research Software},
publisher = {Software Sustainability Institute},
volume = {9},
number = {1},
pages = {14},
year = {2021}
}
```
To cite papers relevant to your code, you ca
没有合适的资源?快使用搜索试试~ 我知道了~
一个自动Twitter机器人,它发布随机电池模拟并回.zip
共56个文件
py:41个
txt:5个
md:4个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 80 浏览量
2023-04-08
18:03:09
上传
评论
收藏 123KB ZIP 举报
温馨提示
一个自动Twitter机器人,它发布随机电池模拟并回.zip
资源推荐
资源详情
资源评论
收起资源包目录
一个自动Twitter机器人,它发布随机电池模拟并回.zip (56个子文件)
BattBot-main
bot
__init__.py 132B
data.txt 1.1MB
config.txt 1KB
last_seen_id.txt 19B
plotting
__init__.py 0B
random_plot_generator.py 4KB
config_generator.py 9KB
comparison_generator.py 9KB
degradation_comparison_generator.py 6KB
utils
__init__.py 0B
desired_decimal_point_generator.py 398B
custom_process.py 908B
resize_gif.py 3KB
degradation_parameter_generator.py 8KB
parameter_value_generator.py 2KB
tweet_text_generator.py 4KB
experiment
__init__.py 0B
experiment_generator.py 2KB
twitter_api
__init__.py 0B
tweet_plot.py 6KB
sync_last_seen_id.py 2KB
api_keys.py 280B
tweet_reply.py 16KB
upload.py 6KB
run-simulation.ipynb 22KB
sitecustomize.py 43B
.coveragerc 157B
.github
workflows
ci.yml 5KB
Makefile 44B
CONTRIBUTING.md 6KB
REQUEST_EXAMPLES.md 8KB
CODE_OF_CONDUCT.md 5KB
runtime.txt 12B
Procfile 48B
requirements.txt 84B
test
__init__.py 132B
without_keys
__init__.py 0B
test_comparison_generator.py 8KB
test_degradation_parameter_generator.py 4KB
test_desired_decimal_point_generator.py 393B
test_config_generator.py 8KB
test_custom_process.py 591B
test_resize_gif.py 2KB
test_random_plot_generator.py 6KB
test_parameter_value_generator.py 4KB
test_degradation_comparison_generator.py 3KB
test_experiment_generator.py 2KB
test_tweet_text_generator.py 8KB
with_keys
test_api_keys.py 854B
test_sync_last_seen_id.py 255B
__init__.py 0B
test_upload.py 3KB
test_tweet_reply.py 8KB
test_tweet_plot.py 4KB
.gitignore 120B
README.md 8KB
共 56 条
- 1
资源评论
快撑死的鱼
- 粉丝: 1w+
- 资源: 9154
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功