add tweet link web endpoint

This commit is contained in:
2022-06-07 15:47:05 -05:00
parent 475ef1c7bf
commit 46d0b2009e
3 changed files with 51 additions and 6 deletions

View File

@@ -126,3 +126,19 @@ class WordleDb:
with db.atomic():
for score in scores:
score.save()
def get_users_without_score(self, round_no, hole_no, tweetable=True):
hole = self.get_or_create_hole(round_no, hole_no)
query_str = """SELECT username
FROM user_tbl u
WHERE NOT EXISTS (
SELECT FROM score WHERE score.user_id = u.user_id AND score.hole_id = {}
)""".format(
hole.hole_id
)
if tweetable:
query_str += " AND u.check_twitter = true"
res = db.execute_sql(query_str)
return [r[0] for r in res]

View File

@@ -196,17 +196,28 @@ class TwitterClient(httpx.AsyncClient):
async def get_wordlegolf_tweets(self):
return self._build_wordle_tweets(await self.search_tweets("#WordleGolf"))
def open_tweet(self, msg):
@classmethod
def open_tweet(cls, msg):
param = urllib.parse.urlencode({"text": msg})
webbrowser.open(f"{self.TWEET_INTENT_URL}?{param}")
webbrowser.open(f"{cls.TWEET_INTENT_URL}?{param}")
async def notify_missing(self, names):
@classmethod
def full_notify_link(cls, names):
header = "Still missing a few #WordleGolf Players today!"
msg = header
while names:
while len(msg) < self.MAX_TWEET_LENGTH and names:
msg += f" @{names.pop()}"
param = urllib.parse.urlencode({"text": msg})
return f"{cls.TWEET_INTENT_URL}?{param}"
@classmethod
async def notify_missing(cls, names):
header = "Still missing a few #WordleGolf Players today!"
msg = header
while names:
while len(msg) < cls.MAX_TWEET_LENGTH and names:
msg += f" @{names.pop()}"
self.open_tweet(msg)
cls.open_tweet(msg)
async def main():

View File

@@ -6,9 +6,12 @@ import time
import dash
import dash.long_callback
import diskcache
import flask
import flask.views
import plotly.graph_objs
import wordlinator.db.pg as db
import wordlinator.twitter
import wordlinator.utils
###################
@@ -348,8 +351,23 @@ def get_stats_graph(_):
server = app.server
class GetLinkView(flask.views.View):
methods = ["GET"]
def dispatch_request(self):
today = wordle_today()
missing_users = db.WordleDb().get_users_without_score(
today.golf_hole.game_no, today.golf_hole.hole_no
)
link = wordlinator.twitter.TwitterClient.full_notify_link(missing_users)
return flask.redirect(link)
server.add_url_rule("/tweet_link", view_func=GetLinkView.as_view("tweet_link"))
def serve(debug=True):
app.run_server(debug=debug)
app.run(debug=debug)
if __name__ == "__main__":