-
Notifications
You must be signed in to change notification settings - Fork 1
/
nyc_dot_bot.py
248 lines (191 loc) · 6.92 KB
/
nyc_dot_bot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
from enum import Enum, auto
import io
import json
import os
import traceback
from urllib.parse import urljoin
from atproto import Client, client_utils
import click
import boto3
from dotenv import load_dotenv
from bs4 import BeautifulSoup
from mastodon import Mastodon
from pdf2image import convert_from_bytes
import requests
import sentry_sdk
import tweepy
sentry_sdk.init(
traces_sample_rate=1.0,
)
load_dotenv()
current_projects_url = "https://www1.nyc.gov/html/dot/html/about/current-projects.shtml"
bucket_name = (
os.environ.get("BUCKET_NAME") or "nyc-dot-current-projects-bot-mastodon-staging"
)
class Platform(Enum):
MASTODON = auto()
TWITTER = auto()
BLUESKY = auto()
PLATFORM = Platform.MASTODON
if os.environ.get("TWITTER_CONSUMER_KEY"):
PLATFORM = Platform.TWITTER
elif os.environ.get("BLUESKY_USERNAME"):
PLATFORM = Platform.BLUESKY
class TooManyNewPDFsException(Exception):
pass
def get_html():
projects_html = requests.get(current_projects_url)
projects_html.raise_for_status()
projects_html.encoding = "utf-8"
return projects_html
def get_pdf_links(projects_html):
soup = BeautifulSoup(projects_html.text, "html.parser")
content = soup.find(class_="view-content")
links = content.find_all("a")
pdf_links = []
for link in links:
if link["href"].endswith("pdf"):
pdf_links.append(link)
return pdf_links
def get_s3_cache(client, key="cache.json"):
cache = client.get_object(Bucket=bucket_name, Key=key)
return json.loads(cache["Body"].read())
def get_local_cache(file_path):
with open(file_path) as f:
content = f.read()
return json.loads(content)
def get_pdf(link):
r = requests.get(link)
r.raise_for_status()
return r.content
def convert_pdf_to_image(pdf):
buf = io.BytesIO()
image = convert_from_bytes(pdf)[0]
image.thumbnail((2048,2048))
image.save(buf, format="JPEG")
buf.seek(0)
return buf
def find_new_links(cached_links, current_links):
new_links = []
for link in current_links:
link["href"] = urljoin(
current_projects_url,
link["href"],
)
if link["href"] not in cached_links:
new_links.append(link)
# prevent tweeting too many
if len(new_links) > 1500:
raise TooManyNewPDFsException
return new_links
def format_link_for_tweet(link):
max_length = 280 - 23 - 1
link_text = link.text
link_text = link_text.replace(" (pdf)", "")
if len(link_text) >= max_length:
link_text = f"{link_text[max_length-3]}..."
return f"{link_text} {link['href']}"
def truncate_text_for_skeet(link):
max_length = 300 - 1
link_text = link.text
link_text = link_text.replace(" (pdf)", "")
if len(link_text) >= max_length:
link_text = f"{link_text[max_length-3]}..."
return link_text
def tweet_new_links(links, dry_run=False, no_tweet=False):
successes = {}
if PLATFORM is Platform.TWITTER:
auth = tweepy.OAuth1UserHandler(
os.environ.get("TWITTER_CONSUMER_KEY"),
os.environ.get("TWITTER_CONSUMER_SECRET"),
os.environ.get("TWITTER_ACCESS_TOKEN"),
os.environ.get("TWITTER_ACCESS_TOKEN_SECRET"),
)
twitter_client_v1 = tweepy.API(auth)
twitter_client_v2 = tweepy.Client(
consumer_key=os.environ.get("TWITTER_CONSUMER_KEY"),
consumer_secret=os.environ.get("TWITTER_CONSUMER_SECRET"),
access_token=os.environ.get("TWITTER_ACCESS_TOKEN"),
access_token_secret=os.environ.get("TWITTER_ACCESS_TOKEN_SECRET"),
)
elif PLATFORM is Platform.BLUESKY:
bsky_client = Client()
bsky_client.login(os.environ.get("BLUESKY_USERNAME"), os.environ.get("BLUESKY_APP_PASSWORD"))
else:
mastodon_client = Mastodon(
api_base_url=os.environ.get("MASTODON_API_BASE_URL"),
access_token=os.environ.get("MASTODON_ACCESS_TOKEN"),
)
# If any of these fail, we want to record the rest succeeded so
# we don't tweet them again. We still want them to go to sentry though.
for link in links:
try:
# link takes 23 chars and we want a space
tweet_text = format_link_for_tweet(link)
image_buf = convert_pdf_to_image(get_pdf(link["href"]))
if dry_run or no_tweet:
print(f'Would have tweeted: "{tweet_text}"')
else:
if PLATFORM is Platform.TWITTER:
media = twitter_client_v1.media_upload(filename="", file=image_buf)
twitter_client_v2.create_tweet(text=tweet_text, media_ids=[media.media_id])
elif PLATFORM is Platform.BLUESKY:
image = image_buf.read()
bsky_client.send_image(
text=client_utils.TextBuilder().link(
truncate_text_for_skeet(link),
link['href'],
),
image=image,
image_alt="Screenshot of first page of PDF. Auto posted so can't describe, sorry."
)
else:
image = image_buf.read()
mastodon_media = mastodon_client.media_post(
image,
mime_type="image/png",
description="Screenshot of first page of PDF. Auto posted so can't describe, sorry.",
)
mastodon_client.status_post(tweet_text, media_ids=[mastodon_media["id"]])
successes[link["href"]] = link.text
except Exception as e:
sentry_sdk.capture_exception(e)
print(e)
traceback.print_exc()
return successes
def run(event=None, context=None, local_cache=None, dry_run=False, no_tweet=False):
cache = None
client = None
if local_cache:
cache = get_local_cache(local_cache)
else:
client = boto3.client("s3")
cache = get_s3_cache(client)
new_links = find_new_links(cache, get_pdf_links(get_html()))
if not new_links:
return
successes = tweet_new_links(new_links, dry_run, no_tweet)
if dry_run:
return
cache.update(successes)
if local_cache:
with open(local_cache, "w") as f:
f.write(json.dumps(cache))
else:
client.put_object(
Bucket=bucket_name,
Key="cache.json",
Body=json.dumps(cache),
)
def lambda_handler(event=None, context=None):
run()
@click.command()
@click.option("--dry-run", is_flag=True)
@click.option("--no-tweet", is_flag=True, help="Updates the cache without tweeting")
@click.option(
"--local-cache", default=None, type=click.Path(dir_okay=False, writable=True)
)
def cli(dry_run, local_cache, no_tweet):
run(local_cache=local_cache, dry_run=dry_run, no_tweet=no_tweet)
if __name__ == "__main__":
cli()