├── tests ├── data │ ├── simple.yml │ ├── broken.yaml │ ├── tweets.txt │ ├── tweets.csv │ ├── test.yaml │ ├── test.json │ └── js │ │ └── tweets │ │ └── 2014-04.js ├── __init__.py ├── test_args.py ├── test_tools.py ├── config.py ├── test_confighelper.py ├── fixtures │ ├── favorite.yaml │ ├── followback.yaml │ ├── unfollow.yaml │ ├── verify_credentials.yaml │ ├── user_timeline.yaml │ └── status.yaml ├── test_archive.py ├── test_api.py └── test_helpers.py ├── .gitignore ├── .github └── workflows │ ├── release.yaml │ └── python.yaml ├── docs ├── source │ ├── api.rst │ ├── index.rst │ ├── cli.rst │ ├── helloworld.rst │ └── conf.py └── Makefile ├── src └── twitter_bot_utils │ ├── __init__.py │ ├── archive.py │ ├── args.py │ ├── tools.py │ ├── confighelper.py │ ├── api.py │ ├── helpers.py │ └── cli.py ├── Makefile ├── pyproject.toml ├── HISTORY.rst ├── README.md └── LICENSE /tests/data/simple.yml: -------------------------------------------------------------------------------- 1 | key: INDIA 2 | secret: LIMA 3 | consumer_key: NOVEMBER 4 | consumer_secret: ECHO 5 | -------------------------------------------------------------------------------- /tests/data/broken.yaml: -------------------------------------------------------------------------------- 1 | users: 2 | example_screen_name: 3 | app: example_app_name 4 | 5 | apps: 6 | example_app_name: 7 | -------------------------------------------------------------------------------- /tests/data/tweets.txt: -------------------------------------------------------------------------------- 1 | @mention hihdfg 2 | tweet with a #hashtag 3 | RT @blah blah blah 4 | Regular old tweet 5 | Tweet with an åccent 6 | Tweet with an 😀 emoji 7 | -------------------------------------------------------------------------------- /tests/data/tweets.csv: -------------------------------------------------------------------------------- 1 | text 2 | @mention hihdfg 3 | tweet with a #hashtag 4 | RT @blah blah blah 5 | Regular old tweet 6 | Tweet with an åccent 7 | Tweet with an 😀 emoji 8 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | __pycache__ 2 | *.pyc 3 | build 4 | *.egg-info 5 | dist 6 | *.zip 7 | readme.rst 8 | .tox 9 | .eggs 10 | *.egg 11 | bots.yaml 12 | .envrc 13 | docs/_build 14 | -------------------------------------------------------------------------------- /tests/__init__.py: -------------------------------------------------------------------------------- 1 | __all__ = [ 2 | 'test_api', 3 | 'test_archive', 4 | 'test_args', 5 | 'test_confighelper', 6 | 'test_helpers', 7 | 'test_tools', 8 | ] 9 | -------------------------------------------------------------------------------- /tests/data/test.yaml: -------------------------------------------------------------------------------- 1 | users: 2 | example_screen_name: 3 | key: INDIA 4 | secret: LIMA 5 | app: example_app_name 6 | custom: user 7 | 8 | apps: 9 | example_app_name: 10 | consumer_key: NOVEMBER 11 | consumer_secret: ECHO 12 | custom: app 13 | 14 | 15 | custom: general 16 | 17 | utf8: åéîøü and ¥ 18 | -------------------------------------------------------------------------------- /tests/data/test.json: -------------------------------------------------------------------------------- 1 | { 2 | "users": { 3 | "example_screen_name": { 4 | "key": "INDIA", 5 | "secret": "LIMA", 6 | "app": "example_app_name", 7 | "custom": "user" 8 | } 9 | }, 10 | "apps": { 11 | "example_app_name": { 12 | "consumer_key": "NOVEMBER", 13 | "consumer_secret": "ECHO", 14 | "custom": "app" 15 | } 16 | }, 17 | "custom": "general", 18 | "utf8": "åéîøü and ¥" 19 | } 20 | -------------------------------------------------------------------------------- /.github/workflows/release.yaml: -------------------------------------------------------------------------------- 1 | name: release 2 | 3 | on: [release] 4 | 5 | jobs: 6 | release: 7 | runs-on: ubuntu-latest 8 | 9 | steps: 10 | - uses: actions/checkout@v2 11 | 12 | - name: Set up Python 3.9 13 | uses: actions/setup-python@v2 14 | with: 15 | python-version: 3.9 16 | 17 | - name: install flit 18 | run: pip install flit 19 | 20 | - name: Publish package 21 | run: flit publish 22 | env: 23 | FLIT_USERNAME: __token__ 24 | FLIT_PASSWORD: ${{ secrets.PYPI_TOKEN }} 25 | -------------------------------------------------------------------------------- /docs/source/api.rst: -------------------------------------------------------------------------------- 1 | API 2 | === 3 | 4 | api 5 | --- 6 | 7 | The ``api.API`` object is a wrapper around ``tweepy.API`` with some additional methods for tracking keys. 8 | 9 | .. automodule:: twitter_bot_utils.api 10 | :members: 11 | :undoc-members: 12 | 13 | args 14 | ---- 15 | 16 | The ``args`` modules provides short-cuts for creating command-line tools for Twitter bots. 17 | 18 | See the :doc:`helloworld` for a fleshed-out example. 19 | 20 | .. automodule:: twitter_bot_utils.args 21 | :members: 22 | :undoc-members: 23 | 24 | helpers 25 | ------- 26 | 27 | These helpers are useful for interacting with the metadata in a Tweepy tweet object 28 | 29 | .. automodule:: twitter_bot_utils.helpers 30 | :members: 31 | :undoc-members: 32 | -------------------------------------------------------------------------------- /tests/test_args.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | import argparse 3 | import os.path 4 | import unittest 5 | 6 | from twitter_bot_utils import args 7 | 8 | 9 | class test_twitter_bot_utils(unittest.TestCase): 10 | # pylint: disable=invalid-name 11 | def setUp(self): 12 | self.screen_name = 'example_screen_name' 13 | 14 | self.parser = argparse.ArgumentParser(description='desc', parents=[args.parent()]) 15 | 16 | self.args = self.parser.parse_args(['-n', '-v']) 17 | 18 | self.txtfile = os.path.join(os.path.dirname(__file__), 'data', 'tweets.txt') 19 | self.archive = os.path.dirname(__file__) 20 | 21 | def test_args(self): 22 | assert self.args.dry_run is True 23 | assert self.args.verbose is True 24 | 25 | 26 | if __name__ == '__main__': 27 | unittest.main() 28 | -------------------------------------------------------------------------------- /tests/test_tools.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | import unittest 3 | 4 | from vcr import VCR 5 | 6 | from twitter_bot_utils import API, tools 7 | 8 | from .config import credentials 9 | 10 | vcr = VCR(filter_headers=['Authorization']) 11 | 12 | 13 | class testTools(unittest.TestCase): 14 | # pylint: disable=invalid-name 15 | def setUp(self): 16 | self.api = API(config_file=False, **credentials) 17 | 18 | @vcr.use_cassette('tests/fixtures/followback.yaml') 19 | def testAutofollow(self): 20 | tools.follow_back(self.api, dry_run=True) 21 | 22 | @vcr.use_cassette('tests/fixtures/unfollow.yaml') 23 | def testAutoUnfollow(self): 24 | tools.unfollow(self.api, dry_run=True) 25 | 26 | @vcr.use_cassette('tests/fixtures/favorite.yaml') 27 | def testFaveMentions(self): 28 | tools.fave_mentions(self.api, dry_run=True) 29 | -------------------------------------------------------------------------------- /docs/source/index.rst: -------------------------------------------------------------------------------- 1 | .. Twitter Bot Utils documentation master file, created by 2 | sphinx-quickstart on Sun Feb 14 17:14:34 2016. 3 | 4 | Twitter Bot Utils 5 | ================== 6 | 7 | The goal of Twitter Bot Utils is to ease the creation of 8 | creating Twitter bots by abstracting away the repetition involved in 9 | dealing with keys. 10 | 11 | Twitter bot utils is somewhat opinionated: it assumes that you want to 12 | create command line tools, and that you can store your keys in a basic text 13 | configuration files. See :doc:`./helloworld` for a basic run-through. 14 | 15 | Some additional documentation is in the `readme `__. 16 | 17 | This package is intended to assist with the creation of bots for artistic 18 | or personal projects. Don't use it to spam or harrass people. 19 | 20 | .. toctree:: 21 | :maxdepth: 2 22 | 23 | helloworld 24 | api 25 | cli 26 | 27 | Index 28 | ====== 29 | 30 | * :ref:`genindex` 31 | * :ref:`modindex` 32 | -------------------------------------------------------------------------------- /src/twitter_bot_utils/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2014-2020 Neil Freeman contact@fakeisthenewreal.org 2 | # This program is free software: you can redistribute it and/or modify 3 | # it under the terms of the GNU General Public License as published by 4 | # the Free Software Foundation, either version 3 of the License, or 5 | # (at your option) any later version. 6 | 7 | # This program is distributed in the hope that it will be useful, 8 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | # GNU General Public License for more details. 11 | 12 | # You should have received a copy of the GNU General Public License 13 | # along with this program. If not, see . 14 | 15 | """Twitter bot utils make it a little easier to set up a Twitter bot""" 16 | 17 | from . import api, archive, args, confighelper, helpers, tools 18 | from .api import API 19 | 20 | __version__ = '0.14.0' 21 | __author__ = 'Neil Freeman' 22 | __license__ = 'GPL-3.0' 23 | __all__ = ['api', 'args', 'archive', 'confighelper', 'helpers', 'tools'] 24 | -------------------------------------------------------------------------------- /.github/workflows/python.yaml: -------------------------------------------------------------------------------- 1 | name: test 2 | 3 | on: [push] 4 | 5 | jobs: 6 | test: 7 | runs-on: ubuntu-latest 8 | 9 | strategy: 10 | matrix: 11 | python-version: [3.9, "3.10", "3.11"] 12 | 13 | steps: 14 | - uses: actions/checkout@v3.5.2 15 | - name: Set up Python ${{ matrix.python-version }} 16 | uses: actions/setup-python@v4.5.0 17 | with: 18 | python-version: ${{ matrix.python-version }} 19 | cache: 'pip' 20 | cache-dependency-path: pyproject.toml 21 | 22 | - name: Install dependencies 23 | run: | 24 | python -m pip install --upgrade pip flit 25 | flit install 26 | flit install --deps production --extras test 27 | 28 | - name: Run Python tests 29 | run: make test 30 | env: 31 | TWITTER_SCREEN_NAME: ${{ secrets.TWITTER_SCREEN_NAME }} 32 | TWITTER_CONSUMER_KEY: ${{ secrets.TWITTER_CONSUMER_KEY }} 33 | TWITTER_CONSUMER_SECRET: ${{ secrets.TWITTER_CONSUMER_SECRET }} 34 | TWITTER_OAUTH_TOKEN: ${{ secrets.TWITTER_OAUTH_TOKEN }} 35 | TWITTER_OAUTH_SECRET: ${{ secrets.TWITTER_OAUTH_SECRET }} 36 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | # Copyright 2014-17 Neil Freeman contact@fakeisthenewreal.org 2 | # This program is free software: you can redistribute it and/or modify 3 | # it under the terms of the GNU General Public License as published by 4 | # the Free Software Foundation, either version 3 of the License, or 5 | # (at your option) any later version. 6 | 7 | # This program is distributed in the hope that it will be useful, 8 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | # GNU General Public License for more details. 11 | 12 | # You should have received a copy of the GNU General Public License 13 | # along with this program. If not, see . 14 | 15 | .PHONY: all 16 | all: docs.zip 17 | 18 | docs.zip: docs/source/conf.py $(wildcard docs/*.rst docs/*/*.rst twitter_bot_utils/*.py) 19 | python -m pip install '.[doc]' 20 | $(MAKE) -C docs html 21 | cd docs/_build/html; \ 22 | zip -qr ../../../$@ . -x '*/.DS_Store' .DS_Store 23 | 24 | .PHONY: test deploy clean 25 | test: 26 | python -m unittest tests/test*.py 27 | tbu --version 28 | tbu like --help >/dev/null 29 | tbu follow --help >/dev/null 30 | tbu auth --help >/dev/null 31 | 32 | format: 33 | isort src tests 34 | black src tests 35 | 36 | clean: ; rm -rf build dist 37 | -------------------------------------------------------------------------------- /tests/config.py: -------------------------------------------------------------------------------- 1 | import os 2 | 3 | credentials = dict( 4 | screen_name=os.environ.get('TWITTER_SCREEN_NAME'), 5 | consumer_key=os.environ.get('TWITTER_CONSUMER_KEY'), 6 | consumer_secret=os.environ.get('TWITTER_CONSUMER_SECRET'), 7 | secret=os.environ.get('TWITTER_OAUTH_SECRET'), 8 | token=os.environ.get('TWITTER_OAUTH_TOKEN'), 9 | ) 10 | 11 | example_tweet = { 12 | "source": """Twitter for iPhone""", 13 | "entities": { 14 | "user_mentions": [{"name": "John Doe", "screen_name": "twitter", "indices": [0, 8], "id_str": "1", "id": 1}], 15 | "media": [], 16 | "hashtags": [], 17 | "urls": [], 18 | "symbols": [], 19 | }, 20 | "in_reply_to_status_id_str": "318563540590010368", 21 | "id_str": "318565861172600832", 22 | "in_reply_to_user_id": 14155645, 23 | "text": "@twitter example tweet example tweet example tweet", 24 | "id": 318565861172600832, 25 | "in_reply_to_status_id": 318563540590010368, 26 | "in_reply_to_screen_name": "twitter", 27 | "in_reply_to_user_id_str": "14155645", 28 | "retweeted": None, 29 | "user": { 30 | "name": "Neil Freeman", 31 | "screen_name": "fitnr", 32 | "protected": False, 33 | "id_str": "6853512", 34 | "profile_image_url_https": "https://pbs.twimg.com/profile_images/431817496350314496/VGgzYAE7_normal.jpeg", 35 | "id": 6853512, 36 | "verified": False, 37 | }, 38 | } 39 | -------------------------------------------------------------------------------- /docs/source/cli.rst: -------------------------------------------------------------------------------- 1 | Command line tools 2 | ================== 3 | 4 | Twitter Bot Utils comes with the ``tbu`` command line tool, which has several subcommands: 5 | 6 | - tbu auth 7 | - tbu follow 8 | - tbu like 9 | - tbu post 10 | 11 | tbu auth 12 | ------------ 13 | 14 | :: 15 | 16 | usage: tbu auth [-h] [-c file] [--app app] [-s] [--consumer-key key] 17 | [--consumer-secret secret] [-V] 18 | 19 | Authorize an account with a twitter application. 20 | 21 | optional arguments: 22 | -h, --help show this help message and exit 23 | -c file config file 24 | --app app app name in config file 25 | -s, --save Save details to config file 26 | --consumer-key key consumer key (aka consumer token) 27 | --consumer-secret secret 28 | consumer secret 29 | -V, --version show program's version number and exit 30 | 31 | tbu follow 32 | ----------- 33 | 34 | :: 35 | 36 | usage: tbu follow [options] screen_name 37 | 38 | automatic following and unfollowing 39 | 40 | positional arguments: 41 | screen_name 42 | 43 | optional arguments: 44 | -h, --help show this help message and exit 45 | -U, --unfollow Unfollow those who don't follow you 46 | -c PATH, --config PATH 47 | bots config file (json or yaml) 48 | -n, --dry-run Don't actually do anything 49 | -v, --verbose Run talkatively 50 | -q, --quiet Run quietly 51 | -V, --version show program's version number and exit 52 | 53 | tbu like 54 | -------- 55 | 56 | :: 57 | 58 | usage: tbu like [options] screen_name 59 | 60 | fave/like mentions 61 | 62 | positional arguments: 63 | screen_name 64 | 65 | optional arguments: 66 | -h, --help show this help message and exit 67 | -c PATH, --config PATH 68 | bots config file (json or yaml) 69 | -n, --dry-run Don't actually do anything 70 | -v, --verbose Run talkatively 71 | -q, --quiet Run quietly 72 | -V, --version show program's version number and exit 73 | 74 | 75 | tbu post 76 | -------- 77 | 78 | :: 79 | 80 | usage: tbu post screen_name "update" [options] 81 | 82 | Post text to a given twitter account 83 | 84 | positional arguments: 85 | screen_name 86 | update 87 | 88 | optional arguments: 89 | -h, --help show this help message and exit 90 | -m MEDIA_FILE, --media-file MEDIA_FILE 91 | -c PATH, --config PATH 92 | bots config file (json or yaml) 93 | -n, --dry-run Don't actually do anything 94 | -v, --verbose Run talkatively 95 | -q, --quiet Run quietly 96 | -------------------------------------------------------------------------------- /src/twitter_bot_utils/archive.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # Copyright 2014-17 Neil Freeman contact@fakeisthenewreal.org 3 | # This program is free software: you can redistribute it and/or modify 4 | # it under the terms of the GNU General Public License as published by 5 | # the Free Software Foundation, either version 3 of the License, or 6 | # (at your option) any later version. 7 | 8 | # This program is distributed in the hope that it will be useful, 9 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 10 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 | # GNU General Public License for more details. 12 | 13 | # You should have received a copy of the GNU General Public License 14 | # along with this program. If not, see . 15 | 16 | """Utilities for reading twitter archives""" 17 | 18 | import csv 19 | import json 20 | from glob import iglob 21 | from os import path 22 | 23 | 24 | def read_csv(directory): 25 | """ 26 | Scrape a twitter archive csv, yielding tweet text. 27 | 28 | Args: 29 | directory (str): CSV file or (directory containing tweets.csv). 30 | field (str): Field with the tweet's text (default: text). 31 | fieldnames (list): The column names for a csv with no header. Must contain . 32 | Leave as None to read CSV header (default: None). 33 | 34 | Returns: 35 | generator 36 | """ 37 | if path.isdir(directory): 38 | csvfile = path.join(directory, "tweets.csv") 39 | else: 40 | csvfile = directory 41 | 42 | with open(csvfile, "r") as f: 43 | for tweet in csv.DictReader(f): 44 | try: 45 | tweet["text"] = tweet["text"].decode("utf-8") 46 | except AttributeError: 47 | pass 48 | 49 | yield tweet 50 | 51 | 52 | def read_json(directory, data_files="data/js/tweets/*.js"): 53 | """ 54 | Scrape a twitter archive file. 55 | Inspiration from https://github.com/mshea/Parse-Twitter-Archive 56 | """ 57 | files = path.join(directory, data_files) 58 | 59 | for fname in iglob(files): 60 | with open(fname, "r") as f: 61 | # Twitter's JSON first line is bogus 62 | data = f.readlines()[1:] 63 | data = "".join(data) 64 | tweetlist = json.loads(data) 65 | 66 | for tweet in tweetlist: 67 | yield tweet 68 | 69 | 70 | def read_text(data_file): 71 | """ 72 | Read a text file containing one tweet per lint, yielding a generator 73 | that returns one tweet at a time. 74 | 75 | Args: 76 | data_file (str): Name of file 77 | 78 | Returns: 79 | generator 80 | """ 81 | with open(data_file, "r") as f: 82 | data = f.readlines() 83 | 84 | for tweet in data: 85 | try: 86 | yield tweet.rstrip().decode("utf-8") 87 | except AttributeError: 88 | yield tweet.rstrip() 89 | -------------------------------------------------------------------------------- /pyproject.toml: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # Copyright 2014-2021 Neil Freeman contact@fakeisthenewreal.org 3 | # This program is free software: you can redistribute it and/or modify 4 | # it under the terms of the GNU General Public License as published by 5 | # the Free Software Foundation, either version 3 of the License, or 6 | # (at your option) any later version. 7 | 8 | # This program is distributed in the hope that it will be useful, 9 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 10 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 | # GNU General Public License for more details. 12 | 13 | # You should have received a copy of the GNU General Public License 14 | # along with this program. If not, see . 15 | [build-system] 16 | requires = ["flit_core >=3.2,<4"] 17 | build-backend = "flit_core.buildapi" 18 | 19 | [project] 20 | name = "twitter_bot_utils" 21 | description = "Python utilities for twitter bots" 22 | readme = "README.md" 23 | authors = [ 24 | { name = "Neil Freeman", email = "contact@fakeisthenewreal.org"} 25 | ] 26 | classifiers = [ 27 | "License :: OSI Approved :: GNU General Public License v3 or later (GPLv3+)", 28 | "Development Status :: 4 - Beta", 29 | "Intended Audience :: Developers", 30 | "Programming Language :: Python :: 3.6", 31 | "Programming Language :: Python :: 3.7", 32 | "Programming Language :: Python :: 3.8", 33 | "Programming Language :: Python :: 3.9", 34 | "Programming Language :: Python :: 3.10", 35 | "Operating System :: OS Independent", 36 | ] 37 | dynamic = ["version"] 38 | dependencies = [ 39 | "tweepy >=4.6, <5", 40 | "pyYAML>=4.2" 41 | ] 42 | 43 | [project.license] 44 | file = "LICENSE" 45 | 46 | [project.urls] 47 | Home = "https://github.com/fitnr/twitter_bot_utils" 48 | 49 | [project.scripts] 50 | tbu = "twitter_bot_utils.cli:main" 51 | 52 | [project.optional-dependencies] 53 | test = [ 54 | "vcrpy==4.1.1", 55 | "mock" 56 | ] 57 | doc = [ 58 | "sphinx", 59 | "sphinx_rtd_theme" 60 | ] 61 | pylint = ["pylint"] 62 | 63 | [tool.tox] 64 | legacy_tox_ini = """ 65 | [tox] 66 | envlist = py35, py36, py37, py38, py39, py310, pylint 67 | isolated_build = true 68 | 69 | [testenv] 70 | deps = .[test] 71 | passenv = 72 | TWITTER_SCREEN_NAME 73 | TWITTER_CONSUMER_KEY 74 | TWITTER_CONSUMER_SECRET 75 | TWITTER_OAUTH_TOKEN 76 | TWITTER_OAUTH_SECRET 77 | 78 | commands = make test 79 | 80 | whitelist_externals = make 81 | 82 | [testenv:pylint] 83 | deps = .[test,pylint] 84 | commands = 85 | pylint src 86 | pylint -d missing-module-docstring,missing-function-docstring,missing-class-docstring tests 87 | 88 | """ 89 | 90 | [tool.black] 91 | line-length = 120 92 | target-version = ["py38"] 93 | include = 'py$' 94 | skip-string-normalization = true 95 | 96 | [tool.pylint.master] 97 | fail-under = "9.5" 98 | 99 | [tool.pylint.basic] 100 | good-names = "f, i, j, k, x, y, z" 101 | 102 | [tool.pylint.message_control] 103 | disable = "print-statement" 104 | 105 | [tool.pylint.format] 106 | max-line-length = 120 107 | 108 | [tool.isort] 109 | line_length = 120 110 | -------------------------------------------------------------------------------- /tests/test_confighelper.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | import os 3 | import unittest 4 | 5 | from twitter_bot_utils import confighelper 6 | 7 | 8 | class test_confighelper(unittest.TestCase): 9 | # pylint: disable=invalid-name 10 | screen_name = 'example_screen_name' 11 | 12 | def setUp(self): 13 | self.datapath = os.path.join(os.path.dirname(__file__), 'data') 14 | self.yaml = os.path.join(self.datapath, 'test.yaml') 15 | self.simple = os.path.join(self.datapath, 'simple.yml') 16 | self.badfile = os.path.join(self.datapath, 'tweets.txt') 17 | 18 | def testDefaultDirs(self): 19 | self.assertIn('~', confighelper.CONFIG_DIRS) 20 | 21 | def test_find_file(self): 22 | for f in (self.simple, self.yaml): 23 | self.assertEqual(f, confighelper.find_file(f)) 24 | self.assertEqual( 25 | f, confighelper.find_file(default_bases=(os.path.basename(f),), default_directories=[self.datapath]) 26 | ) 27 | 28 | def test_yaml(self): 29 | config = confighelper.configure(self.screen_name, config_file=self.yaml) 30 | self.assertEqual(config['key'], 'INDIA') 31 | self.assertEqual(config["consumer_key"], "NOVEMBER") 32 | 33 | def test_simple(self): 34 | config = confighelper.configure(config_file=self.simple) 35 | self.assertEqual(config['key'], 'INDIA') 36 | self.assertEqual(config["consumer_key"], "NOVEMBER") 37 | 38 | def test_parse(self): 39 | with self.assertRaises(ValueError): 40 | confighelper.parse('foo.unknown') 41 | 42 | def testConfigKwargPassing(self): 43 | conf = confighelper.parse(self.yaml) 44 | config = confighelper.configure(config_file=self.yaml, **conf) 45 | assert conf['custom'] == config['custom'] 46 | 47 | def testConfigBadFileType(self): 48 | with self.assertRaises(ValueError): 49 | confighelper.parse(self.badfile) 50 | 51 | def testDumpConfig(self): 52 | conf = confighelper.parse(self.yaml) 53 | sink = 'a.yaml' 54 | confighelper.dump(conf, sink) 55 | 56 | dumped = confighelper.parse(sink) 57 | 58 | assert dumped['custom'] == conf['custom'] 59 | assert 'users' in dumped 60 | 61 | os.remove(sink) 62 | 63 | def testDumpConfigBadFileType(self): 64 | with self.assertRaises(ValueError): 65 | confighelper.dump({}, 'foo.whatever') 66 | 67 | def testMissingConfig(self): 68 | with self.assertRaises(Exception): 69 | confighelper.find_file('imaginary.yaml', (os.path.dirname(__file__),)) 70 | 71 | def test_config_setup(self): 72 | config = confighelper.configure(self.screen_name, config_file=self.yaml, random='foo') 73 | 74 | assert config['secret'] == 'LIMA' 75 | assert config['consumer_key'] == 'NOVEMBER' 76 | assert config['random'] == 'foo' 77 | 78 | def testSimpleConfig(self): 79 | config = confighelper.configure(config_file=self.simple, random='foo') 80 | assert config['secret'] == 'LIMA' 81 | assert config['consumer_key'] == 'NOVEMBER' 82 | assert config['random'] == 'foo' 83 | 84 | if __name__ == '__main__': 85 | unittest.main() 86 | -------------------------------------------------------------------------------- /tests/fixtures/favorite.yaml: -------------------------------------------------------------------------------- 1 | interactions: 2 | - request: 3 | body: null 4 | headers: 5 | Host: [api.twitter.com] 6 | method: GET 7 | uri: https://api.twitter.com/1.1/favorites/list.json?include_entities=False&count=150 8 | response: 9 | body: {string: '[]'} 10 | headers: 11 | cache-control: ['no-cache, no-store, must-revalidate, pre-check=0, post-check=0'] 12 | content-disposition: [attachment; filename=json.json] 13 | content-length: ['2'] 14 | content-type: [application/json;charset=utf-8] 15 | date: ['Thu, 30 Nov 2017 19:41:15 GMT'] 16 | expires: ['Tue, 31 Mar 1981 05:00:00 GMT'] 17 | last-modified: ['Thu, 30 Nov 2017 19:41:14 GMT'] 18 | pragma: [no-cache] 19 | server: [tsa_b] 20 | set-cookie: ['personalization_id="v1_eHdv8hsFSqHLvdxnd2SQSg=="; Expires=Sat, 21 | 30 Nov 2019 19:41:15 UTC; Path=/; Domain=.twitter.com', lang=en; Path=/, 22 | 'guest_id=v1%3A151207087494775911; Expires=Sat, 30 Nov 2019 19:41:14 UTC; 23 | Path=/; Domain=.twitter.com'] 24 | status: [200 OK] 25 | strict-transport-security: [max-age=631138519] 26 | x-access-level: [read-write] 27 | x-connection-hash: [08af3ea9d9981da5fe3dc8e6f93f94e8] 28 | x-content-type-options: [nosniff] 29 | x-frame-options: [SAMEORIGIN] 30 | x-rate-limit-limit: ['75'] 31 | x-rate-limit-remaining: ['73'] 32 | x-rate-limit-reset: ['1512071735'] 33 | x-response-time: ['68'] 34 | x-transaction: [005200ef0001a47f] 35 | x-twitter-response-tags: [BouncerCompliant] 36 | x-xss-protection: [1; mode=block] 37 | status: {code: 200, message: OK} 38 | - request: 39 | body: null 40 | headers: 41 | Host: [api.twitter.com] 42 | method: GET 43 | uri: https://api.twitter.com/1.1/statuses/mentions_timeline.json?include_entities=False&trim_user=True&count=75 44 | response: 45 | body: {string: '[]'} 46 | headers: 47 | cache-control: ['no-cache, no-store, must-revalidate, pre-check=0, post-check=0'] 48 | content-disposition: [attachment; filename=json.json] 49 | content-length: ['2'] 50 | content-type: [application/json;charset=utf-8] 51 | date: ['Thu, 30 Nov 2017 19:41:15 GMT'] 52 | expires: ['Tue, 31 Mar 1981 05:00:00 GMT'] 53 | last-modified: ['Thu, 30 Nov 2017 19:41:15 GMT'] 54 | pragma: [no-cache] 55 | server: [tsa_b] 56 | set-cookie: ['personalization_id="v1_0rthq95O9jr5f+1SQ+rAOQ=="; Expires=Sat, 57 | 30 Nov 2019 19:41:15 UTC; Path=/; Domain=.twitter.com', lang=en; Path=/, 58 | 'guest_id=v1%3A151207087530787332; Expires=Sat, 30 Nov 2019 19:41:15 UTC; 59 | Path=/; Domain=.twitter.com'] 60 | status: [200 OK] 61 | strict-transport-security: [max-age=631138519] 62 | x-access-level: [read-write] 63 | x-connection-hash: [2593e3d6edc88c780b65605ae30424a3] 64 | x-content-type-options: [nosniff] 65 | x-frame-options: [SAMEORIGIN] 66 | x-rate-limit-limit: ['75'] 67 | x-rate-limit-remaining: ['73'] 68 | x-rate-limit-reset: ['1512071735'] 69 | x-response-time: ['175'] 70 | x-transaction: [00b6738c00a4a2a9] 71 | x-twitter-response-tags: [BouncerCompliant] 72 | x-xss-protection: [1; mode=block] 73 | status: {code: 200, message: OK} 74 | version: 1 75 | -------------------------------------------------------------------------------- /docs/source/helloworld.rst: -------------------------------------------------------------------------------- 1 | Hello World 2 | =========== 3 | 4 | The first step of any bot is to set up an app and a new account. Those steps 5 | are an exercise for the reader. 6 | 7 | Twitter Bot Utils is opinionated about one thing: it wants you to store authentication 8 | keys in a file called ``~/bots.yaml`` or ``~/bots.json``. (It's actually not that opinionated 9 | about where the file goes, read on.) 10 | 11 | If you're using a YAML file, it should look like this: 12 | 13 | .. code:: yaml 14 | 15 | apps: 16 | my_app_name: 17 | consumer_key: LONGSTRINGOFLETTERS-ANDNUMBERS 18 | consumer_secret: LETTERSANDNUMBERS 19 | users: 20 | # twitter screen_name 21 | MyBotName: 22 | key: LONGSTRINGOFLETTERS-ANDNUMBERS 23 | secret: LETTERSANDNUMBERS 24 | # The app key should match a key in apps below 25 | app: my_app_name 26 | 27 | 28 | Wait, we haven't authenticated the account with the app. Let's do that quickly 29 | with the Twitter Bot Utils ``twitter-auth`` command: 30 | 31 | .. code:: bash 32 | 33 | $ twitter-auth --app my_app_name 34 | https://api.twitter.com/oauth/authorize?oauth_token=dWXqSAAAAAAALgurAAABUuIOe0c 35 | Please visit this url, click "Authorize app" and enter in the PIN: 36 | > 37 | 38 | 39 | Now visit the URL in your favorite browser, authorize the app, and you'll be rewarded with 40 | key and secret, which you can place in ``bots.yaml``. 41 | 42 | Next, create a python file called ``my_twitter_bot.py`` that looks like this: 43 | 44 | .. code:: python 45 | 46 | import argparse 47 | import twitter_bot_utils as tbu 48 | 49 | def main(): 50 | parser = argparse.ArgumentParser(description='my twitter bot') 51 | tbu.args.add_default_args(parser, version='1.0') 52 | 53 | args = parser.parse_args() 54 | api = tbu.api.API(args.user) 55 | 56 | if not args.dry_run: 57 | api.update_status('Hello World!') 58 | api.logger.info('I just tweeted!') 59 | 60 | if __name__ == '__main__': 61 | main() 62 | 63 | 64 | On the command line, this will create a full-fledged app that will have lots of tricks: 65 | 66 | .. code:: bash 67 | 68 | $ python my_twitter_bot.py --help 69 | usage: my_twitter_bot.py [-h] [-c PATH] [-n] [-v] [-q] [-V] [-u screen_name] 70 | 71 | my twitter bot 72 | 73 | optional arguments: 74 | -h, --help show this help message and exit 75 | -c PATH, --config PATH 76 | bots config file (json or yaml) 77 | -n, --dry-run Don't actually do anything 78 | -v, --verbose Run talkatively 79 | -q, --quiet Run quietly 80 | -V, --version show program's version number and exit 81 | 82 | 83 | To tweet, run this: 84 | 85 | .. code:: bash 86 | 87 | $ python my_twitter_bot.py -u MyBotName 88 | I just tweeted! 89 | 90 | Now you can go ahead and add this command to ``cron``, and you're good to go! 91 | 92 | Another approach 93 | ---------------- 94 | 95 | Create the ``bots.yaml`` file as above, but when creating your bot, just set it to print a tweet: 96 | 97 | .. code:: python 98 | 99 | def main(): 100 | print('This is a tweet!') 101 | 102 | if __name__ == '__main__': 103 | main() 104 | 105 | Now, pipe your scripts output to the ``tbu post`` command: 106 | 107 | .. code:: bash 108 | 109 | $ python3 my_twitter_bot.py | tbu post MyBotName 110 | -------------------------------------------------------------------------------- /tests/fixtures/followback.yaml: -------------------------------------------------------------------------------- 1 | interactions: 2 | - request: 3 | body: null 4 | headers: 5 | Host: [api.twitter.com] 6 | method: GET 7 | uri: https://api.twitter.com/1.1/followers/ids.json 8 | response: 9 | body: {string: '{"ids":[],"next_cursor":0,"next_cursor_str":"0","previous_cursor":0,"previous_cursor_str":"0"}'} 10 | headers: 11 | cache-control: ['no-cache, no-store, must-revalidate, pre-check=0, post-check=0'] 12 | content-disposition: [attachment; filename=json.json] 13 | content-length: ['94'] 14 | content-type: [application/json;charset=utf-8] 15 | date: ['Thu, 30 Nov 2017 19:41:14 GMT'] 16 | expires: ['Tue, 31 Mar 1981 05:00:00 GMT'] 17 | last-modified: ['Thu, 30 Nov 2017 19:41:14 GMT'] 18 | pragma: [no-cache] 19 | server: [tsa_b] 20 | set-cookie: ['personalization_id="v1_XcxzAU+gmk/GCT5qVNndpg=="; Expires=Sat, 21 | 30 Nov 2019 19:41:14 UTC; Path=/; Domain=.twitter.com', lang=en; Path=/, 22 | 'guest_id=v1%3A151207087428326344; Expires=Sat, 30 Nov 2019 19:41:14 UTC; 23 | Path=/; Domain=.twitter.com'] 24 | status: [200 OK] 25 | strict-transport-security: [max-age=631138519] 26 | x-access-level: [read-write] 27 | x-connection-hash: [9bd3fb42c3f63913f4e285a5981553e7] 28 | x-content-type-options: [nosniff] 29 | x-frame-options: [SAMEORIGIN] 30 | x-rate-limit-limit: ['15'] 31 | x-rate-limit-remaining: ['11'] 32 | x-rate-limit-reset: ['1512071733'] 33 | x-response-time: ['27'] 34 | x-transaction: [0042022c005c9900] 35 | x-twitter-response-tags: [BouncerCompliant] 36 | x-xss-protection: [1; mode=block] 37 | status: {code: 200, message: OK} 38 | - request: 39 | body: null 40 | headers: 41 | Authorization: ['OAuth oauth_nonce="83137076989014121471512070874", oauth_timestamp="1512070874", 42 | oauth_version="1.0", oauth_signature_method="HMAC-SHA1", oauth_consumer_key="OrmGEg8ipfgyukzZpkUzHQ", 43 | oauth_token="936312048459374592-55nmHyOZ7Z9UdowNU9DNAy2JtC6ZAzg", oauth_signature="F4LQCqDB1GfvIHGcviDGftBamZk%3D"'] 44 | Host: [api.twitter.com] 45 | method: GET 46 | uri: https://api.twitter.com/1.1/friends/ids.json 47 | response: 48 | body: {string: '{"ids":[6853512],"next_cursor":0,"next_cursor_str":"0","previous_cursor":0,"previous_cursor_str":"0"}'} 49 | headers: 50 | cache-control: ['no-cache, no-store, must-revalidate, pre-check=0, post-check=0'] 51 | content-disposition: [attachment; filename=json.json] 52 | content-length: ['101'] 53 | content-type: [application/json;charset=utf-8] 54 | date: ['Thu, 30 Nov 2017 19:41:14 GMT'] 55 | expires: ['Tue, 31 Mar 1981 05:00:00 GMT'] 56 | last-modified: ['Thu, 30 Nov 2017 19:41:14 GMT'] 57 | pragma: [no-cache] 58 | server: [tsa_b] 59 | set-cookie: ['personalization_id="v1_t9Mf+aUbU/aDtkUdgLilvg=="; Expires=Sat, 60 | 30 Nov 2019 19:41:14 UTC; Path=/; Domain=.twitter.com', lang=en; Path=/, 61 | 'guest_id=v1%3A151207087451214781; Expires=Sat, 30 Nov 2019 19:41:14 UTC; 62 | Path=/; Domain=.twitter.com'] 63 | status: [200 OK] 64 | strict-transport-security: [max-age=631138519] 65 | x-access-level: [read-write] 66 | x-connection-hash: [40e16dda32f5e30ab76bf3975fe2b673] 67 | x-content-type-options: [nosniff] 68 | x-frame-options: [SAMEORIGIN] 69 | x-rate-limit-limit: ['15'] 70 | x-rate-limit-remaining: ['11'] 71 | x-rate-limit-reset: ['1512071734'] 72 | x-response-time: ['20'] 73 | x-transaction: [0067642000ca5931] 74 | x-twitter-response-tags: [BouncerCompliant] 75 | x-xss-protection: [1; mode=block] 76 | status: {code: 200, message: OK} 77 | version: 1 78 | -------------------------------------------------------------------------------- /tests/fixtures/unfollow.yaml: -------------------------------------------------------------------------------- 1 | interactions: 2 | - request: 3 | body: null 4 | headers: 5 | Host: [api.twitter.com] 6 | method: GET 7 | uri: https://api.twitter.com/1.1/followers/ids.json 8 | response: 9 | body: {string: '{"ids":[],"next_cursor":0,"next_cursor_str":"0","previous_cursor":0,"previous_cursor_str":"0"}'} 10 | headers: 11 | cache-control: ['no-cache, no-store, must-revalidate, pre-check=0, post-check=0'] 12 | content-disposition: [attachment; filename=json.json] 13 | content-length: ['94'] 14 | content-type: [application/json;charset=utf-8] 15 | date: ['Thu, 30 Nov 2017 19:41:13 GMT'] 16 | expires: ['Tue, 31 Mar 1981 05:00:00 GMT'] 17 | last-modified: ['Thu, 30 Nov 2017 19:41:13 GMT'] 18 | pragma: [no-cache] 19 | server: [tsa_b] 20 | set-cookie: ['personalization_id="v1_5dacL5TCYFzpplP3m50h5A=="; Expires=Sat, 21 | 30 Nov 2019 19:41:13 UTC; Path=/; Domain=.twitter.com', lang=en; Path=/, 22 | 'guest_id=v1%3A151207087375368846; Expires=Sat, 30 Nov 2019 19:41:13 UTC; 23 | Path=/; Domain=.twitter.com'] 24 | status: [200 OK] 25 | strict-transport-security: [max-age=631138519] 26 | x-access-level: [read-write] 27 | x-connection-hash: [3df19d3cf42248066571c1209cc642c4] 28 | x-content-type-options: [nosniff] 29 | x-frame-options: [SAMEORIGIN] 30 | x-rate-limit-limit: ['15'] 31 | x-rate-limit-remaining: ['12'] 32 | x-rate-limit-reset: ['1512071733'] 33 | x-response-time: ['24'] 34 | x-transaction: [00240b0300f5728d] 35 | x-twitter-response-tags: [BouncerCompliant] 36 | x-xss-protection: [1; mode=block] 37 | status: {code: 200, message: OK} 38 | - request: 39 | body: null 40 | headers: 41 | Authorization: ['OAuth oauth_nonce="33809483964833820151512070873", oauth_timestamp="1512070873", 42 | oauth_version="1.0", oauth_signature_method="HMAC-SHA1", oauth_consumer_key="OrmGEg8ipfgyukzZpkUzHQ", 43 | oauth_token="936312048459374592-55nmHyOZ7Z9UdowNU9DNAy2JtC6ZAzg", oauth_signature="n%2BmOwPTemyYnSuzYdUahqfoirsw%3D"'] 44 | Host: [api.twitter.com] 45 | method: GET 46 | uri: https://api.twitter.com/1.1/friends/ids.json 47 | response: 48 | body: {string: '{"ids":[6853512],"next_cursor":0,"next_cursor_str":"0","previous_cursor":0,"previous_cursor_str":"0"}'} 49 | headers: 50 | cache-control: ['no-cache, no-store, must-revalidate, pre-check=0, post-check=0'] 51 | content-disposition: [attachment; filename=json.json] 52 | content-length: ['101'] 53 | content-type: [application/json;charset=utf-8] 54 | date: ['Thu, 30 Nov 2017 19:41:13 GMT'] 55 | expires: ['Tue, 31 Mar 1981 05:00:00 GMT'] 56 | last-modified: ['Thu, 30 Nov 2017 19:41:13 GMT'] 57 | pragma: [no-cache] 58 | server: [tsa_b] 59 | set-cookie: ['personalization_id="v1_geitFgNGUeiZesBQh/lEhQ=="; Expires=Sat, 60 | 30 Nov 2019 19:41:13 UTC; Path=/; Domain=.twitter.com', lang=en; Path=/, 61 | 'guest_id=v1%3A151207087395890000; Expires=Sat, 30 Nov 2019 19:41:13 UTC; 62 | Path=/; Domain=.twitter.com'] 63 | status: [200 OK] 64 | strict-transport-security: [max-age=631138519] 65 | x-access-level: [read-write] 66 | x-connection-hash: [c71c9942929e1918cb9a82ee769bb943] 67 | x-content-type-options: [nosniff] 68 | x-frame-options: [SAMEORIGIN] 69 | x-rate-limit-limit: ['15'] 70 | x-rate-limit-remaining: ['12'] 71 | x-rate-limit-reset: ['1512071734'] 72 | x-response-time: ['23'] 73 | x-transaction: [006d69a2000c4b41] 74 | x-twitter-response-tags: [BouncerCompliant] 75 | x-xss-protection: [1; mode=block] 76 | status: {code: 200, message: OK} 77 | version: 1 78 | -------------------------------------------------------------------------------- /tests/test_archive.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | import argparse 3 | import inspect 4 | import os 5 | import sys 6 | import unittest 7 | 8 | import tweepy 9 | 10 | from twitter_bot_utils import archive, args, confighelper 11 | 12 | from .config import example_tweet 13 | 14 | 15 | class test_twitter_bot_utils(unittest.TestCase): 16 | # pylint: disable=invalid-name 17 | screen_name = 'example_screen_name' 18 | 19 | archive = os.path.dirname(__file__) 20 | csvfile = os.path.join(os.path.dirname(__file__), 'data', 'tweets.csv') 21 | txtfile = os.path.join(os.path.dirname(__file__), 'data', 'tweets.txt') 22 | yaml = os.path.join(os.path.dirname(__file__), 'data', 'test.yaml') 23 | 24 | def setUp(self): 25 | self.api = tweepy.API() 26 | self.status = tweepy.models.Status.parse(self.api, example_tweet) 27 | 28 | parent = args.parent(version='1.2.3') 29 | self.parser = argparse.ArgumentParser(description='desc', parents=[parent]) 30 | 31 | sys.argv = ['test', '--dry-run', '-v', '-c', self.yaml] 32 | self.args = self.parser.parse_args() 33 | 34 | def test_setup(self): 35 | assert isinstance(self.parser, argparse.ArgumentParser) 36 | assert self.parser.description == 'desc' 37 | 38 | def test_parsing_args(self): 39 | assert self.args.dry_run 40 | assert self.args.verbose 41 | 42 | def test_find_conf_file(self): 43 | assert confighelper.find_file(self.yaml) == self.yaml 44 | real_path = os.path.realpath(os.path.dirname(__file__)) 45 | 46 | self.assertEqual( 47 | confighelper.find_file(default_directories=[real_path], default_bases=[self.yaml]), 48 | os.path.realpath(self.yaml), 49 | ) 50 | 51 | def test_parse(self): 52 | parsed = confighelper.parse(self.yaml) 53 | assert parsed['users']['example_screen_name']['key'] == 'INDIA' 54 | self.assertEqual(parsed['custom'], 'general') 55 | self.assertEqual(parsed['users']['example_screen_name']['custom'], 'user') 56 | 57 | def test_loading_archive_data(self): 58 | archives = archive.read_json(self.archive) 59 | assert inspect.isgenerator(archives) 60 | a = list(archives) 61 | self.assertEqual(len(a), 3) 62 | 63 | self.assertEqual(a[2]['text'], u"#ééé #buttons") 64 | 65 | def test_loading_csv_data(self): 66 | txt = archive.read_csv(self.csvfile) 67 | assert inspect.isgenerator(txt) 68 | tweets = list(txt) 69 | 70 | try: 71 | self.assertIsInstance(tweets[0]['text'], unicode) 72 | assert isinstance(tweets[4]['text'], unicode) 73 | except NameError: 74 | assert isinstance(tweets[0]['text'], str) 75 | assert isinstance(tweets[4]['text'], str) 76 | 77 | self.assertEqual(len(tweets), 6) 78 | self.assertEqual(tweets[4]['text'], u'Tweet with an åccent') 79 | self.assertEqual(tweets[5]['text'], u'Tweet with an 😀 emoji') 80 | 81 | def test_loading_text_data(self): 82 | txt = archive.read_text(self.txtfile) 83 | assert inspect.isgenerator(txt) 84 | tweets = list(txt) 85 | 86 | try: 87 | self.assertIsInstance(tweets[0], unicode) 88 | assert isinstance(tweets[4], unicode) 89 | except NameError: 90 | assert isinstance(tweets[0], str) 91 | assert isinstance(tweets[4], str) 92 | 93 | self.assertEqual(len(tweets), 6) 94 | self.assertEqual(tweets[4], u'Tweet with an åccent') 95 | self.assertEqual(tweets[5], u'Tweet with an 😀 emoji') 96 | 97 | 98 | if __name__ == '__main__': 99 | unittest.main() 100 | -------------------------------------------------------------------------------- /tests/test_api.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | import argparse 3 | import logging 4 | import os 5 | import unittest 6 | 7 | import tweepy 8 | from vcr import VCR 9 | 10 | from twitter_bot_utils import api, confighelper 11 | 12 | from .config import credentials 13 | 14 | vcr = VCR(filter_headers=['Authorization']) 15 | 16 | 17 | class test_twitter_bot_utils(unittest.TestCase): 18 | # pylint: disable=invalid-name 19 | _api = None 20 | 21 | def __init__(self, *args, **kwargs): 22 | log = logging.getLogger("vcr") 23 | log.setLevel(logging.ERROR) 24 | super().__init__(*args, **kwargs) 25 | 26 | def setUp(self): 27 | self.yaml = os.path.join(os.path.dirname(__file__), 'data', 'test.yaml') 28 | self.simple = os.path.join(os.path.dirname(__file__), 'data', 'simple.yml') 29 | self.txtfile = os.path.join(os.path.dirname(__file__), 'data', 'tweets.txt') 30 | self.archive = os.path.dirname(__file__) 31 | 32 | @vcr.use_cassette('tests/fixtures/verify_credentials.yaml') 33 | def api(self): 34 | if not self._api: 35 | self._api = api.API(config_file=False, **credentials) 36 | self.assertEqual(self._api.screen_name, credentials['screen_name']) 37 | 38 | return self._api 39 | 40 | def testApiSetup(self): 41 | self.api() 42 | 43 | def test_api_creation_ns(self): 44 | args = argparse.Namespace(**credentials) 45 | twitter = api.API(args, config_file=self.yaml) 46 | assert isinstance(twitter, api.API) 47 | 48 | def test_api_missing_config(self): 49 | # Missing file raises IO Error 50 | self.assertRaises(IOError, api.API, 'example', config_file='dfV35d/does/not/exist/982') 51 | 52 | def test_api_broken_config(self): 53 | brokenconfig = os.path.join(os.path.dirname(__file__), 'data', 'broken.yaml') 54 | 55 | # Broken file raises ValueError 56 | self.assertRaises(ValueError, api.API, use_env=False, config_file=brokenconfig) 57 | 58 | def testEnviron(self): 59 | twitter = self.api() 60 | self.assertEqual(twitter.screen_name, credentials['screen_name']) 61 | self.assertEqual(twitter.auth.consumer_key, credentials['consumer_key']) 62 | self.assertEqual(twitter.auth.consumer_secret, credentials['consumer_secret']) 63 | self.assertEqual(twitter.auth.access_token, credentials['token']) 64 | self.assertEqual(twitter.auth.access_token_secret, credentials['secret']) 65 | 66 | def testApiAttributes(self): 67 | twitter = api.API(screen_name='example_screen_name', config_file=self.yaml, use_env=False) 68 | 69 | assert twitter.config['custom'] == 'user' 70 | assert twitter.screen_name == 'example_screen_name' 71 | assert twitter.app == 'example_app_name' 72 | 73 | @vcr.use_cassette('tests/fixtures/user_timeline.yaml') 74 | def test_recent_tweets(self): 75 | twitter = self.api() 76 | self.assertEqual(twitter.last_tweet, 936319185277341697) 77 | 78 | @vcr.use_cassette('tests/fixtures/status.yaml') 79 | def test_user_status(self): 80 | twitter = self.api() 81 | 82 | status1 = super(api.API, twitter).update_status("Just running some tests...") 83 | self.assertIsNotNone(status1) 84 | assert status1.text == "Just running some tests..." 85 | 86 | status = twitter.update_status("Just running some more tests...") 87 | self.assertIsNotNone(status, 'Returned status object is None') 88 | assert status.text == "Just running some more tests..." 89 | 90 | def testSetupAuth(self): 91 | auth = confighelper.setup_auth(**credentials) 92 | self.assertIsInstance(auth, tweepy.OAuth1UserHandler) 93 | 94 | 95 | if __name__ == '__main__': 96 | unittest.main() 97 | -------------------------------------------------------------------------------- /tests/fixtures/verify_credentials.yaml: -------------------------------------------------------------------------------- 1 | interactions: 2 | - request: 3 | body: null 4 | headers: 5 | Host: [api.twitter.com] 6 | method: GET 7 | uri: https://api.twitter.com/1.1/account/verify_credentials.json 8 | response: 9 | body: {string: '{"id":936312048459374592,"id_str":"936312048459374592","name":"bot 10 | utils","screen_name":"botutils","location":"","description":"","url":"https:\/\/t.co\/nsX8IijSfN","entities":{"url":{"urls":[{"url":"https:\/\/t.co\/nsX8IijSfN","expanded_url":"http:\/\/github.com\/fitnr\/twitter_bot_utils","display_url":"github.com\/fitnr\/twitter_\u2026","indices":[0,23]}]},"description":{"urls":[]}},"protected":false,"followers_count":0,"friends_count":1,"listed_count":0,"created_at":"Thu 11 | Nov 30 19:12:42 +0000 2017","favourites_count":0,"utc_offset":-28800,"time_zone":"Pacific 12 | Time (US & Canada)","geo_enabled":false,"verified":false,"statuses_count":3,"lang":"en","status":{"created_at":"Thu 13 | Nov 30 19:45:48 +0000 2017","id":936320375775989762,"id_str":"936320375775989762","text":"Just 14 | running some more tests...","truncated":false,"entities":{"hashtags":[],"symbols":[],"user_mentions":[],"urls":[]},"source":"\u003ca 15 | href=\"http:\/\/fakeisthenewreal.org\/strangeallure\" rel=\"nofollow\"\u003estrange 16 | allure\u003c\/a\u003e","in_reply_to_status_id":null,"in_reply_to_status_id_str":null,"in_reply_to_user_id":null,"in_reply_to_user_id_str":null,"in_reply_to_screen_name":null,"geo":null,"coordinates":null,"place":null,"contributors":null,"is_quote_status":false,"retweet_count":0,"favorite_count":0,"favorited":false,"retweeted":false,"lang":"en"},"contributors_enabled":false,"is_translator":false,"is_translation_enabled":false,"profile_background_color":"F5F8FA","profile_background_image_url":null,"profile_background_image_url_https":null,"profile_background_tile":false,"profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/936315057100558336\/o6-BGpHe_normal.jpg","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/936315057100558336\/o6-BGpHe_normal.jpg","profile_link_color":"1DA1F2","profile_sidebar_border_color":"C0DEED","profile_sidebar_fill_color":"DDEEF6","profile_text_color":"333333","profile_use_background_image":true,"has_extended_profile":false,"default_profile":true,"default_profile_image":false,"following":false,"follow_request_sent":false,"notifications":false,"translator_type":"none"}'} 17 | headers: 18 | cache-control: ['no-cache, no-store, must-revalidate, pre-check=0, post-check=0'] 19 | content-disposition: [attachment; filename=json.json] 20 | content-length: ['2132'] 21 | content-type: [application/json;charset=utf-8] 22 | date: ['Thu, 30 Nov 2017 23:44:10 GMT'] 23 | expires: ['Tue, 31 Mar 1981 05:00:00 GMT'] 24 | last-modified: ['Thu, 30 Nov 2017 23:44:10 GMT'] 25 | pragma: [no-cache] 26 | server: [tsa_b] 27 | set-cookie: ['personalization_id="v1_01mRCunzCAmKDJJXMyJtew=="; Expires=Sat, 28 | 30 Nov 2019 23:44:10 UTC; Path=/; Domain=.twitter.com', lang=en; Path=/, 29 | 'guest_id=v1%3A151208545027317458; Expires=Sat, 30 Nov 2019 23:44:10 UTC; 30 | Path=/; Domain=.twitter.com'] 31 | status: [200 OK] 32 | strict-transport-security: [max-age=631138519] 33 | x-access-level: [read-write] 34 | x-connection-hash: [710d6144ba7c8732bbfb4dc04ef294cb] 35 | x-content-type-options: [nosniff] 36 | x-frame-options: [SAMEORIGIN] 37 | x-rate-limit-limit: ['75'] 38 | x-rate-limit-remaining: ['74'] 39 | x-rate-limit-reset: ['1512086350'] 40 | x-response-time: ['42'] 41 | x-transaction: [00c9b08e00441908] 42 | x-twitter-response-tags: [BouncerExempt, BouncerCompliant] 43 | x-xss-protection: [1; mode=block] 44 | status: {code: 200, message: OK} 45 | version: 1 46 | -------------------------------------------------------------------------------- /tests/fixtures/user_timeline.yaml: -------------------------------------------------------------------------------- 1 | interactions: 2 | - request: 3 | body: null 4 | headers: 5 | Host: [api.twitter.com] 6 | method: GET 7 | uri: https://api.twitter.com/1.1/statuses/user_timeline.json?exclude_replies=False&include_rts=True&screen_name=botutils&count=1000 8 | response: 9 | body: {string: '[{"created_at":"Thu Nov 30 19:41:04 +0000 2017","id":936319185277341697,"id_str":"936319185277341697","text":"I 10 | am here to help test bot tools","truncated":false,"entities":{"hashtags":[],"symbols":[],"user_mentions":[],"urls":[]},"source":"\u003ca 11 | href=\"http:\/\/twitter.com\" rel=\"nofollow\"\u003eTwitter Web Client\u003c\/a\u003e","in_reply_to_status_id":null,"in_reply_to_status_id_str":null,"in_reply_to_user_id":null,"in_reply_to_user_id_str":null,"in_reply_to_screen_name":null,"user":{"id":936312048459374592,"id_str":"936312048459374592","name":"bot 12 | utils","screen_name":"botutils","location":"","description":"","url":"https:\/\/t.co\/nsX8IijSfN","entities":{"url":{"urls":[{"url":"https:\/\/t.co\/nsX8IijSfN","expanded_url":"http:\/\/github.com\/fitnr\/twitter_bot_utils","display_url":"github.com\/fitnr\/twitter_\u2026","indices":[0,23]}]},"description":{"urls":[]}},"protected":false,"followers_count":0,"friends_count":1,"listed_count":0,"created_at":"Thu 13 | Nov 30 19:12:42 +0000 2017","favourites_count":0,"utc_offset":-28800,"time_zone":"Pacific 14 | Time (US & Canada)","geo_enabled":false,"verified":false,"statuses_count":1,"lang":"en","contributors_enabled":false,"is_translator":false,"is_translation_enabled":false,"profile_background_color":"F5F8FA","profile_background_image_url":null,"profile_background_image_url_https":null,"profile_background_tile":false,"profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/936315057100558336\/o6-BGpHe_normal.jpg","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/936315057100558336\/o6-BGpHe_normal.jpg","profile_link_color":"1DA1F2","profile_sidebar_border_color":"C0DEED","profile_sidebar_fill_color":"DDEEF6","profile_text_color":"333333","profile_use_background_image":true,"has_extended_profile":false,"default_profile":true,"default_profile_image":false,"following":false,"follow_request_sent":false,"notifications":false,"translator_type":"none"},"geo":null,"coordinates":null,"place":null,"contributors":null,"is_quote_status":false,"retweet_count":0,"favorite_count":0,"favorited":false,"retweeted":false,"lang":"en"}]'} 15 | headers: 16 | cache-control: ['no-cache, no-store, must-revalidate, pre-check=0, post-check=0'] 17 | content-disposition: [attachment; filename=json.json] 18 | content-length: ['2113'] 19 | content-type: [application/json;charset=utf-8] 20 | date: ['Thu, 30 Nov 2017 19:41:13 GMT'] 21 | expires: ['Tue, 31 Mar 1981 05:00:00 GMT'] 22 | last-modified: ['Thu, 30 Nov 2017 19:41:13 GMT'] 23 | pragma: [no-cache] 24 | server: [tsa_b] 25 | set-cookie: ['personalization_id="v1_gVIGPZmmNM0cPNCpAIqgCw=="; Expires=Sat, 26 | 30 Nov 2019 19:41:13 UTC; Path=/; Domain=.twitter.com', lang=en; Path=/, 27 | 'guest_id=v1%3A151207087323638946; Expires=Sat, 30 Nov 2019 19:41:13 UTC; 28 | Path=/; Domain=.twitter.com'] 29 | status: [200 OK] 30 | strict-transport-security: [max-age=631138519] 31 | x-access-level: [read-write] 32 | x-connection-hash: [b8e2cfba8f584ca3fb2c37c3a4897427] 33 | x-content-type-options: [nosniff] 34 | x-frame-options: [SAMEORIGIN] 35 | x-rate-limit-limit: ['900'] 36 | x-rate-limit-remaining: ['898'] 37 | x-rate-limit-reset: ['1512071733'] 38 | x-response-time: ['43'] 39 | x-transaction: [00d4e0b30012730b] 40 | x-twitter-response-tags: [BouncerCompliant] 41 | x-xss-protection: [1; mode=block] 42 | status: {code: 200, message: OK} 43 | version: 1 44 | -------------------------------------------------------------------------------- /src/twitter_bot_utils/args.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # Copyright 2014-17 Neil Freeman 3 | # This program is free software: you can redistribute it and/or modify 4 | # it under the terms of the GNU General Public License as published by 5 | # the Free Software Foundation, either version 3 of the License, or 6 | # (at your option) any later version. 7 | 8 | # This program is distributed in the hope that it will be useful, 9 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 10 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 | # GNU General Public License for more details. 12 | 13 | # You should have received a copy of the GNU General Public License 14 | # along with this program. If not, see . 15 | 16 | """Argument helper for cli tool.""" 17 | 18 | import argparse 19 | import logging 20 | import sys 21 | 22 | 23 | def add_default_args(parser, version=None, include=None): 24 | """ 25 | Add default arguments to a parser. These are: 26 | - config: argument for specifying a configuration file. 27 | - user: argument for specifying a user. 28 | - dry-run: option for running without side effects. 29 | - verbose: option for running verbosely. 30 | - quiet: option for running quietly. 31 | - version: option for spitting out version information. 32 | 33 | Args: 34 | version (str): version to return on --version 35 | include (Sequence): default arguments to add to cli. Default: (config, user, dry-run, verbose, quiet) 36 | """ 37 | include = include or ("config", "user", "dry-run", "verbose", "quiet") 38 | 39 | if "config" in include: 40 | parser.add_argument( 41 | "-c", 42 | "--config", 43 | dest="config_file", 44 | metavar="PATH", 45 | default=None, 46 | type=str, 47 | help="bots config file (json or yaml)", 48 | ) 49 | if "user" in include: 50 | parser.add_argument("-u", "--user", dest="screen_name", type=str, help="Twitter screen name") 51 | 52 | if "dry-run" in include: 53 | parser.add_argument("-n", "--dry-run", action="store_true", help="Don't actually do anything") 54 | 55 | if "verbose" in include: 56 | parser.add_argument("-v", "--verbose", action="store_true", help="Run talkatively") 57 | 58 | if "quiet" in include: 59 | parser.add_argument("-q", "--quiet", action="store_true", help="Run quietly") 60 | 61 | if version: 62 | parser.add_argument("-V", "--version", action="version", version="%(prog)s " + version) 63 | 64 | 65 | def parent(version=None, include=None): 66 | """ 67 | Return the default args as a parent parser, optionally adding a version 68 | 69 | Args: 70 | version (str): version to return on --version 71 | include (Sequence): default arguments to add to cli. Default: (config, user, dry-run, verbose, quiet) 72 | """ 73 | parser = argparse.ArgumentParser(add_help=False) 74 | add_default_args(parser, version=version, include=include) 75 | return parser 76 | 77 | 78 | def add_logger(name, level=None, format=None): 79 | """ 80 | Set up a stdout logger. 81 | 82 | Args: 83 | name (str): name of the logger 84 | level: defaults to logging.INFO 85 | format (str): format string for logging output. 86 | defaults to ``%(filename)-11s %(lineno)-3d: %(message)s``. 87 | 88 | Returns: 89 | The logger object. 90 | """ 91 | # pylint: disable=redefined-builtin 92 | format = format or "%(filename)-11s %(lineno)-3d: %(message)s" 93 | log = logging.getLogger(name) 94 | 95 | # Set logging level. 96 | log.setLevel(level or logging.INFO) 97 | 98 | handler = logging.StreamHandler(sys.stdout) 99 | handler.setFormatter(logging.Formatter(format)) 100 | log.addHandler(handler) 101 | 102 | return log 103 | -------------------------------------------------------------------------------- /tests/data/js/tweets/2014-04.js: -------------------------------------------------------------------------------- 1 | Grailbird.data.tweets_2014_04 = 2 | [ 3 | { 4 | "source": "TweetDeck", 5 | "entities": { 6 | "user_mentions": [ 7 | { 8 | "name": "Ingrid Burrington", 9 | "screen_name": "lifewinning", 10 | "indices": [ 11 | 0, 12 | 12 13 | ], 14 | "id_str": "348082699", 15 | "id": 348082699 16 | } 17 | ], 18 | "media": [], 19 | "hashtags": [], 20 | "urls": [] 21 | }, 22 | "in_reply_to_status_id_str": "461491840546795520", 23 | "geo": {}, 24 | "id_str": "461493917046022144", 25 | "in_reply_to_user_id": 348082699, 26 | "text": "@lifewinning dsjflsjglkjdhfgskjdfhg", 27 | "id": 461493917046022144, 28 | "in_reply_to_status_id": 461491840546795520, 29 | "created_at": "2014-04-30 13:14:58 +0000", 30 | "in_reply_to_screen_name": "lifewinning", 31 | "in_reply_to_user_id_str": "348082699", 32 | "user": { 33 | "name": "Neil Freeman", 34 | "screen_name": "fitnr", 35 | "protected": false, 36 | "id_str": "6853512", 37 | "profile_image_url_https": "https://pbs.twimg.com/profile_images/431817496350314496/VGgzYAE7_normal.jpeg", 38 | "id": 6853512, 39 | "verified": false 40 | } 41 | }, 42 | { 43 | "source": "Twitter for iPhone", 44 | "entities": { 45 | "user_mentions": [ 46 | { 47 | "name": "names", 48 | "screen_name": "arbitrarynames", 49 | "indices": [ 50 | 3, 51 | 18 52 | ], 53 | "id_str": "2693547697", 54 | "id": 2693547697 55 | } 56 | ], 57 | "media": [], 58 | "hashtags": [], 59 | "urls": [] 60 | }, 61 | "geo": {}, 62 | "id_str": "494625026159951872", 63 | "text": "RT @arbitrarynames: Regina Aura", 64 | "retweeted_status": { 65 | "source": "Words & Warps", 66 | "entities": { 67 | "user_mentions": [], 68 | "media": [], 69 | "hashtags": [], 70 | "urls": [] 71 | }, 72 | "geo": {}, 73 | "id_str": "494622312277475328", 74 | "text": "Regina Aura", 75 | "id": 494622312277475328, 76 | "created_at": "2014-07-30 23:15:23 +0000", 77 | "user": { 78 | "name": "names", 79 | "screen_name": "arbitrarynames", 80 | "protected": false, 81 | "id_str": "2693547697", 82 | "profile_image_url_https": "https://pbs.twimg.com/profile_images/494560926587052033/OsA2QXSU_normal.jpeg", 83 | "id": 2693547697, 84 | "verified": false 85 | } 86 | } 87 | }, 88 | { 89 | "source": "Cloudhopper", 90 | "entities": { 91 | "user_mentions": [], 92 | "media": [], 93 | "hashtags": [ 94 | { 95 | "text": "ééé", 96 | "indices": [ 97 | 0, 98 | 5 99 | ] 100 | }, 101 | { 102 | "text": "buttons", 103 | "indices": [ 104 | 6, 105 | 14 106 | ] 107 | } 108 | ], 109 | "urls": [] 110 | }, 111 | "geo": {}, 112 | "id_str": "6132827606", 113 | "text": "#ééé #buttons", 114 | "id": 6132827606, 115 | "created_at": "2009-11-28 00:00:00 +0000", 116 | "user": { 117 | "name": "Neil Freeman", 118 | "screen_name": "fitnr", 119 | "protected": false, 120 | "id_str": "6853512", 121 | "profile_image_url_https": "https://pbs.twimg.com/profile_images/431817496350314496/VGgzYAE7_normal.jpeg", 122 | "id": 6853512, 123 | "verified": false 124 | } 125 | } 126 | ] -------------------------------------------------------------------------------- /src/twitter_bot_utils/tools.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # Copyright 2014-17 Neil Freeman contact@fakeisthenewreal.org 3 | # This program is free software: you can redistribute it and/or modify 4 | # it under the terms of the GNU General Public License as published by 5 | # the Free Software Foundation, either version 3 of the License, or 6 | # (at your option) any later version. 7 | 8 | # This program is distributed in the hope that it will be useful, 9 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 10 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 | # GNU General Public License for more details. 12 | 13 | # You should have received a copy of the GNU General Public License 14 | # along with this program. If not, see . 15 | 16 | """A bit of a grab-bag, honestly.""" 17 | 18 | from time import sleep 19 | 20 | from tweepy.errors import TooManyRequests as RateLimitError, TweepyException as TweepError 21 | 22 | RATE_LIMIT_RESET_MINUTES = 15 23 | 24 | 25 | def follow_back(api, dry_run=None): 26 | """Follow back using the given API""" 27 | _autofollow(api, "follow", dry_run) 28 | 29 | 30 | def unfollow(api, dry_run=None): 31 | """Unlfollow-back back using the given API""" 32 | _autofollow(api, "unfollow", dry_run) 33 | 34 | 35 | def _autofollow(api, action, dry_run): 36 | """ 37 | Follow back or unfollow the friends/followers of user authenicated in 'api'. 38 | :api twitter_bot_utils.api.API 39 | :dry_run bool don't actually (un)follow, just report 40 | """ 41 | try: 42 | # get the last 5000 followers 43 | followers = api.get_follower_ids() 44 | 45 | # Get the last 5000 people user has followed 46 | friends = api.get_friend_ids() 47 | 48 | except TweepError as err: 49 | api.logger.error("%s: error getting followers/followers", action) 50 | api.logger.error("%s", err) 51 | return 52 | 53 | if action == "unfollow": 54 | method = api.destroy_friendship 55 | independent, dependent = followers, friends 56 | 57 | elif action == "follow": 58 | method = api.create_friendship 59 | independent, dependent = friends, followers 60 | else: 61 | raise IndexError("Unknown action: {}".format(action)) 62 | 63 | api.logger.info("%sing: found %s friends, %s followers", action, len(friends), len(followers)) 64 | 65 | # auto-following: 66 | # for all my followers 67 | # if i don't already follow them: create friendship 68 | 69 | # auto-unfollowing: 70 | # for all my friends 71 | # if they don't follow me: destroy friendship 72 | targets = [x for x in dependent if x not in independent] 73 | 74 | for uid in targets: 75 | try: 76 | api.logger.info("%sing %s", action, uid) 77 | 78 | if not dry_run: 79 | method(id=uid) 80 | 81 | except RateLimitError: 82 | api.logger.warning( 83 | "reached Twitter's rate limit, sleeping for %d minutes", 84 | RATE_LIMIT_RESET_MINUTES, 85 | ) 86 | sleep(RATE_LIMIT_RESET_MINUTES * 60) 87 | method(id=uid) 88 | 89 | except TweepError as err: 90 | api.logger.error("error %sing on %s", action, uid) 91 | api.logger.error("code %s: %s", err.api_code, err) 92 | 93 | 94 | def fave_mentions(api, dry_run=None): 95 | """ 96 | Fave (aka like) recent mentions from user authenicated in 'api'. 97 | :api twitter_bot_utils.api.API 98 | :dry_run bool don't actually favorite, just report 99 | """ 100 | f = api.get_favorites(include_entities=False, count=150) 101 | favs = [m.id_str for m in f] 102 | 103 | mentions = api.mentions_timeline(trim_user=True, include_entities=False, count=75) 104 | 105 | for mention in mentions: 106 | # only try to fav if not in recent favs 107 | if mention.id_str not in favs: 108 | try: 109 | api.logger.info("liking %s: %s", mention.id_str, mention.text) 110 | 111 | if not dry_run: 112 | api.create_favorite(mention.id_str, include_entities=False) 113 | 114 | except RateLimitError: 115 | api.logger.warning( 116 | "reached Twitter's rate limit, sleeping for %d minutes", 117 | RATE_LIMIT_RESET_MINUTES, 118 | ) 119 | sleep(RATE_LIMIT_RESET_MINUTES * 60) 120 | api.create_favorite(mention.id_str, include_entities=False) 121 | 122 | except TweepError as err: 123 | api.logger.error("error liking %s", mention.id_str) 124 | api.logger.error("code %s: %s", err.api_code, err) 125 | -------------------------------------------------------------------------------- /src/twitter_bot_utils/confighelper.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # Copyright 2014-17 Neil Freeman contact@fakeisthenewreal.org 3 | # This program is free software: you can redistribute it and/or modify 4 | # it under the terms of the GNU General Public License as published by 5 | # the Free Software Foundation, either version 3 of the License, or 6 | # (at your option) any later version. 7 | 8 | # This program is distributed in the hope that it will be useful, 9 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 10 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 | # GNU General Public License for more details. 12 | 13 | # You should have received a copy of the GNU General Public License 14 | # along with this program. If not, see . 15 | 16 | """Tools for managing bots.yaml and other config files.""" 17 | 18 | import json 19 | from itertools import product 20 | from os import getcwd, path 21 | 22 | import tweepy 23 | import yaml 24 | 25 | CONFIG_DIRS = [ 26 | getcwd(), 27 | "~", 28 | path.join("~", "bots"), 29 | ] 30 | 31 | CONFIG_BASES = ["bots.yml", "bots.yaml", "bots.json"] 32 | 33 | 34 | def configure(screen_name=None, config_file=None, app=None, **kwargs): 35 | """ 36 | Set up a config dictionary using a bots.yaml config file and optional keyword args. 37 | 38 | Args: 39 | screen_name (str): screen_name of user to search for in config file 40 | config_file (str): Path to read for the config file 41 | app (str): Name of the app to look for in the config file. Defaults to the one set in users.{screen_name}. 42 | directories (str): Directories to read for the bots.yaml/json file. Defaults to ``CONFIG_DIRS``. 43 | bases (str): File names to look for in the directories. Defaults to ``CONFIG_BASES``. 44 | """ 45 | # Use passed config file, or look for it in the default path. 46 | # Super-optionally, accept a different place to look for the file 47 | dirs = kwargs.pop("directories", None) 48 | bases = kwargs.pop("bases", None) 49 | file_config = {} 50 | if config_file is not False: 51 | config_file = find_file(config_file, dirs, bases) 52 | file_config = parse(config_file) 53 | 54 | # config and keys dicts 55 | # Pull non-authentication settings from the file. 56 | # Kwargs, user, app, and general settings are included, in that order of preference 57 | # Exclude apps and users sections from config 58 | config = {k: v for k, v in file_config.items() if k not in ("apps", "users")} 59 | 60 | user_conf = file_config.get("users", {}).get(screen_name, {}) 61 | app = app or user_conf.get("app") 62 | app_conf = file_config.get("apps", {}).get(app, {}) 63 | 64 | # Pull user and app data from the file 65 | config.update(app_conf) 66 | config.update(user_conf) 67 | 68 | # kwargs take precendence over config file 69 | config.update({k: v for k, v in kwargs.items() if v is not None}) 70 | 71 | return config 72 | 73 | 74 | def parse(file_path): 75 | """Parse a YAML file.""" 76 | _, ext = path.splitext(file_path) 77 | 78 | if ext not in (".yaml", ".yml"): 79 | raise ValueError(f"Unrecognized config file type: {ext}") 80 | 81 | with open(file_path, "r", encoding="utf-8") as f: 82 | return yaml.safe_load(f) 83 | 84 | 85 | def find_file(config_file=None, default_directories=None, default_bases=None): 86 | """Search for a config file in a list of files.""" 87 | if config_file: 88 | if path.exists(path.expanduser(config_file)): 89 | return config_file 90 | 91 | raise FileNotFoundError(f"Config file not found: {config_file}") 92 | 93 | dirs = default_directories or CONFIG_DIRS 94 | dirs = [getcwd()] + dirs 95 | 96 | bases = default_bases or CONFIG_BASES 97 | 98 | for directory, base in product(dirs, bases): 99 | filepath = path.expanduser(path.join(directory, base)) 100 | if path.exists(filepath): 101 | return filepath 102 | 103 | raise FileNotFoundError(f"Config file not found in {dirs}") 104 | 105 | 106 | def setup_auth(**keys): 107 | """Set up Tweepy authentication using passed args or config file settings.""" 108 | return tweepy.OAuth1UserHandler( 109 | consumer_key=keys["consumer_key"], 110 | consumer_secret=keys["consumer_secret"], 111 | access_token=keys.get("token", keys.get("key", keys.get("oauth_token"))), 112 | access_token_secret=keys.get("secret", keys.get("oauth_secret")), 113 | ) 114 | 115 | 116 | def dump(contents, file_path): 117 | """Dump a file's contents as a Python object""" 118 | _, ext = path.splitext(file_path) 119 | 120 | if ext not in (".yaml", ".yml"): 121 | raise ValueError(f"Unrecognized config file type {ext}") 122 | 123 | with open(file_path, "w", encoding="utf-8") as f: 124 | yaml.dump(contents, f, canonical=False, default_flow_style=False, indent=2) 125 | -------------------------------------------------------------------------------- /HISTORY.rst: -------------------------------------------------------------------------------- 1 | 0.14 2 | ---- 3 | * Bump required tweepy to 4.6 4 | * Remove `api.update_status` method with retry 5 | * Remove `use_env` option in API. 6 | * Remove support for bots.json files 7 | 8 | 0.13 9 | ---- 10 | * Add `tbu retweet` method to command-line tool 11 | * Boring updates to packaging method and testing 12 | * Remove media upload methods now available in tweepy 13 | 14 | 0.12.1 15 | ------ 16 | * Remove remaining Python 2 methods and support (#6) 17 | * Move tests to tox 18 | 19 | 0.12.0 20 | ------ 21 | * Remove support for Python 2 22 | 23 | 0.11.6.post1 24 | ------ 25 | * Fix bad import in helpers 26 | 27 | 0.11.6 28 | ------ 29 | * Add `api.media_upload` method as workaround for old/missing functions in tweepy 30 | * Add `helpers.length` method to get length of text as understood by Twitter 31 | * Change default length on `helpers.chomp` to 280 32 | * Remove explicit support for Tweepy 3.4.0 33 | 34 | 0.11.5 35 | ------ 36 | * Fix a bug that would prevent proper return of a Status object with API.update_status in some cases. 37 | * Expand tests to include latest with Tweepy from Github. 38 | 39 | 0.11.4 40 | ------ 41 | * Add ability to read auth keys from environment variables: TWITTER_CONSUMER_KEY, TWITTER_CONSUMER_SECRET, TWITTER_KEY, TWITTER_SECRET 42 | 43 | 0.11.2 44 | ------ 45 | * Fixes and expands `tbu auth` interface (issues #4 and #5) 46 | * Allow install for a greater range of twitter_bot_utils versions 47 | 48 | 0.11.1 49 | ------ 50 | 51 | * Remove (no)header option. 52 | 53 | 0.11.0 54 | ------ 55 | 56 | * Consolidated command line tools to subcommands of `tbu`. Added deprecation warnings to earlier versions of commands. 57 | * Add (no)header option to archive reading. 58 | 59 | 0.10.5 60 | ------ 61 | 62 | * Fix encoding bug when reading non-ASCII text from archives. 63 | * Add docs 64 | 65 | 0.10.4 66 | ------ 67 | * Add `include` argument to `args.parent` 68 | * Bump required `tweepy` to take advantage of `TweepError.api_code` 69 | * In Py 3, don't read config files as strings, not bytes 70 | * Check rate limits in follow/fave tools 71 | 72 | 0.10.3 73 | ------ 74 | * Simplify config section 75 | * Add twitter-auth command line tool 76 | 77 | 0.10.post1 78 | ------ 79 | * Fix missing handler on `API.logger`. 80 | * Change internal api for logger, now accepts a `logging` level. 81 | 82 | 0.10.0 83 | ------ 84 | * Add `helpers.chomp` method for progressively shortening strings. 85 | * Remove app, secret, consumer-key and consumer-secret command line args. A bots.yaml config file now mandatory. 86 | * No longer urlencode when queryizing 87 | * Logging: Remove file logger, add silent option, start logger with args, add logger in api 88 | * Rename helper utils to 'auto-follow' and 'fave-mentions' 89 | 90 | 0.9.7 91 | ----- 92 | * Fix py3 error on reading archives 93 | 94 | 0.9.6 95 | ----- 96 | * Look farther back when picking recent tweets 97 | 98 | 0.9.5.1 99 | ------- 100 | 101 | * Fix a little bug in the `archive.read_csv` api. Now accepts directories or paths. 102 | 103 | 0.9.5 104 | ----- 105 | 106 | * Add abilty to read Twitter csv archive files with `archive.read_csv` 107 | 108 | 0.9.1.post1 109 | ----------- 110 | 111 | * OK, we don't want universal builds, whoops. 112 | 113 | 0.9.1 114 | ----- 115 | 116 | * Fixed Windows bug (thanks hugovk) 117 | * Added mock to tests, using Travis for CI 118 | * Smoother Python 2/3 integration 119 | 120 | 0.9 121 | ----- 122 | 123 | * Setup easier for tasks that don't require Twitter authentication 124 | * Big update to follow/fave CLI 125 | * Added tox and various tests 126 | * Refactored args module 127 | 128 | 0.8.1 129 | ----- 130 | 131 | * Overhaul command line follow/favorite utilities 132 | * Fix imports in Py3 133 | * Expand tests 134 | 135 | 0.8 136 | ----- 137 | * No longer accept argpase.Namespace in api.API. use keyword args instead. 138 | 139 | 0.7 140 | ----- 141 | 142 | * Change api for creating parsers. `creation` module is gone, use tbu.args.parent() to pass a parent to `argparse.ArgumentParser`. 143 | 144 | 0.6.6 145 | ----- 146 | 147 | * Grab longer user timeline for establishing recent replies, retweets 148 | 149 | 0.6.5 150 | ----- 151 | 152 | * Fix bugs in queryize, recent tweets in API 153 | * use logger named screen_name in follow tools 154 | 155 | 0.6.4 156 | ----- 157 | 158 | * Add helpers.queryize - formats a list of terms for a Twitter search. 159 | * Automatically use ellipsis character ('…') in helpers.shorten when `ellipsis=True`. 160 | 161 | 162 | 0.6.3 163 | ----- 164 | 165 | * Add helpers.shorten - cuts a string down to 140 characters without breaking words. 166 | 167 | 0.6.2 168 | ----- 169 | 170 | Add 'archive' module for reading Twitter archives or simple text files. 171 | 172 | 173 | 0.6.1 174 | ----- 175 | 176 | Typos 177 | 178 | 0.6 179 | --- 180 | 181 | * Add confighelper module, with tools for parsing simple config files 182 | * Fix Python 3 compatibility 183 | 184 | 0.5.2 185 | ----- 186 | 187 | Changes: 188 | 189 | * Add helpers.replace_urls method 190 | 191 | 0.5 192 | --- 193 | 194 | Changes: 195 | 196 | * Release into the wild 197 | * Simplify config getting and setting when creating api.API 198 | * Import with api.API, instead of API living in __all__ 199 | * Simplify error-throwing 200 | * Find handling of bad configs 201 | * Update docs 202 | 203 | 0.4 204 | --- 205 | 206 | Changes: 207 | 208 | * Add test cases 209 | * Move tools to tools.py 210 | * Add test formatting 211 | * Update docs 212 | * Add entity filters 213 | -------------------------------------------------------------------------------- /src/twitter_bot_utils/api.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # Copyright 2014-2020 Neil Freeman 3 | # This program is free software: you can redistribute it and/or modify 4 | # it under the terms of the GNU General Public License as published by 5 | # the Free Software Foundation, either version 3 of the License, or 6 | # (at your option) any later version. 7 | # This program is distributed in the hope that it will be useful, 8 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | # GNU General Public License for more details. 11 | # You should have received a copy of the GNU General Public License 12 | # along with this program. If not, see . 13 | 14 | """ Wrapper around the tweepy.API """ 15 | 16 | import logging 17 | import os 18 | from argparse import Namespace 19 | 20 | import tweepy 21 | 22 | from . import args as tbu_args 23 | from .confighelper import configure 24 | 25 | PROTECTED_INFO = [ 26 | "consumer_key", 27 | "consumer_secret", 28 | "key", 29 | "token", 30 | "oauth_token", 31 | "secret", 32 | "oauth_secret", 33 | ] 34 | 35 | 36 | class API(tweepy.API): 37 | 38 | """ 39 | Extends the tweepy API with config-file handling. 40 | 41 | Args: 42 | args (Namespace): argparse.Namespace to read. 43 | screen_name (str): Twitter screen name 44 | config_file (str): Config file. When False, don't read any config files. 45 | Defaults to bots.json or bots.yaml in ~/ or ~/bots/. 46 | logger_name (str): Use a logger with this name. Defaults to screen_name 47 | format (str): Format for logger. Defaults to 'file lineno: message' 48 | verbose (bool): Set logging level to DEBUG 49 | quiet (bool): Set logging level to ERROR. Overrides verbose. 50 | kwargs: Other settings will be passed to the config 51 | """ 52 | 53 | def __init__(self, args=None, **kwargs): 54 | """ 55 | Construct the tbu.API object. 56 | """ 57 | # Update the kwargs with non-None contents of args 58 | if isinstance(args, Namespace): 59 | kwargs.update({k: v for k, v in vars(args).items() if v is not None}) 60 | 61 | self._screen_name = kwargs.pop("screen_name", None) 62 | 63 | # Add a logger 64 | level = logging.DEBUG if kwargs.pop("verbose", None) else None 65 | level = logging.ERROR if kwargs.get("quiet", None) else level 66 | self.logger = tbu_args.add_logger( 67 | kwargs.pop("logger_name", self._screen_name), 68 | level, 69 | kwargs.pop("format", None), 70 | ) 71 | 72 | # get config file and parse it 73 | config = configure(self._screen_name, **kwargs) 74 | self._config = {k: v for k, v in config.items() if k not in PROTECTED_INFO} 75 | keys = {k: v for k, v in config.items() if k in PROTECTED_INFO} 76 | 77 | try: 78 | # set up auth 79 | auth = tweepy.OAuth1UserHandler( 80 | consumer_key=keys["consumer_key"], 81 | consumer_secret=keys["consumer_secret"], 82 | access_token=keys.get("token", keys.get("key", keys.get("oauth_token"))), 83 | access_token_secret=keys.get("secret", keys.get("oauth_secret")), 84 | ) 85 | except KeyError as err: 86 | missing = [p for p in PROTECTED_INFO if p not in keys] 87 | raise ValueError(f"Incomplete config. Missing {missing}") from err 88 | 89 | self._last_tweet = None 90 | self._last_reply = None 91 | self._last_retweet = None 92 | 93 | # initiate api connection 94 | super().__init__(auth) 95 | 96 | @property 97 | def config(self): 98 | """Return config dict.""" 99 | return self._config 100 | 101 | @property 102 | def screen_name(self): 103 | """Return authorized @screen_name""" 104 | return self._screen_name 105 | 106 | @property 107 | def app(self): 108 | """Return current app name.""" 109 | return self._config["app"] 110 | 111 | def _sinces(self): 112 | timeline = self.user_timeline(screen_name=self.screen_name, count=1000, include_rts=True, exclude_replies=False) 113 | 114 | if timeline: 115 | self._last_tweet = timeline[0].id 116 | else: 117 | self._last_tweet = self._last_reply = self._last_retweet = None 118 | return 119 | 120 | try: 121 | self._last_reply = max(t.id for t in timeline if t.in_reply_to_user_id) 122 | except ValueError: 123 | self._last_reply = None 124 | 125 | try: 126 | self._last_retweet = max(t.id for t in timeline if t.retweeted) 127 | except ValueError: 128 | self._last_retweet = None 129 | 130 | def _last(self, last_what, refresh=None): 131 | if refresh or getattr(self, last_what) is None: 132 | self._sinces() 133 | 134 | return getattr(self, last_what) 135 | 136 | @property 137 | def last_tweet(self): 138 | """Return most recent sent tweet.""" 139 | return self._last("_last_tweet") 140 | 141 | @property 142 | def last_reply(self): 143 | """Return most recent sent reply.""" 144 | return self._last("_last_reply") 145 | 146 | @property 147 | def last_retweet(self): 148 | """Return most recent retweet.""" 149 | return self._last("_last_retweet") 150 | -------------------------------------------------------------------------------- /tests/test_helpers.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | from __future__ import unicode_literals 3 | 4 | import unittest 5 | 6 | import tweepy 7 | 8 | from twitter_bot_utils import helpers 9 | 10 | from .config import example_tweet 11 | 12 | 13 | class test_tbu_helpers(unittest.TestCase): 14 | # pylint: disable=invalid-name 15 | def setUp(self): 16 | self.api = tweepy.API() 17 | self.tweet = example_tweet 18 | self.status = tweepy.models.Status.parse(self.api, self.tweet) 19 | 20 | def test_has_entities(self): 21 | assert helpers.has_entities(self.status) is True 22 | assert helpers.has_entities(self.tweet) is True 23 | 24 | assert helpers.has_media(self.tweet) is False 25 | assert helpers.has_media(self.status) is False 26 | 27 | assert helpers.has_symbol(self.status) is False 28 | assert helpers.has_symbol(self.tweet) is False 29 | 30 | def test_has_hashtag(self): 31 | assert helpers.has_hashtag(self.status) is False 32 | assert helpers.has_hashtag(self.tweet) is False 33 | 34 | def test_has_mention(self): 35 | assert helpers.has_mention(self.status) 36 | assert helpers.has_mention(self.tweet) 37 | 38 | def test_remove_entities(self): 39 | assert helpers.remove_entity(self.status, 'hashtags') == self.status.text 40 | assert helpers.remove_entity(self.status, 'user_mentions') == " example tweet example tweet example tweet" 41 | assert ( 42 | helpers.remove_entities(self.status, ['hashtags', 'user_mentions']) 43 | == " example tweet example tweet example tweet" 44 | ) 45 | assert ( 46 | helpers.remove_entities(self.tweet, ['hashtags', 'user_mentions']) 47 | == " example tweet example tweet example tweet" 48 | ) 49 | 50 | def test_replace_urls(self): 51 | assert helpers.replace_urls(self.status) == self.tweet['text'] 52 | 53 | self.tweet['entities']['urls'] = [{"indices": [0, 12], "expanded_url": "http://long.long.url"}] 54 | self.tweet['text'] = 'http://short hey' 55 | 56 | status = tweepy.models.Status.parse(self.api, self.tweet) 57 | 58 | assert helpers.replace_urls(status) == "http://long.long.url hey" 59 | 60 | def test_shorten(self): 61 | hello = ( 62 | "This is a long string that's longer than 140 characters, " 63 | "yes it's quite long. It's so long that we need to shorten it. " 64 | "Supercalifragilisticexpialidocious! Amazing. I want to test this." 65 | ) 66 | 67 | for i in range(10, 180, 10): 68 | self.assertLessEqual(len(helpers.shorten(hello, i)), i) 69 | 70 | assert helpers.shorten(hello, ellipsis=True)[-1:] == '…' 71 | assert helpers.shorten(hello, ellipsis='...')[-3:] == '...' 72 | 73 | assert helpers.shorten('hello') == 'hello' 74 | assert helpers.shorten(hello, 500) == hello 75 | 76 | assert helpers.shorten(hello, ellipsis=True) == ( 77 | "This is a long string that's longer than 140 characters, " 78 | "yes it's quite long. It's so long that we need to shorten it…" 79 | ) 80 | 81 | def test_querize(self): 82 | query = ('hi', 'bye', 'oh wow', '-no', '-nuh uh') 83 | self.assertEqual(helpers.queryize(query).strip(), '"hi" OR "bye" OR "oh wow" -"no" -"nuh uh"') 84 | self.assertEqual(helpers.queryize(query, 'user'), '"hi" OR "bye" OR "oh wow" -"no" -"nuh uh" -from:user') 85 | 86 | def testLength(self): 87 | # equal len and length 88 | strings = [ 89 | 'happy 123', # ascii 90 | u'ქართული ენა', # Georgian 91 | u'˗˖˭ʰ', # Spacing modifiers 92 | u'āz̪u̾ìì', # diacretics 93 | ] 94 | for s in strings: 95 | self.assertEqual(len(s), helpers.length(s)) 96 | 97 | # compare non-normalized with normalized forms 98 | self.assertEqual(helpers.length('café'), helpers.length('café')) 99 | 100 | # characters that count as "2" 101 | strings = [ 102 | u'ᏣᎳᎩᎦᏬᏂᎯᏍᏗ', # Cherokee language 103 | # Phonetic extensions 104 | ''.join(chr(x) for x in range(int('1D00', 16), int('1DBF', 16))), 105 | # generally a bunch of higher-level unicode 106 | ''.join(chr(x) for x in range(int('1100', 16), int('2F96C', 16), 200)), 107 | ] 108 | for s in strings: 109 | self.assertEqual(len(s) * 2, helpers.length(s)) 110 | 111 | s = ''.join(x + y for x, y in zip(u'ᏣᎳᎩᎦᏬᏂᎯᏍᏗ', u'abcdefghi')) 112 | self.assertEqual(int(len(s) * 1.5), helpers.length(s)) 113 | 114 | def testChomp(self): 115 | long_string = ( 116 | "It was the best of times, it was the worst of times, " 117 | "it was the age of wisdom, it was the age of foolishness, " 118 | "it was the epoch of belief, it was the epoch of incredulity, " 119 | "it was the season of Light, it was the season of Darkness, " 120 | "it was the spring of hope, it was the winter of despair, " 121 | "we had everything before us, we had nothing before us, " 122 | "we were all going direct to Heaven, we were all going direct the other way— in short, " 123 | "the period was so far like the present period, " 124 | "that some of its noisiest authorities insisted on its being received, " 125 | "for good or for evil, in the superlative degree of comparison only." 126 | ) 127 | 128 | chomp_140 = ( 129 | "It was the best of times, it was the worst of times, it was the age of wisdom, " 130 | "it was the age of foolishness, it was the epoch of belief" 131 | ) 132 | 133 | self.assertEqual(helpers.chomp(long_string, max_len=140), chomp_140) 134 | 135 | self.assertEqual(helpers.chomp(long_string, max_len=30), "It was the best of times") 136 | 137 | chomp_140_comma = ( 138 | "It was the best of times, it was the worst of times, it was the age of wisdom, " 139 | "it was the age of foolishness, it was the epoch of belief, it" 140 | ) 141 | 142 | self.assertEqual(helpers.chomp(long_string, split=' ,', max_len=140), chomp_140_comma) 143 | 144 | self.assertEqual(helpers.chomp(long_string, split='9'), long_string) 145 | 146 | 147 | if __name__ == '__main__': 148 | unittest.main() 149 | -------------------------------------------------------------------------------- /tests/fixtures/status.yaml: -------------------------------------------------------------------------------- 1 | interactions: 2 | - request: 3 | body: null 4 | headers: 5 | Content-Length: ['0'] 6 | Host: [api.twitter.com] 7 | method: POST 8 | uri: https://api.twitter.com/1.1/statuses/update.json?status=Just+running+some+tests... 9 | response: 10 | body: {string: '{"created_at":"Thu Nov 30 19:45:47 +0000 2017","id":936320374022856704,"id_str":"936320374022856704","text":"Just 11 | running some tests...","truncated":false,"entities":{"hashtags":[],"symbols":[],"user_mentions":[],"urls":[]},"source":"\u003ca 12 | href=\"http:\/\/fakeisthenewreal.org\/strangeallure\" rel=\"nofollow\"\u003estrange 13 | allure\u003c\/a\u003e","in_reply_to_status_id":null,"in_reply_to_status_id_str":null,"in_reply_to_user_id":null,"in_reply_to_user_id_str":null,"in_reply_to_screen_name":null,"user":{"id":936312048459374592,"id_str":"936312048459374592","name":"bot 14 | utils","screen_name":"botutils","location":"","description":"","url":"https:\/\/t.co\/nsX8IijSfN","entities":{"url":{"urls":[{"url":"https:\/\/t.co\/nsX8IijSfN","expanded_url":"http:\/\/github.com\/fitnr\/twitter_bot_utils","display_url":"github.com\/fitnr\/twitter_\u2026","indices":[0,23]}]},"description":{"urls":[]}},"protected":false,"followers_count":0,"friends_count":1,"listed_count":0,"created_at":"Thu 15 | Nov 30 19:12:42 +0000 2017","favourites_count":0,"utc_offset":-28800,"time_zone":"Pacific 16 | Time (US & Canada)","geo_enabled":false,"verified":false,"statuses_count":2,"lang":"en","contributors_enabled":false,"is_translator":false,"is_translation_enabled":false,"profile_background_color":"F5F8FA","profile_background_image_url":null,"profile_background_image_url_https":null,"profile_background_tile":false,"profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/936315057100558336\/o6-BGpHe_normal.jpg","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/936315057100558336\/o6-BGpHe_normal.jpg","profile_link_color":"1DA1F2","profile_sidebar_border_color":"C0DEED","profile_sidebar_fill_color":"DDEEF6","profile_text_color":"333333","profile_use_background_image":true,"has_extended_profile":false,"default_profile":true,"default_profile_image":false,"following":false,"follow_request_sent":false,"notifications":false,"translator_type":"none"},"geo":null,"coordinates":null,"place":null,"contributors":null,"is_quote_status":false,"retweet_count":0,"favorite_count":0,"favorited":false,"retweeted":false,"lang":"en"}'} 17 | headers: 18 | cache-control: ['no-cache, no-store, must-revalidate, pre-check=0, post-check=0'] 19 | content-disposition: [attachment; filename=json.json] 20 | content-length: ['2125'] 21 | content-type: [application/json;charset=utf-8] 22 | date: ['Thu, 30 Nov 2017 19:45:47 GMT'] 23 | expires: ['Tue, 31 Mar 1981 05:00:00 GMT'] 24 | last-modified: ['Thu, 30 Nov 2017 19:45:47 GMT'] 25 | pragma: [no-cache] 26 | server: [tsa_b] 27 | set-cookie: ['personalization_id="v1_1548yRkcyqHLnrZ2lsxVRQ=="; Expires=Sat, 28 | 30 Nov 2019 19:45:47 UTC; Path=/; Domain=.twitter.com', lang=en; Path=/, 29 | 'guest_id=v1%3A151207114779371796; Expires=Sat, 30 Nov 2019 19:45:47 UTC; 30 | Path=/; Domain=.twitter.com'] 31 | status: [200 OK] 32 | strict-transport-security: [max-age=631138519] 33 | x-access-level: [read-write] 34 | x-connection-hash: [d5cb5dfd34e30b909921aae04b1d9946] 35 | x-content-type-options: [nosniff] 36 | x-frame-options: [SAMEORIGIN] 37 | x-response-time: ['202'] 38 | x-transaction: [006e4fab0069b1cb] 39 | x-twitter-response-tags: [BouncerCompliant] 40 | x-xss-protection: [1; mode=block] 41 | status: {code: 200, message: OK} 42 | - request: 43 | body: null 44 | headers: 45 | Content-Length: ['0'] 46 | Host: [api.twitter.com] 47 | method: POST 48 | uri: https://api.twitter.com/1.1/statuses/update.json?status=Just+running+some+more+tests... 49 | response: 50 | body: {string: '{"created_at":"Thu Nov 30 19:45:48 +0000 2017","id":936320375775989762,"id_str":"936320375775989762","text":"Just 51 | running some more tests...","truncated":false,"entities":{"hashtags":[],"symbols":[],"user_mentions":[],"urls":[]},"source":"\u003ca 52 | href=\"http:\/\/fakeisthenewreal.org\/strangeallure\" rel=\"nofollow\"\u003estrange 53 | allure\u003c\/a\u003e","in_reply_to_status_id":null,"in_reply_to_status_id_str":null,"in_reply_to_user_id":null,"in_reply_to_user_id_str":null,"in_reply_to_screen_name":null,"user":{"id":936312048459374592,"id_str":"936312048459374592","name":"bot 54 | utils","screen_name":"botutils","location":"","description":"","url":"https:\/\/t.co\/nsX8IijSfN","entities":{"url":{"urls":[{"url":"https:\/\/t.co\/nsX8IijSfN","expanded_url":"http:\/\/github.com\/fitnr\/twitter_bot_utils","display_url":"github.com\/fitnr\/twitter_\u2026","indices":[0,23]}]},"description":{"urls":[]}},"protected":false,"followers_count":0,"friends_count":1,"listed_count":0,"created_at":"Thu 55 | Nov 30 19:12:42 +0000 2017","favourites_count":0,"utc_offset":-28800,"time_zone":"Pacific 56 | Time (US & Canada)","geo_enabled":false,"verified":false,"statuses_count":3,"lang":"en","contributors_enabled":false,"is_translator":false,"is_translation_enabled":false,"profile_background_color":"F5F8FA","profile_background_image_url":null,"profile_background_image_url_https":null,"profile_background_tile":false,"profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/936315057100558336\/o6-BGpHe_normal.jpg","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/936315057100558336\/o6-BGpHe_normal.jpg","profile_link_color":"1DA1F2","profile_sidebar_border_color":"C0DEED","profile_sidebar_fill_color":"DDEEF6","profile_text_color":"333333","profile_use_background_image":true,"has_extended_profile":false,"default_profile":true,"default_profile_image":false,"following":false,"follow_request_sent":false,"notifications":false,"translator_type":"none"},"geo":null,"coordinates":null,"place":null,"contributors":null,"is_quote_status":false,"retweet_count":0,"favorite_count":0,"favorited":false,"retweeted":false,"lang":"en"}'} 57 | headers: 58 | cache-control: ['no-cache, no-store, must-revalidate, pre-check=0, post-check=0'] 59 | content-disposition: [attachment; filename=json.json] 60 | content-length: ['2130'] 61 | content-type: [application/json;charset=utf-8] 62 | date: ['Thu, 30 Nov 2017 19:45:48 GMT'] 63 | expires: ['Tue, 31 Mar 1981 05:00:00 GMT'] 64 | last-modified: ['Thu, 30 Nov 2017 19:45:48 GMT'] 65 | pragma: [no-cache] 66 | server: [tsa_b] 67 | set-cookie: ['personalization_id="v1_VskhtuxY5Hz33ZqD2SUx2Q=="; Expires=Sat, 68 | 30 Nov 2019 19:45:48 UTC; Path=/; Domain=.twitter.com', lang=en; Path=/, 69 | 'guest_id=v1%3A151207114821891231; Expires=Sat, 30 Nov 2019 19:45:48 UTC; 70 | Path=/; Domain=.twitter.com'] 71 | status: [200 OK] 72 | strict-transport-security: [max-age=631138519] 73 | x-access-level: [read-write] 74 | x-connection-hash: [81c0c5e226af76afddc71aab3ef87d3c] 75 | x-content-type-options: [nosniff] 76 | x-frame-options: [SAMEORIGIN] 77 | x-response-time: ['145'] 78 | x-transaction: [004445b5006b5eb9] 79 | x-twitter-response-tags: [BouncerCompliant] 80 | x-xss-protection: [1; mode=block] 81 | status: {code: 200, message: OK} 82 | version: 1 83 | -------------------------------------------------------------------------------- /src/twitter_bot_utils/helpers.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # Copyright 2014-17 Neil Freeman contact@fakeisthenewreal.org 3 | # This program is free software: you can redistribute it and/or modify 4 | # it under the terms of the GNU General Public License as published by 5 | # the Free Software Foundation, either version 3 of the License, or 6 | # (at your option) any later version. 7 | 8 | # This program is distributed in the hope that it will be useful, 9 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 10 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 | # GNU General Public License for more details. 12 | 13 | # You should have received a copy of the GNU General Public License 14 | # along with this program. If not, see . 15 | 16 | """Helpers for working with tweets""" 17 | 18 | import re 19 | import unicodedata 20 | from html import parser 21 | 22 | 23 | def has_url(status): 24 | """Return true if a tweet has an url entity.""" 25 | return has_entity(status, "urls") 26 | 27 | 28 | def has_hashtag(status): 29 | """Return true if a tweet has a hashtag entity.""" 30 | return has_entity(status, "hashtags") 31 | 32 | 33 | def has_mention(status): 34 | """Return true if a tweet has a mention entity.""" 35 | return has_entity(status, "user_mentions") 36 | 37 | 38 | def has_media(status): 39 | """Return true if a tweet has a media entity.""" 40 | return has_entity(status, "media") 41 | 42 | 43 | def has_symbol(status): 44 | """Return true if a tweet has a symbol entity.""" 45 | return has_entity(status, "symbols") 46 | 47 | 48 | def has_entity(status, entitykey): 49 | """ 50 | Return true if a tweet has the given entity key. 51 | 52 | Args: 53 | status (tweepy.Status): A tweet 54 | entitykey (str): A possible entity name to check for, e.g. "urls". 55 | 56 | Returns: 57 | boolean 58 | """ 59 | try: 60 | return len(status.entities[entitykey]) > 0 61 | 62 | except AttributeError: 63 | return len(status["entities"][entitykey]) > 0 64 | 65 | 66 | def has_entities(status): 67 | """ 68 | Returns true if a Status object has entities. 69 | 70 | Args: 71 | status: either a tweepy.Status object or a dict returned from Twitter API 72 | """ 73 | try: 74 | if sum(len(v) for v in status.entities.values()) > 0: 75 | return True 76 | 77 | except AttributeError: 78 | if sum(len(v) for v in status["entities"].values()) > 0: 79 | return True 80 | 81 | return False 82 | 83 | 84 | def format_status(status): 85 | """Remove escaped characters from a status.""" 86 | return parser.unescape(status.text).strip() 87 | 88 | 89 | def remove_mentions(status): 90 | """Remove mentions from status text""" 91 | return remove_entities(status, ["user_mentions"]) 92 | 93 | 94 | def remove_urls(status): 95 | """Remove urls from status text""" 96 | return remove_entities(status, ["urls"]) 97 | 98 | 99 | def remove_symbols(status): 100 | """Remove symbols from status text""" 101 | return remove_entities(status, ["symbols"]) 102 | 103 | 104 | def remove_hashtags(status): 105 | """Remove hashtags from status text""" 106 | return remove_entities(status, ["hastags"]) 107 | 108 | 109 | def remove_entity(status, entitytype): 110 | """Use indices to remove given entity type from status text""" 111 | return remove_entities(status, [entitytype]) 112 | 113 | 114 | def remove_entities(status, entitylist): 115 | """Remove entities for a list of items.""" 116 | try: 117 | entities = status.entities 118 | text = status.text 119 | except AttributeError: 120 | entities = status.get("entities", dict()) 121 | text = status["text"] 122 | 123 | indices = [ent["indices"] for etype, entval in list(entities.items()) for ent in entval if etype in entitylist] 124 | indices.sort(key=lambda x: x[0], reverse=True) 125 | 126 | for start, end in indices: 127 | text = text[:start] + text[end:] 128 | 129 | return text 130 | 131 | 132 | def replace_urls(status): 133 | """ 134 | Replace shorturls in a status with expanded urls. 135 | 136 | Args: 137 | status (tweepy.status): A tweepy status object 138 | 139 | Returns: 140 | str 141 | """ 142 | text = status.text 143 | 144 | if not has_url(status): 145 | return text 146 | 147 | urls = [(e["indices"], e["expanded_url"]) for e in status.entities["urls"]] 148 | urls.sort(key=lambda x: x[0][0], reverse=True) 149 | 150 | for (start, end), url in urls: 151 | text = text[:start] + url + text[end:] 152 | 153 | return text 154 | 155 | 156 | def shorten(string, length=140, ellipsis=None): 157 | """ 158 | Shorten a string to 140 characters without breaking words. 159 | Optionally add an ellipsis character: '…' if ellipsis=True, or a given string 160 | e.g. ellipsis=' (cut)' 161 | """ 162 | # pylint: disable=redefined-outer-name 163 | string = string.strip() 164 | 165 | if len(string) > length: 166 | if ellipsis is True: 167 | ellipsis = "…" 168 | else: 169 | ellipsis = ellipsis or "" 170 | 171 | i = length - len(ellipsis) 172 | 173 | return " ".join(string[:i].split(" ")[:-1]).strip(",;:.") + ellipsis 174 | 175 | return string 176 | 177 | 178 | def queryize(terms, exclude_screen_name=None): 179 | """ 180 | Create query from list of terms, using OR 181 | but intelligently excluding terms beginning with '-' (Twitter's NOT operator). 182 | Optionally add -from:exclude_screen_name. 183 | 184 | >>> helpers.queryize(['apple', 'orange', '-peach']) 185 | u'apple OR orange -peach' 186 | 187 | Args: 188 | terms (list): Search terms. 189 | exclude_screen_name (str): A single screen name to exclude from the search. 190 | 191 | Returns: 192 | A string ready to be passed to tweepy.API.search 193 | """ 194 | ors = " OR ".join('"{}"'.format(x) for x in terms if not x.startswith("-")) 195 | nots = " ".join('-"{}"'.format(x[1:]) for x in terms if x.startswith("-")) 196 | screen_name = "-from:{}".format(exclude_screen_name) if exclude_screen_name else "" 197 | return " ".join((ors, nots, screen_name)) 198 | 199 | 200 | def chomp(text, max_len=280, split=None): 201 | """ 202 | Shorten a string so that it fits under max_len, splitting it at 'split'. 203 | Not guaranteed to return a string under max_len, as it may not be possible 204 | 205 | Args: 206 | text (str): String to shorten 207 | max_len (int): maximum length. default 140 208 | split (str): strings to split on (default is common punctuation: "-;,.") 209 | """ 210 | split = split or "—;,." 211 | while length(text) > max_len: 212 | try: 213 | text = re.split(r"[" + split + "]", text[::-1], 1)[1][::-1] 214 | except IndexError: 215 | return text 216 | 217 | return text 218 | 219 | 220 | def length(text, maxval=None): 221 | """ 222 | Count the length of a str the way Twitter does, 223 | double-counting "wide" characters (e.g. ideographs, emoji) 224 | 225 | Args: 226 | text (str): Text to count. 227 | maxval (int): The maximum encoding that will be counted as 1 character. 228 | Defaults to 4351 (ჿ GEORGIAN LETTER LABIAL SIGN, U+10FF) 229 | 230 | Returns: 231 | int 232 | """ 233 | maxval = maxval or 4351 234 | try: 235 | assert isinstance(text, str), "helpers.length requires a string argument" 236 | except AssertionError as err: 237 | raise TypeError from err 238 | return sum(2 if ord(x) > maxval else 1 for x in unicodedata.normalize("NFC", text)) 239 | -------------------------------------------------------------------------------- /docs/Makefile: -------------------------------------------------------------------------------- 1 | # Makefile for Sphinx documentation 2 | # 3 | 4 | # You can set these variables from the command line. 5 | SPHINXOPTS = 6 | SPHINXBUILD = sphinx-build 7 | PAPER = 8 | BUILDDIR = _build 9 | 10 | # User-friendly check for sphinx-build 11 | ifeq ($(shell which $(SPHINXBUILD) >/dev/null 2>&1; echo $$?), 1) 12 | $(error The '$(SPHINXBUILD)' command was not found. Make sure you have Sphinx installed, then set the SPHINXBUILD environment variable to point to the full path of the '$(SPHINXBUILD)' executable. Alternatively you can add the directory with the executable to your PATH. If you don't have Sphinx installed, grab it from http://sphinx-doc.org/) 13 | endif 14 | 15 | # Internal variables. 16 | PAPEROPT_a4 = -D latex_paper_size=a4 17 | PAPEROPT_letter = -D latex_paper_size=letter 18 | ALLSPHINXOPTS = $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) source 19 | # the i18n builder cannot share the environment and doctrees with the others 20 | I18NSPHINXOPTS = $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) source 21 | 22 | .PHONY: help clean html dirhtml singlehtml pickle json htmlhelp qthelp devhelp epub latex latexpdf text man changes linkcheck doctest coverage gettext 23 | 24 | help: 25 | @echo "Please use \`make ' where is one of" 26 | @echo " html to make standalone HTML files" 27 | @echo " dirhtml to make HTML files named index.html in directories" 28 | @echo " singlehtml to make a single large HTML file" 29 | @echo " pickle to make pickle files" 30 | @echo " json to make JSON files" 31 | @echo " htmlhelp to make HTML files and a HTML help project" 32 | @echo " qthelp to make HTML files and a qthelp project" 33 | @echo " applehelp to make an Apple Help Book" 34 | @echo " devhelp to make HTML files and a Devhelp project" 35 | @echo " epub to make an epub" 36 | @echo " latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter" 37 | @echo " latexpdf to make LaTeX files and run them through pdflatex" 38 | @echo " latexpdfja to make LaTeX files and run them through platex/dvipdfmx" 39 | @echo " text to make text files" 40 | @echo " man to make manual pages" 41 | @echo " texinfo to make Texinfo files" 42 | @echo " info to make Texinfo files and run them through makeinfo" 43 | @echo " gettext to make PO message catalogs" 44 | @echo " changes to make an overview of all changed/added/deprecated items" 45 | @echo " xml to make Docutils-native XML files" 46 | @echo " pseudoxml to make pseudoxml-XML files for display purposes" 47 | @echo " linkcheck to check all external links for integrity" 48 | @echo " doctest to run all doctests embedded in the documentation (if enabled)" 49 | @echo " coverage to run coverage check of the documentation (if enabled)" 50 | 51 | clean: 52 | rm -rf $(BUILDDIR)/* 53 | 54 | html: 55 | $(SPHINXBUILD) -b html $(ALLSPHINXOPTS) $(BUILDDIR)/html 56 | @echo 57 | @echo "Build finished. The HTML pages are in $(BUILDDIR)/html." 58 | 59 | dirhtml: 60 | $(SPHINXBUILD) -b dirhtml $(ALLSPHINXOPTS) $(BUILDDIR)/dirhtml 61 | @echo 62 | @echo "Build finished. The HTML pages are in $(BUILDDIR)/dirhtml." 63 | 64 | singlehtml: 65 | $(SPHINXBUILD) -b singlehtml $(ALLSPHINXOPTS) $(BUILDDIR)/singlehtml 66 | @echo 67 | @echo "Build finished. The HTML page is in $(BUILDDIR)/singlehtml." 68 | 69 | pickle: 70 | $(SPHINXBUILD) -b pickle $(ALLSPHINXOPTS) $(BUILDDIR)/pickle 71 | @echo 72 | @echo "Build finished; now you can process the pickle files." 73 | 74 | json: 75 | $(SPHINXBUILD) -b json $(ALLSPHINXOPTS) $(BUILDDIR)/json 76 | @echo 77 | @echo "Build finished; now you can process the JSON files." 78 | 79 | htmlhelp: 80 | $(SPHINXBUILD) -b htmlhelp $(ALLSPHINXOPTS) $(BUILDDIR)/htmlhelp 81 | @echo 82 | @echo "Build finished; now you can run HTML Help Workshop with the" \ 83 | ".hhp project file in $(BUILDDIR)/htmlhelp." 84 | 85 | qthelp: 86 | $(SPHINXBUILD) -b qthelp $(ALLSPHINXOPTS) $(BUILDDIR)/qthelp 87 | @echo 88 | @echo "Build finished; now you can run "qcollectiongenerator" with the" \ 89 | ".qhcp project file in $(BUILDDIR)/qthelp, like this:" 90 | @echo "# qcollectiongenerator $(BUILDDIR)/qthelp/TwitterBotUtils.qhcp" 91 | @echo "To view the help file:" 92 | @echo "# assistant -collectionFile $(BUILDDIR)/qthelp/TwitterBotUtils.qhc" 93 | 94 | applehelp: 95 | $(SPHINXBUILD) -b applehelp $(ALLSPHINXOPTS) $(BUILDDIR)/applehelp 96 | @echo 97 | @echo "Build finished. The help book is in $(BUILDDIR)/applehelp." 98 | @echo "N.B. You won't be able to view it unless you put it in" \ 99 | "~/Library/Documentation/Help or install it in your application" \ 100 | "bundle." 101 | 102 | devhelp: 103 | $(SPHINXBUILD) -b devhelp $(ALLSPHINXOPTS) $(BUILDDIR)/devhelp 104 | @echo 105 | @echo "Build finished." 106 | @echo "To view the help file:" 107 | @echo "# mkdir -p $$HOME/.local/share/devhelp/TwitterBotUtils" 108 | @echo "# ln -s $(BUILDDIR)/devhelp $$HOME/.local/share/devhelp/TwitterBotUtils" 109 | @echo "# devhelp" 110 | 111 | epub: 112 | $(SPHINXBUILD) -b epub $(ALLSPHINXOPTS) $(BUILDDIR)/epub 113 | @echo 114 | @echo "Build finished. The epub file is in $(BUILDDIR)/epub." 115 | 116 | latex: 117 | $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex 118 | @echo 119 | @echo "Build finished; the LaTeX files are in $(BUILDDIR)/latex." 120 | @echo "Run \`make' in that directory to run these through (pdf)latex" \ 121 | "(use \`make latexpdf' here to do that automatically)." 122 | 123 | latexpdf: 124 | $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex 125 | @echo "Running LaTeX files through pdflatex..." 126 | $(MAKE) -C $(BUILDDIR)/latex all-pdf 127 | @echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex." 128 | 129 | latexpdfja: 130 | $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex 131 | @echo "Running LaTeX files through platex and dvipdfmx..." 132 | $(MAKE) -C $(BUILDDIR)/latex all-pdf-ja 133 | @echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex." 134 | 135 | text: 136 | $(SPHINXBUILD) -b text $(ALLSPHINXOPTS) $(BUILDDIR)/text 137 | @echo 138 | @echo "Build finished. The text files are in $(BUILDDIR)/text." 139 | 140 | man: 141 | $(SPHINXBUILD) -b man $(ALLSPHINXOPTS) $(BUILDDIR)/man 142 | @echo 143 | @echo "Build finished. The manual pages are in $(BUILDDIR)/man." 144 | 145 | texinfo: 146 | $(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo 147 | @echo 148 | @echo "Build finished. The Texinfo files are in $(BUILDDIR)/texinfo." 149 | @echo "Run \`make' in that directory to run these through makeinfo" \ 150 | "(use \`make info' here to do that automatically)." 151 | 152 | info: 153 | $(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo 154 | @echo "Running Texinfo files through makeinfo..." 155 | make -C $(BUILDDIR)/texinfo info 156 | @echo "makeinfo finished; the Info files are in $(BUILDDIR)/texinfo." 157 | 158 | gettext: 159 | $(SPHINXBUILD) -b gettext $(I18NSPHINXOPTS) $(BUILDDIR)/locale 160 | @echo 161 | @echo "Build finished. The message catalogs are in $(BUILDDIR)/locale." 162 | 163 | changes: 164 | $(SPHINXBUILD) -b changes $(ALLSPHINXOPTS) $(BUILDDIR)/changes 165 | @echo 166 | @echo "The overview file is in $(BUILDDIR)/changes." 167 | 168 | linkcheck: 169 | $(SPHINXBUILD) -b linkcheck $(ALLSPHINXOPTS) $(BUILDDIR)/linkcheck 170 | @echo 171 | @echo "Link check complete; look for any errors in the above output " \ 172 | "or in $(BUILDDIR)/linkcheck/output.txt." 173 | 174 | doctest: 175 | $(SPHINXBUILD) -b doctest $(ALLSPHINXOPTS) $(BUILDDIR)/doctest 176 | @echo "Testing of doctests in the sources finished, look at the " \ 177 | "results in $(BUILDDIR)/doctest/output.txt." 178 | 179 | coverage: 180 | $(SPHINXBUILD) -b coverage $(ALLSPHINXOPTS) $(BUILDDIR)/coverage 181 | @echo "Testing of coverage in the sources finished, look at the " \ 182 | "results in $(BUILDDIR)/coverage/python.txt." 183 | 184 | xml: 185 | $(SPHINXBUILD) -b xml $(ALLSPHINXOPTS) $(BUILDDIR)/xml 186 | @echo 187 | @echo "Build finished. The XML files are in $(BUILDDIR)/xml." 188 | 189 | pseudoxml: 190 | $(SPHINXBUILD) -b pseudoxml $(ALLSPHINXOPTS) $(BUILDDIR)/pseudoxml 191 | @echo 192 | @echo "Build finished. The pseudo-XML files are in $(BUILDDIR)/pseudoxml." 193 | -------------------------------------------------------------------------------- /src/twitter_bot_utils/cli.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # Copyright 2014-17 Neil Freeman contact@fakeisthenewreal.org 3 | # This program is free software: you can redistribute it and/or modify 4 | # it under the terms of the GNU General Public License as published by 5 | # the Free Software Foundation, either version 3 of the License, or 6 | # (at your option) any later version. 7 | 8 | # This program is distributed in the hope that it will be useful, 9 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 10 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 | # GNU General Public License for more details. 12 | 13 | # You should have received a copy of the GNU General Public License 14 | # along with this program. If not, see . 15 | 16 | """Command line interface for twitter-bot-utils""" 17 | 18 | import logging 19 | import sys 20 | from argparse import ArgumentParser 21 | 22 | import tweepy 23 | 24 | from . import __version__ as version 25 | from . import api, args, confighelper, tools 26 | 27 | ARGS = ["config", "dry-run", "verbose", "quiet"] 28 | AUTHORIZATION_FAILED_MESSAGE = "Authorization failed. Check that the consumer key and secret are correct." 29 | DEPRECATION = "This command is deprecated. Please use the tbu command." 30 | 31 | 32 | def fave_mentions(arguments=None): 33 | """Add a favorite to recent mentions.""" 34 | if arguments is None: 35 | parser = ArgumentParser(description="fave/like mentions", usage="%(prog)s [options] screen_name") 36 | parser.add_argument("screen_name", type=str) 37 | args.add_default_args(parser, version=version, include=ARGS) 38 | print(DEPRECATION, file=sys.stderr) 39 | 40 | arguments = parser.parse_args() 41 | twitter = api.API(arguments) 42 | 43 | tools.fave_mentions(twitter, arguments.dry_run) 44 | 45 | 46 | def auto_follow(arguments=None): 47 | """Follow-back recent followers.""" 48 | if arguments is None: 49 | parser = ArgumentParser( 50 | description="automatic following and unfollowing", 51 | usage="%(prog)s [options] screen_name", 52 | ) 53 | parser.add_argument("screen_name", type=str) 54 | parser.add_argument("-U", "--unfollow", action="store_true", help="Unfollow those who don't follow you") 55 | args.add_default_args(parser, version=version, include=ARGS) 56 | arguments = parser.parse_args() 57 | print(DEPRECATION, file=sys.stderr) 58 | 59 | twitter = api.API(arguments) 60 | 61 | if arguments.unfollow: 62 | tools.unfollow(twitter, arguments.dry_run) 63 | else: 64 | tools.follow_back(twitter, arguments.dry_run) 65 | 66 | 67 | def authenticate(arguments=None): 68 | """Authenticate with Twitter API""" 69 | if arguments is None: 70 | parser = ArgumentParser(description="Authorize an account with a twitter application.") 71 | 72 | parser.add_argument("-c", metavar="file", type=str, default=None, dest="config_file", help="config file") 73 | parser.add_argument("--app", metavar="app", type=str, help="app name in config file") 74 | parser.add_argument("-s", "--save", action="store_true", help="Save details to config file") 75 | 76 | parser.add_argument("--consumer-key", metavar="key", type=str, help="consumer key (aka consumer token)") 77 | parser.add_argument("--consumer-secret", metavar="secret", type=str, help="consumer secret") 78 | parser.add_argument("-V", "--version", action="version", version="%(prog)s " + version) 79 | 80 | arguments = parser.parse_args() 81 | print(DEPRECATION, file=sys.stderr) 82 | 83 | # it's possible to pass keys and then save them to the files 84 | if arguments.config_file: 85 | file_name = confighelper.find_file(arguments.config_file) 86 | config = confighelper.parse(file_name) 87 | else: 88 | file_name = None 89 | config = {} 90 | 91 | # Use passed credentials. 92 | if arguments.consumer_key and arguments.consumer_secret: 93 | consumer_key = arguments.consumer_key 94 | consumer_secret = arguments.consumer_secret 95 | 96 | # Go find credentials. 97 | else: 98 | try: 99 | conf = config["apps"][arguments.app] if arguments.app else config 100 | 101 | consumer_secret = conf["consumer_secret"] 102 | consumer_key = conf["consumer_key"] 103 | 104 | except KeyError as err: 105 | msg = "Couldn't find consumer-key and consumer-secret for '{}' in {}".format(arguments.app, file_name) 106 | raise KeyError(msg) from err 107 | 108 | auth = tweepy.OAuthHandler(consumer_key, consumer_secret, "oob") 109 | 110 | print(auth.get_authorization_url()) 111 | verifier = input("Please visit this url, click 'Authorize app' and enter in the PIN:\n> ") 112 | 113 | try: 114 | auth.get_access_token(verifier) 115 | except tweepy.error.TweepError: 116 | print(AUTHORIZATION_FAILED_MESSAGE) 117 | return 118 | 119 | # True is the const passed when no file name is given 120 | if arguments.save is not True: 121 | file_name = arguments.save 122 | 123 | # Save the keys back to the config file 124 | if arguments.save and file_name: 125 | apps = config["apps"] = config.get("apps", {}) 126 | users = config["users"] = config.get("users", {}) 127 | 128 | app = arguments.app or "default" 129 | screen_name = auth.get_username().encode("utf-8") 130 | 131 | apps[app] = apps.get(app, {}) 132 | apps[app].update( 133 | { 134 | "consumer_key": consumer_key, 135 | "consumer_secret": consumer_secret, 136 | } 137 | ) 138 | users[screen_name] = users.get(screen_name, {}) 139 | users[screen_name].update( 140 | { 141 | "key": auth.access_token.encode("utf-8"), 142 | "secret": auth.access_token_secret.encode("utf-8"), 143 | "app": (arguments.app or "default"), 144 | } 145 | ) 146 | 147 | confighelper.dump(config, file_name) 148 | 149 | print(f"Saved keys in {file_name}") 150 | 151 | # Or just print them 152 | else: 153 | print(f"key: {auth.access_token}\nsecret: {auth.access_token_secret}") 154 | 155 | 156 | def post(arguments): 157 | """Post text to a given twitter account.""" 158 | twitter = api.API(arguments) 159 | params = {} 160 | 161 | if arguments.update == "-": 162 | params["status"] = sys.stdin.read() 163 | else: 164 | params["status"] = arguments.update 165 | 166 | if arguments.media_file: 167 | medias = [twitter.media_upload(m) for m in arguments.media_file] 168 | params["media_ids"] = [m.media_id for m in medias] 169 | 170 | try: 171 | logging.getLogger(arguments.screen_name).info("status: %s", params["status"]) 172 | if not arguments.dry_run: 173 | twitter.update_status(**params) 174 | 175 | except tweepy.TweepError as err: 176 | logging.getLogger(arguments.screen_name).error(err) 177 | 178 | 179 | def retweet(arguments): 180 | """Retweet a status""" 181 | twitter = api.API(arguments) 182 | twitter.retweet(id=arguments.id) 183 | 184 | 185 | def main(): 186 | """Command line interface for `tbu`.""" 187 | parser = ArgumentParser() 188 | parser.add_argument("-V", "--version", action="version", version="%(prog)s " + version) 189 | 190 | subparsers = parser.add_subparsers() 191 | 192 | poster = subparsers.add_parser( 193 | "post", 194 | description="Post text to a given twitter account", 195 | usage='%(prog)s screen_name "update" [options]', 196 | ) 197 | poster.add_argument("screen_name", type=str) 198 | poster.add_argument("update", type=str) 199 | poster.add_argument("-m", "--media-file", type=str, action="append") 200 | args.add_default_args(poster, include=["config", "dry-run", "verbose", "quiet"]) 201 | poster.set_defaults(func=post) 202 | 203 | follow = subparsers.add_parser( 204 | "follow", 205 | description="automatic following and unfollowing", 206 | usage="%(prog)s [options] screen_name", 207 | ) 208 | follow.add_argument("screen_name", type=str) 209 | follow.add_argument("-U", "--unfollow", action="store_true", help="Unfollow those who don't follow you") 210 | follow.set_defaults(func=auto_follow) 211 | 212 | auth = subparsers.add_parser( 213 | "auth", 214 | description="Authorize an account with a twitter application.", 215 | usage="%(prog)s [options]", 216 | ) 217 | auth.add_argument("-c", metavar="file", type=str, default=None, dest="config_file", help="config file") 218 | auth.add_argument("--app", metavar="app", type=str, help="app name in config file") 219 | auth.add_argument( 220 | "-s", 221 | "--save", 222 | nargs="?", 223 | const=True, 224 | help="Save details to config file. If no file is given, uses file in --config.", 225 | ) 226 | auth.add_argument("--consumer-key", metavar="key", type=str, help="consumer key (aka consumer token)") 227 | auth.add_argument("--consumer-secret", metavar="secret", type=str, help="consumer secret") 228 | auth.set_defaults(func=authenticate) 229 | 230 | fave = subparsers.add_parser("like", description="fave/like mentions", usage="%(prog)s [options] screen_name") 231 | fave.add_argument("screen_name", type=str) 232 | fave.set_defaults(func=fave) 233 | 234 | arguments = parser.parse_args() 235 | arguments.func(arguments) 236 | -------------------------------------------------------------------------------- /docs/source/conf.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # Twitter Bot Utils documentation build configuration file, created by 4 | # sphinx-quickstart on Sun Feb 14 17:14:34 2016. 5 | # 6 | # This file is execfile()d with the current directory set to its 7 | # containing dir. 8 | # 9 | # Note that not all possible configuration values are present in this 10 | # autogenerated file. 11 | # 12 | # All configuration values have a default; values that are commented out 13 | # serve to show the default. 14 | 15 | import sys 16 | import os 17 | import shlex 18 | 19 | # If extensions (or modules to document with autodoc) are in another directory, 20 | # add these directories to sys.path here. If the directory is relative to the 21 | # documentation root, use os.path.abspath to make it absolute, like shown here. 22 | #sys.path.insert(0, os.path.abspath('.')) 23 | 24 | # -- General configuration ------------------------------------------------ 25 | 26 | # If your documentation needs a minimal Sphinx version, state it here. 27 | #needs_sphinx = '1.0' 28 | 29 | # Add any Sphinx extension module names here, as strings. They can be 30 | # extensions coming with Sphinx (named 'sphinx.ext.*') or your custom 31 | # ones. 32 | extensions = [ 33 | 'sphinx.ext.autodoc', 34 | 'sphinx.ext.coverage', 35 | 'sphinx.ext.napoleon', 36 | ] 37 | 38 | # Add any paths that contain templates here, relative to this directory. 39 | templates_path = ['_templates'] 40 | 41 | # The suffix(es) of source filenames. 42 | # You can specify multiple suffix as a list of string: 43 | # source_suffix = ['.rst', '.md'] 44 | source_suffix = '.rst' 45 | 46 | # The encoding of source files. 47 | #source_encoding = 'utf-8-sig' 48 | 49 | # The master toctree document. 50 | master_doc = 'index' 51 | 52 | # General information about the project. 53 | project = u'Twitter Bot Utils' 54 | copyright = u'2016, Neil Freeman' 55 | author = u'Neil Freeman' 56 | 57 | # The version info for the project you're documenting, acts as replacement for 58 | # |version| and |release|, also used in various other places throughout the 59 | # built documents. 60 | # 61 | with open(os.path.join(os.path.dirname(__file__), '../..', 'src/twitter_bot_utils/__init__.py')) as i: 62 | version = next(r for r in i.readlines() if '__version__' in r).split('=')[1].strip('"\' \n') 63 | 64 | # The short X.Y version. 65 | # The full version, including alpha/beta/rc tags. 66 | release = version 67 | 68 | # The language for content autogenerated by Sphinx. Refer to documentation 69 | # for a list of supported languages. 70 | # 71 | # This is also used if you do content translation via gettext catalogs. 72 | # Usually you set "language" from the command line for these cases. 73 | language = None 74 | 75 | # There are two options for replacing |today|: either, you set today to some 76 | # non-false value, then it is used: 77 | #today = '' 78 | # Else, today_fmt is used as the format for a strftime call. 79 | #today_fmt = '%B %d, %Y' 80 | 81 | # List of patterns, relative to source directory, that match files and 82 | # directories to ignore when looking for source files. 83 | exclude_patterns = [] 84 | 85 | # The reST default role (used for this markup: `text`) to use for all 86 | # documents. 87 | #default_role = None 88 | 89 | # If true, '()' will be appended to :func: etc. cross-reference text. 90 | #add_function_parentheses = True 91 | 92 | # If true, the current module name will be prepended to all description 93 | # unit titles (such as .. function::). 94 | #add_module_names = True 95 | 96 | # If true, sectionauthor and moduleauthor directives will be shown in the 97 | # output. They are ignored by default. 98 | #show_authors = False 99 | 100 | # The name of the Pygments (syntax highlighting) style to use. 101 | pygments_style = 'sphinx' 102 | 103 | # A list of ignored prefixes for module index sorting. 104 | #modindex_common_prefix = [] 105 | 106 | # If true, keep warnings as "system message" paragraphs in the built documents. 107 | #keep_warnings = False 108 | 109 | # If true, `todo` and `todoList` produce output, else they produce nothing. 110 | todo_include_todos = False 111 | 112 | 113 | # -- Options for HTML output ---------------------------------------------- 114 | 115 | # The theme to use for HTML and HTML Help pages. See the documentation for 116 | # a list of builtin themes. 117 | html_theme = 'sphinx_rtd_theme' 118 | nosidebar = True 119 | 120 | # Theme options are theme-specific and customize the look and feel of a theme 121 | # further. For a list of options available for each theme, see the 122 | # documentation. 123 | #html_theme_options = {} 124 | 125 | # Add any paths that contain custom themes here, relative to this directory. 126 | #html_theme_path = [] 127 | 128 | # The name for this set of Sphinx documents. If None, it defaults to 129 | # " v documentation". 130 | #html_title = None 131 | 132 | # A shorter title for the navigation bar. Default is the same as html_title. 133 | #html_short_title = None 134 | 135 | # The name of an image file (relative to this directory) to place at the top 136 | # of the sidebar. 137 | #html_logo = None 138 | 139 | # The name of an image file (within the static path) to use as favicon of the 140 | # docs. This file should be a Windows icon file (.ico) being 16x16 or 32x32 141 | # pixels large. 142 | #html_favicon = None 143 | 144 | # Add any paths that contain custom static files (such as style sheets) here, 145 | # relative to this directory. They are copied after the builtin static files, 146 | # so a file named "default.css" will overwrite the builtin "default.css". 147 | html_static_path = ['_static'] 148 | 149 | # Add any extra paths that contain custom files (such as robots.txt or 150 | # .htaccess) here, relative to this directory. These files are copied 151 | # directly to the root of the documentation. 152 | #html_extra_path = [] 153 | 154 | # If not '', a 'Last updated on:' timestamp is inserted at every page bottom, 155 | # using the given strftime format. 156 | #html_last_updated_fmt = '%b %d, %Y' 157 | 158 | # If true, SmartyPants will be used to convert quotes and dashes to 159 | # typographically correct entities. 160 | #html_use_smartypants = True 161 | 162 | # Custom sidebar templates, maps document names to template names. 163 | #html_sidebars = {} 164 | 165 | # Additional templates that should be rendered to pages, maps page names to 166 | # template names. 167 | #html_additional_pages = {} 168 | 169 | # If false, no module index is generated. 170 | #html_domain_indices = True 171 | 172 | # If false, no index is generated. 173 | #html_use_index = True 174 | 175 | # If true, the index is split into individual pages for each letter. 176 | #html_split_index = False 177 | 178 | # If true, links to the reST sources are added to the pages. 179 | html_show_sourcelink = False 180 | 181 | # If true, "Created using Sphinx" is shown in the HTML footer. Default is True. 182 | html_show_sphinx = False 183 | 184 | # If true, "(C) Copyright ..." is shown in the HTML footer. Default is True. 185 | #html_show_copyright = True 186 | 187 | # If true, an OpenSearch description file will be output, and all pages will 188 | # contain a tag referring to it. The value of this option must be the 189 | # base URL from which the finished HTML is served. 190 | #html_use_opensearch = '' 191 | 192 | # This is the file name suffix for HTML files (e.g. ".xhtml"). 193 | #html_file_suffix = None 194 | 195 | # Language to be used for generating the HTML full-text search index. 196 | # Sphinx supports the following languages: 197 | # 'da', 'de', 'en', 'es', 'fi', 'fr', 'hu', 'it', 'ja' 198 | # 'nl', 'no', 'pt', 'ro', 'ru', 'sv', 'tr' 199 | #html_search_language = 'en' 200 | 201 | # A dictionary with options for the search language support, empty by default. 202 | # Now only 'ja' uses this config value 203 | #html_search_options = {'type': 'default'} 204 | 205 | # The name of a javascript file (relative to the configuration directory) that 206 | # implements a search results scorer. If empty, the default will be used. 207 | #html_search_scorer = 'scorer.js' 208 | 209 | # Output file base name for HTML help builder. 210 | htmlhelp_basename = 'TwitterBotUtilsdoc' 211 | 212 | # -- Options for LaTeX output --------------------------------------------- 213 | 214 | latex_elements = { 215 | # The paper size ('letterpaper' or 'a4paper'). 216 | #'papersize': 'letterpaper', 217 | 218 | # The font size ('10pt', '11pt' or '12pt'). 219 | #'pointsize': '10pt', 220 | 221 | # Additional stuff for the LaTeX preamble. 222 | #'preamble': '', 223 | 224 | # Latex figure (float) alignment 225 | #'figure_align': 'htbp', 226 | } 227 | 228 | # Grouping the document tree into LaTeX files. List of tuples 229 | # (source start file, target name, title, 230 | # author, documentclass [howto, manual, or own class]). 231 | latex_documents = [ 232 | (master_doc, 'TwitterBotUtils.tex', u'Twitter Bot Utils Documentation', 233 | u'Neil Freeman', 'manual'), 234 | ] 235 | 236 | # The name of an image file (relative to this directory) to place at the top of 237 | # the title page. 238 | #latex_logo = None 239 | 240 | # For "manual" documents, if this is true, then toplevel headings are parts, 241 | # not chapters. 242 | #latex_use_parts = False 243 | 244 | # If true, show page references after internal links. 245 | #latex_show_pagerefs = False 246 | 247 | # If true, show URL addresses after external links. 248 | #latex_show_urls = False 249 | 250 | # Documents to append as an appendix to all manuals. 251 | #latex_appendices = [] 252 | 253 | # If false, no module index is generated. 254 | #latex_domain_indices = True 255 | 256 | 257 | # -- Options for manual page output --------------------------------------- 258 | 259 | # One entry per manual page. List of tuples 260 | # (source start file, name, description, authors, manual section). 261 | man_pages = [ 262 | (master_doc, 'twitterbotutils', u'Twitter Bot Utils Documentation', 263 | [author], 1) 264 | ] 265 | 266 | # If true, show URL addresses after external links. 267 | #man_show_urls = False 268 | 269 | 270 | # -- Options for Texinfo output ------------------------------------------- 271 | 272 | # Grouping the document tree into Texinfo files. List of tuples 273 | # (source start file, target name, title, author, 274 | # dir menu entry, description, category) 275 | texinfo_documents = [ 276 | (master_doc, 'TwitterBotUtils', u'Twitter Bot Utils Documentation', 277 | author, 'TwitterBotUtils', 'One line description of project.', 278 | 'Miscellaneous'), 279 | ] 280 | 281 | # Documents to append as an appendix to all manuals. 282 | #texinfo_appendices = [] 283 | 284 | # If false, no module index is generated. 285 | #texinfo_domain_indices = True 286 | 287 | # How to display URL addresses: 'footnote', 'no', or 'inline'. 288 | #texinfo_show_urls = 'footnote' 289 | 290 | # If true, do not generate a @detailmenu in the "Top" node's menu. 291 | #texinfo_no_detailmenu = False 292 | 293 | napoleon_numpy_docstring = False 294 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # twitter bot utils 2 | 3 | Twitter bot utils make it a little easier to set up a Twitter bot, with an eye to making config and command-line options easy to manage and reproduce. They're intended for managing a small-to-medium-sized coterie of Twitter accounts on one machine. The package is a super-simple wrapper for the excellent [Tweepy](http://tweepy.org) library. It also provides shortcuts for setting up command line tools with [argparse](https://docs.python.org/3/library/argparse.html). 4 | 5 | This package is intended to assist with the creation of bots for artistic or personal projects. Don't use it to spam or harrass people. 6 | 7 | Works with python >= 3.6. 8 | 9 | Install with `pip install twitter_bot_utils`. 10 | 11 | See a basic run through in the [Hello World](https://pythonhosted.org/twitter_bot_utils/helloworld.html) section of the [documentation](https://pythonhosted.org/twitter_bot_utils). 12 | 13 | ## Authenticating 14 | 15 | One hurdle with setting up bots is getting the proper authentication keys. It can be a bit of a pain to log in and out of Twitter's app site. Twitter bot utils comes with `tbu auth`, a command line helper for this: 16 | ```` 17 | $ tbu auth --consumer-key 1233... --consumer-secret 345... 18 | ```` 19 | 20 | This will prompt you with an url. Open this in a browser where your bot is logged in, click "Authorize". Twitter will show you an authorization code, enter this on the command line, and presto! your keys will be displayed. 21 | 22 | `tbu auth` is inspired by a feature of [`twurl`](https://github.com/twitter/twurl), Twitter's full-fledged command line tool. 23 | 24 | ## Config files 25 | 26 | One goal of Twitter Bot Utils is to create Tweepy instances with authentication data stored in a simple config file. This gives botmakers a simple, reusable place to store keys outside of source control. 27 | 28 | By default, Twitter bot utils looks for a file called `bots.yaml` (or `.yml`) in the current directory, your home directory (`~/`) or the `~/bots` directory. Custom config locations can be set, too. 29 | 30 | These are two ways to lay out a bots config file. The basic way covers just one user and one app: 31 | 32 | ````yaml 33 | token: ... 34 | secret: ... 35 | consumer_key: ... 36 | consumer_secret: ... 37 | my_setting: "bots are good" 38 | ```` 39 | 40 | If you have more than one bot, a simple setup is to have one app for each bot. Visit [apps.twitter.com](https://apps.twitter.com), register the app, and then choose "Create my access token" in the "keys and tokens" tab. 41 | ````yaml 42 | general_setting: "all bots share this setting" 43 | 44 | users: 45 | # twitter screen_name 46 | MyBotName: 47 | token: ... 48 | secret: ... 49 | consumer_key: ... 50 | consumer_secret: ... 51 | custom_setting: "bots are great" 52 | 53 | other_bot: 54 | ... 55 | ```` 56 | 57 | If you have one app shared by several bots, create an `apps` section in the config file: 58 | ````yaml 59 | apps: 60 | my_app_name: 61 | consumer_key: ... 62 | consumer_secret: ... 63 | users: 64 | MyBotName: 65 | token: ... 66 | secret: ... 67 | app: my_app_name 68 | ```` 69 | 70 | The `twitter-auth` utility will happily read settings from a `bots.yaml` file set up like this: 71 | ```` 72 | twitter-auth -c ~/bots.yaml --app my_app_name 73 | ```` 74 | 75 | ## Using config files to talk to Twitter 76 | 77 | Using a config file in one of the default locations doesn't require any extra settings: 78 | 79 | ````python 80 | import twitter_bot_utils as tbu 81 | 82 | # Automatically check for a config file in the above-named directories 83 | twitter = tbu.API(screen_name='MyBotName') 84 | ```` 85 | 86 | The `twitter` object is a fully-authenticated tweepy API object. So you can now do this: 87 | ````python 88 | twitter.update_status(status='hello world') 89 | ```` 90 | 91 | The `bots` config file is also useful for storing keys and parameters for other APIs, or for your own bots. 92 | 93 | ````python 94 | # Get a config settings from your bots config file. This might be the key for a third-party API 95 | # Use a general setting 96 | twitter.config['general_setting'] 97 | # "all bots share this setting" 98 | 99 | # Settings from the user and app section are also available: 100 | twitter.config['custom_setting'] 101 | # "bots are great" 102 | 103 | twitter.config['app_setting'] 104 | # "apple juice" 105 | ```` 106 | 107 | Set a custom config file with the `config_file` argument: 108 | ```` 109 | # Specify a specific config file 110 | twitter = tbu.API(screen_name='MyBotName', config_file='path/to/config.yaml') 111 | ```` 112 | 113 | Twitter bot utils comes with some built-in command line parsers, and the API object will also happily consume the result of `argparse.parser.parse_args()` (see below for details). 114 | 115 | ### Without user authentication 116 | 117 | Some Twitter API queries don't require user authentication. To set up an Tweepy API instance without user authentication, set up a bots.yaml file as above, but omit the `users` section. Use the app keyword argument: 118 | ````python 119 | twitter = tbu.API(app='my_app_name', config_file='path/to/config.yaml') 120 | 121 | twitter.search(q="Twitter searches don't require user authentication") 122 | ```` 123 | 124 | ## Recent tweets 125 | 126 | The `twitter_bot_utils.API` object extends `tweepy.API` with some methods useful for bots: 127 | 128 | * Methods to check for the ID of recent tweets: `last_tweet`, `last_reply`, `last_retweet`. These are useful if your bot searches twitter and wants to avoid ingesting the same material. 129 | 130 | ````python 131 | twitter = tbu.API(screen_name='MyBotName') 132 | 133 | twitter.last_tweet 134 | # id of most recent tweet from MyBotName 135 | 136 | twitter.last_reply 137 | # id of most recent reply from MyBotName 138 | 139 | twitter.last_retweet 140 | # id of most recent retweet from MyBotName 141 | 142 | # Example: what's happened since the last time the bot was active? 143 | twitter.search('#botALLY', since_id=twitter.last_tweet) 144 | ```` 145 | 146 | Twitter bot utils also adds a retry in `update_status` when Twitter is over capacity. If `update_status` gets a 503 error from Twitter, it will wait 10 seconds and try again. 147 | 148 | ## Default Command Line Options 149 | 150 | It's useful to package bots as command line apps so that they can be easily run with `cron`. Twitter bot utils includes some helpers for working with `argparse`. 151 | 152 | Some useful command line flags are available by default: 153 | 154 | * `-u, --user`: Screen name to run as 155 | * `-n, --dry-run`: Don't tweet, just output to stdout 156 | * `-v, --verbose`: Log to stdout 157 | * `-q, --quiet`: Only log errors 158 | * `-c, --config`: path to a config file. This is a JSON or YAML file laid out according to the above format. This option isn't needed if the config file is in one of the default places. 159 | 160 | Say this is `mybot.py`: 161 | 162 | ````python 163 | import argparse 164 | import twitter_bot_utils as tbu 165 | 166 | # This sets up an argparse.ArgumentParser with the default arguments 167 | parent = tbu.args.parent() 168 | parser = argparse.ArgumentParser('My Example Bot', parents=[parent]) 169 | parser.add_argument('--my-arg', type=str, help='A custom argument') 170 | 171 | args = parser.parse_args() 172 | 173 | # Set up the tweepy API 174 | # Note that you can pass the argparse.Namespace object 175 | twitter = tbu.API(args) 176 | 177 | # Generate a tweet somehow 178 | tweet = my_tweet_function(args.my_arg) 179 | 180 | # The API includes an instance of logging 181 | # debug logs will output to stdout only if --verbose is set 182 | # info logs will output even without --verbose 183 | api.logger.debug("Generated %s", tweet) 184 | 185 | # Use args.dry_run to control tweeting 186 | if not args.dry_run: 187 | twitter.update_status(tweet) 188 | ```` 189 | 190 | Then on the command line: 191 | ````bash 192 | > python mybot.py --help 193 | usage: mybot.py [options] 194 | 195 | My Example Bot 196 | 197 | optional arguments: 198 | -h, --help show this help message and exit 199 | -c PATH, --config PATH 200 | bots config file (json or yaml) 201 | -u SCREEN_NAME, --user SCREEN_NAME 202 | Twitter screen name 203 | -n, --dry-run Don't actually do anything 204 | -v, --verbose Run talkatively 205 | -q, --quiet Run quietly 206 | --my-arg MY_ARG A custom argument 207 | 208 | # Looks for settings in a config file (e.g. bots.yaml, see config section above) 209 | # Prints results to stdout and doesn't publish anything 210 | > python yourapp.py --dry-run --verbose 211 | Generated 212 | 213 | # Run quietly, say in a crontab file 214 | > python yourapp.py --user MyBotName --quiet 215 | Generated 216 | ```` 217 | 218 | ## Helpers 219 | ### Checking for entities 220 | 221 | Easily check if tweets have specific entities: 222 | 223 | ````python 224 | import twitter_bot_utils 225 | 226 | # Don't set include_entities to False and expect the below to work 227 | statuses = twitter.search('example search', include_entities=True) 228 | 229 | status = status[0] 230 | 231 | twitter_bot_utils.helpers.has_mention(status) 232 | # returns True if status has one or more mentions, otherwise False 233 | 234 | twitter_bot_utils.helpers.has_hashtag(status) 235 | # returns True if status has one or more hashtags, otherwise False 236 | 237 | twitter_bot_utils.helpers.has_media(status) 238 | # returns True if status has one or more media entities (images, video), otherwise False 239 | 240 | twitter_bot_utils.helpers.has_entities(status) 241 | # returns True if status has any entities 242 | 243 | # These also exist: 244 | twitter_bot_utils.helpers.has_url 245 | twitter_bot_utils.helpers.has_symbol 246 | ```` 247 | 248 | ### Filtering out entities 249 | 250 | These helpers remove entities from a tweet's text. 251 | 252 | ````python 253 | import twitter_bot_utils as tbu 254 | 255 | api = tbu.API(screen_name='MyBotName') 256 | 257 | results = api.search("special topic") 258 | 259 | results[0].text 260 | # 'This is an example tweet with a #hashtag and a link http://foo.com' 261 | 262 | tbu.helpers.remove_entity(results[0], 'hashtags') 263 | # 'This is an example tweet with a and a link http://foo.com' 264 | 265 | tbu.helpers.remove_entity(results[0], 'urls') 266 | # 'This is an example tweet with a #hashtag and a link ' 267 | 268 | # Remove multiple entities with remove_entities. 269 | tbu.helpers.remove_entities(results[0], ['urls', 'hashtags', 'media']) 270 | # 'This is an example tweet with a and a link ' 271 | ```` 272 | 273 | ### Command line utilities 274 | 275 | Twitter bot utils includes a command line tool with a few useful subcommands: 276 | 277 | * `tbu auth`: Authenticate and account with a Twitter app. 278 | * `tbu follow`: Follow accounts that follow your bot 279 | * `tbu like`: Like (aka favorite) your bot's mentions 280 | * `tbu post`: Basic command line for posting text and images 281 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | --------------------------------------------------------------------------------