├── .gitignore ├── LICENSE ├── README.md ├── config.py ├── in-action.gif ├── poller.py ├── posprint.py └── requirements.txt /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | pip-wheel-metadata/ 24 | share/python-wheels/ 25 | *.egg-info/ 26 | .installed.cfg 27 | *.egg 28 | MANIFEST 29 | 30 | # PyInstaller 31 | # Usually these files are written by a python script from a template 32 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 33 | *.manifest 34 | *.spec 35 | 36 | # Installer logs 37 | pip-log.txt 38 | pip-delete-this-directory.txt 39 | 40 | # Unit test / coverage reports 41 | htmlcov/ 42 | .tox/ 43 | .nox/ 44 | .coverage 45 | .coverage.* 46 | .cache 47 | nosetests.xml 48 | coverage.xml 49 | *.cover 50 | *.py,cover 51 | .hypothesis/ 52 | .pytest_cache/ 53 | 54 | # Translations 55 | *.mo 56 | *.pot 57 | 58 | # Django stuff: 59 | *.log 60 | local_settings.py 61 | db.sqlite3 62 | db.sqlite3-journal 63 | 64 | # Flask stuff: 65 | instance/ 66 | .webassets-cache 67 | 68 | # Scrapy stuff: 69 | .scrapy 70 | 71 | # Sphinx documentation 72 | docs/_build/ 73 | 74 | # PyBuilder 75 | target/ 76 | 77 | # Jupyter Notebook 78 | .ipynb_checkpoints 79 | 80 | # IPython 81 | profile_default/ 82 | ipython_config.py 83 | 84 | # pyenv 85 | .python-version 86 | 87 | # pipenv 88 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 89 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 90 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 91 | # install all needed dependencies. 92 | #Pipfile.lock 93 | 94 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow 95 | __pypackages__/ 96 | 97 | # Celery stuff 98 | celerybeat-schedule 99 | celerybeat.pid 100 | 101 | # SageMath parsed files 102 | *.sage.py 103 | 104 | # Environments 105 | .env 106 | .venv 107 | env/ 108 | venv/ 109 | ENV/ 110 | env.bak/ 111 | venv.bak/ 112 | 113 | # Spyder project settings 114 | .spyderproject 115 | .spyproject 116 | 117 | # Rope project settings 118 | .ropeproject 119 | 120 | # mkdocs documentation 121 | /site 122 | 123 | # mypy 124 | .mypy_cache/ 125 | .dmypy.json 126 | dmypy.json 127 | 128 | # Pyre type checker 129 | .pyre/ 130 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Andrew Healey 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Twitter Ticker Tape 2 | 3 | Screens tend to keep me awake in the evening so I wondered if I could print out my Twitter home timeline, live, as I read a book in the evening. 4 | 5 | Here’s the final project in action: 6 | 7 | ![A POS58 receipt printer printing out textual tweets](https://github.com/healeycodes/twitter-ticker-tape/blob/master/in-action.gif) 8 | 9 | See my [**blog post**](https://healeycodes.com/twitter-ticker-tape/) for more extensive documentation and a beginner-friendly explanation of the project. 10 | 11 |
12 | 13 | ### Install 14 | 15 | _Tested with: Python 3.5.3, Raspbian GNU/Linux 9 (stretch)_ 16 | 17 | `pip3 install -r requirements.txt` 18 | 19 |
20 | 21 | ### Setup 22 | 23 | Set your Twitter API keys inside `config.py` or export them as environmental values. 24 | 25 | - `CONSUMER_KEY` 26 | - `CONSUMER_SECRET` 27 | - `ACCESS_TOKEN` 28 | - `ACCESS_TOKEN_SECRET` 29 | 30 | If you're using a different printer to POS58 then export the vendor id and product id as environmental values. 31 | 32 | - `ID_VENDOR` 33 | - `ID_PRODUCT` 34 | 35 |
36 | 37 | ### Run 38 | 39 | `python3 poller.py` 40 | 41 |
42 | 43 | ### License 44 | 45 | MIT. 46 | -------------------------------------------------------------------------------- /config.py: -------------------------------------------------------------------------------- 1 | import os 2 | 3 | consumer_key = os.getenv('CONSUMER_KEY', 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx') 4 | consumer_secret = os.getenv('CONSUMER_SECRET', 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx') 5 | access_token = os.getenv('ACCESS_TOKEN', 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx') 6 | access_token_secret = os.getenv('ACCESS_TOKEN_SECRET', 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx') 7 | -------------------------------------------------------------------------------- /in-action.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/healeycodes/twitter-ticker-tape/83cc782bc2c58f92163fe5b6ca178fee7678c062/in-action.gif -------------------------------------------------------------------------------- /poller.py: -------------------------------------------------------------------------------- 1 | import tweepy 2 | from emoji import demojize 3 | from time import sleep 4 | from collections import OrderedDict 5 | 6 | import config 7 | import posprint 8 | 9 | auth = tweepy.OAuthHandler( 10 | config.consumer_key, 11 | config.consumer_secret 12 | ) 13 | auth.set_access_token( 14 | config.access_token, 15 | config.access_token_secret 16 | ) 17 | 18 | api = tweepy.API(auth) 19 | 20 | # we use an ordered dictionary so we can remove old tweets 21 | # from the data structure. Our program may be long-running 22 | # and we don't want to run out of memory 23 | seen_statuses = OrderedDict() 24 | 25 | while True: 26 | public_tweets = api.home_timeline(tweet_mode='extended') 27 | for status in public_tweets: 28 | 29 | # we're interested in statuses we haven't seen before 30 | if status.id not in seen_statuses: 31 | 32 | # store the statuses we use 33 | seen_statuses[status.id] = status 34 | 35 | # dump old statuses that can't appear again 36 | if len(seen_statuses) > 20: 37 | seen_statuses.popitem() 38 | 39 | user = status.user.screen_name + ':\n' 40 | 41 | # get the full tweet text regardless of type 42 | # https://github.com/tweepy/tweepy/issues/935 43 | if hasattr(status, 'retweeted_status'): 44 | tweet = user + status.retweeted_status.full_text 45 | else: 46 | tweet = user + status.full_text 47 | 48 | # trim unprintable characters that POS58 can't output 49 | # by turning emojis into textual representation. 50 | # 😅 becomes :grinning_face_with_sweat: 51 | tweet = demojize(tweet).encode( 52 | encoding='ascii', 53 | # throwaway non ascii characters 54 | errors='ignore' 55 | ).decode() 56 | 57 | posprint.output(tweet) 58 | 59 | # twitter restricts statuses/home_timeline to once per minute 60 | sleep(65) 61 | -------------------------------------------------------------------------------- /posprint.py: -------------------------------------------------------------------------------- 1 | import os 2 | import usb.core 3 | import usb.util 4 | import textwrap 5 | 6 | """Demo program to print to the POS58 USB thermal receipt printer. This is 7 | labeled under different companies, but is made by Zijiang. See 8 | http://zijiang.com 9 | 10 | MIT License — Copyright (c) 2019 Vince Patron 11 | """ 12 | 13 | # In Linux, you must: 14 | # 15 | # 1) Add your user to the Linux group "lp" (line printer), otherwise you will 16 | # get a user permissions error when trying to print. 17 | # 18 | # 2) Add a udev rule to allow all users to use this USB device, otherwise you 19 | # will get a permissions error also. Example: 20 | # 21 | # In /etc/udev/rules.d create a file ending in .rules, such as 22 | # 33-receipt-printer.rules with the contents: 23 | # 24 | # # Set permissions to let anyone use the thermal receipt printer 25 | # SUBSYSTEM=="usb", ATTR{idVendor}=="0416", ATTR{idProduct}=="5011", MODE="666" 26 | 27 | def output(data): 28 | # find our device 29 | # 0416:5011 is POS58 USB thermal receipt printer 30 | ID_VENDOR = os.getenv('ID_VENDOR', 0x0416) 31 | ID_PRODUCT = os.getenv('ID_VENDOR', 0x5011) 32 | dev = usb.core.find(idVendor=ID_VENDOR, idProduct=ID_PRODUCT) 33 | 34 | # was it found? 35 | if dev is None: 36 | raise ValueError('Device not found') 37 | 38 | # disconnect it from kernel 39 | needs_reattach = False 40 | if dev.is_kernel_driver_active(0): 41 | needs_reattach = True 42 | dev.detach_kernel_driver(0) 43 | 44 | # set the active configuration. With no arguments, the first 45 | # configuration will be the active one 46 | dev.set_configuration() 47 | 48 | # get an endpoint instance 49 | cfg = dev.get_active_configuration() 50 | intf = cfg[(0,0)] 51 | 52 | ep = usb.util.find_descriptor( 53 | intf, 54 | # match the first OUT endpoint 55 | custom_match = \ 56 | lambda e: \ 57 | usb.util.endpoint_direction(e.bEndpointAddress) == \ 58 | usb.util.ENDPOINT_OUT) 59 | 60 | assert ep is not None 61 | 62 | # print! 63 | lines = textwrap.wrap(data, width=30) 64 | for line in lines: 65 | ep.write(line + '\n') 66 | ep.write('\n\n\n\n') 67 | 68 | # reattach if it was attached originally 69 | dev.reset() 70 | if needs_reattach: 71 | dev.attach_kernel_driver(0) 72 | print('Reattached USB device to kernel driver') 73 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | pyusb==1.0.2 2 | tweepy==3.8.0 3 | emoji==0.5.4 4 | 5 | --------------------------------------------------------------------------------