Skip to content

Commit

Permalink
Merge pull request #4 from ryandeering/feature/3-bluesky-support
Browse files Browse the repository at this point in the history
Bluesky support + package updates
  • Loading branch information
ryandeering authored Oct 21, 2024
2 parents 561e0ae + 2e10a34 commit 8a7afea
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 27 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pylint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.9", "3.10", "3.11"]
python-version: ["3.11", "3.12"]
steps:
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
Expand Down
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
# [Dublin Airport Security Times](https://twitter.com/DASecurityTimes)
# Dublin Airport Security Times

[![linting: pylint](https://img.shields.io/badge/linting-pylint-yellowgreen)](https://github.com/ryandeering/DASecurityTimes/actions)

A project born from frustration with Dublin Airport bottlenecking in the summer of 2022, I created a public Twitter bot so that people can easily assess how long they should give themselves to get through airport security, as well as to give an idea of how busy the airport is.

I am not naturally a Python developer, only using the language really for little scripts, data analytics and working with Tensorflow. But certain libraries like Tweepy, BeautifulSoup and Matplotlib seemed like a logical choice for using it with this project. It was also a great excuse to get more familiar with it from a networking and I/O point of view. So, if you see anything you might consider not too 'Pythonic' or just bad practice, let me know!

The project is set up to post on Twitter every 30 minutes, inserting into an InfluxDB measurement everytime it does so. It also posts a chart using Matplotlib twice a day, at midnight and midday, of the previous 24 hours of activity. This can be seen below.
The project is set up to post on Twitter every 30 minutes, inserting into an InfluxDB measurement everytime it does so. It also posts a chart using Matplotlib twice a day, at midnight and midday, of the previous 24 hours of activity. It has now has a BlueSky version. This can be seen below.

The bot accumulated 1,000 followers in it's first week of existence!

[Link to Bot](https://twitter.com/DASecurityTimes)
- [Twitter](https://twitter.com/DASecurityTimes)
- [BlueSky](https://bsky.app/profile/dasecuritytimes.bsky.social)

<h1> Built With </h1>

Expand All @@ -19,5 +20,6 @@ The bot accumulated 1,000 followers in it's first week of existence!
* Numpy, Pandas, Matplotlib, Seaborn - For data analytics and creation of chart
* [Tweepy](https://www.tweepy.org/) - Python library to call Twitter API
* [Pillow](https://pillow.readthedocs.io/en/stable/) - Python image library
* [ATProto](https://atproto.blue/en/latest/) - ATProto SDK, underlying decentralised network behind BlueSky

![image](https://user-images.githubusercontent.com/37181720/174308996-703472b3-58af-4412-9573-d0c0225dc62e.png)
4 changes: 4 additions & 0 deletions credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
- CONSUMER_SECRET: Twitter API consumer secret for authentication.
- ACCESS_TOKEN: Twitter API access token for authentication.
- ACCESS_TOKEN_SECRET: Twitter API access token secret for authentication.
- BLUESKY_USERNAME: BlueSky username for authentication.
- BLUESKY_PASSWORD: BlueSKy password for authentication.
- INFLUXDB_URL: URL of the InfluxDB instance.
- INFLUXDB_TOKEN: Authentication token for InfluxDB.
- INFLUXDB_ORG: InfluxDB organization name.
Expand All @@ -14,6 +16,8 @@
CONSUMER_SECRET = ""
ACCESS_TOKEN = ""
ACCESS_TOKEN_SECRET = ""
BLUESKY_USERNAME=""
BLUESKY_PASSWORD=""
INFLUXDB_URL = ""
INFLUXDB_TOKEN = ""
INFLUXDB_ORG = ""
Expand Down
64 changes: 48 additions & 16 deletions daabot.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
"""
This script automates the process of tweeting Dublin Airport security times
and logs them in an InfluxDB database.
This script automates the process of posting Dublin Airport security times
to Twitter and Bluesky + logs them in an InfluxDB database.
Written and maintained by Ryan Deering (@ryandeering)
2022-2024
"""

import calendar
Expand All @@ -14,12 +15,15 @@
from bs4 import BeautifulSoup
from influxdb_client import InfluxDBClient, Point
from influxdb_client.client.write_api import SYNCHRONOUS
from atproto import Client as BlueskyClient, exceptions as atproto_exceptions

from credentials import (
CONSUMER_KEY,
CONSUMER_SECRET,
ACCESS_TOKEN,
ACCESS_TOKEN_SECRET,
BLUESKY_USERNAME,
BLUESKY_PASSWORD,
INFLUXDB_URL,
INFLUXDB_BUCKET,
INFLUXDB_ORG,
Expand All @@ -30,16 +34,25 @@

def init_twitter_api():
"""
Initialize and return a Tweepy API client using the provided credentials.
Initialise and return a Tweepy API client using the provided credentials.
"""
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
return tweepy.API(auth)


def init_influxdb_client():
def init_bluesky_api():
"""
Initialize and return an InfluxDB client with the provided credentials.
Initialise and return a Bluesky API client using the provided credentials.
"""
client = BlueskyClient()
client.login(BLUESKY_USERNAME, BLUESKY_PASSWORD)
return client


def init_influxdb_api():
"""
Initialise and return an InfluxDB client with the provided credentials.
"""
return InfluxDBClient(url=INFLUXDB_URL, token=INFLUXDB_TOKEN, org=INFLUXDB_ORG)

Expand Down Expand Up @@ -68,23 +81,23 @@ def write_to_influxdb(influx_client, t1_minutes, t2_minutes):
write_api.write(bucket=INFLUXDB_BUCKET, record=point)


def generate_tweet(today, t1_minutes, t2_minutes):
def generate_post(today, t1_minutes, t2_minutes):
"""
Generate a tweet with security times information.
Generate a post with security times information.
"""
date_time = today.strftime("%d/%m/%Y %H:%M")
tweet = (
post = (
f"Current times - {calendar.day_name[today.weekday()]}, {date_time} \n"
f"Terminal 1: {t1_minutes} minutes\nTerminal 2: {t2_minutes} minutes"
)

if t1_minutes == "60" or t2_minutes == "60":
tweet += (
post += (
"\n\nWARNING: Allow yourself extra time to get through security. "
"A terminal is peaking."
)

return tweet
return post


def plot_and_upload():
Expand Down Expand Up @@ -126,23 +139,42 @@ def upload_tweet(api, text, image_buffer=None):
print("An error occurred while posting the tweet:", tweep_error)


def upload_bluesky_post(client, text, image_buffer=None):
"""
Upload a post to Bluesky
"""
try:
if image_buffer:
image_buffer.seek(0)
client.send_image(
text=text, image=image_buffer, image_alt="Dublin Airport security times"
)
else:
client.send_post(text=text)
except atproto_exceptions.AtProtocolError as error:
print(f"An error occurred while posting to Bluesky: {error}")


def main():
"""
Main function to fetch security times, generate a tweet, and upload it.
Main function to fetch security times, generate a post, and upload it.
"""
api = init_twitter_api()
influx_client = init_influxdb_client()
twitter_api = init_twitter_api()
bluesky_api = init_bluesky_api()
influx_api = init_influxdb_api()
t1_minutes, t2_minutes = get_airport_times()

today = datetime.now()
tweet_text = generate_tweet(today, t1_minutes, t2_minutes)
post_text = generate_post(today, t1_minutes, t2_minutes)

if t1_minutes is not None and t2_minutes is not None:
write_to_influxdb(influx_client, t1_minutes, t2_minutes)
write_to_influxdb(influx_api, t1_minutes, t2_minutes)

plot_media = plot_and_upload()

upload_tweet(api, tweet_text, plot_media)
upload_tweet(twitter_api, post_text, plot_media)

upload_bluesky_post(bluesky_api, post_text, plot_media)


if __name__ == "__main__":
Expand Down
15 changes: 8 additions & 7 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
beautifulsoup4==4.12.2
influxdb_client==1.39.0
matplotlib==3.8.2
numpy==1.26.3
pandas==2.1.4
beautifulsoup4==4.12.3
influxdb_client==1.46.0
matplotlib==3.9.2
numpy==2.1.2
pandas==2.2.3
Pillow==10.2.0
Requests==2.31.0
seaborn==0.13.1
tweepy==4.14.0
seaborn==0.13.2
tweepy==4.14.0
atproto==0.0.54

0 comments on commit 8a7afea

Please sign in to comment.