Skip to content

Commit fc2e577

Browse files
committed
fix #1, thank you kawarimidoll!
1 parent 5e835bb commit fc2e577

File tree

2 files changed

+60
-46
lines changed

2 files changed

+60
-46
lines changed

main.py

+53-42
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,17 @@
1212
# lets aiohttp
1313
# i wrote one recently.....
1414

15+
# lol jk aiohttp is more finicky than flask despite being more performant
16+
# throws a 400 error, seems like you need another server/wsgi in front of it to validate correctly formatted html
17+
# but my requests are relatively benign and it's still failing
18+
1519
creds = json.load(open("credentials.json"))
1620
USERNAME = creds.get("USERNAME")
1721
APP_PASSWORD = creds.get("APP_PASSWORD")
1822

1923
'''
2024
GET / HTTP/1.1
2125
user-agent: Mozilla/5.0 (compatible; Discordbot/2.0; +https://discordapp.com)
22-
host: 5.42.201.69:8082
2326
'''
2427

2528
discord_preview_for_video_tweet = '''
@@ -97,11 +100,54 @@
97100
cc @mr_ligi @coderobe
98101
"""
99102

103+
def generate_html(full_path):
104+
# path = /profile/klatz.co/post/3jua5rlgrq42p
105+
# or
106+
# path = /profile/DID/post/3jua5rlgrq42p
107+
108+
session = atprototools.Session(USERNAME,APP_PASSWORD)
109+
post_content = session.get_bloot_by_url(full_path).json()
110+
111+
post_content = post_content.get("posts")[0]
112+
113+
author = post_content.get("author")
114+
img_url=""
115+
try:
116+
img_url = post_content.get("embed").get("images")[0].get("fullsize")
117+
except:
118+
pass
119+
record = post_content.get("record")
120+
text = record.get("text")
121+
122+
html = f"""
123+
<html lang="en">
124+
<head>
125+
126+
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type" />
127+
<meta content="#7FFFD4" name="theme-color" />
128+
<meta property="og:site_name" content="psky.app" />
129+
130+
<meta name="twitter:card" content="summary_large_image" />
131+
<meta name="twitter:title" content="{author.get("displayName")} (@{author.get("handle")}) " />
132+
<meta name="twitter:image" content="{img_url}" />
133+
<meta name="twitter:creator" content="@{author.get("displayName")}" />
134+
135+
<meta property="og:description" content="{text}" />
136+
137+
<!-- TODO what on earth is this -->
138+
<!-- <link rel="alternate" href="https://vxtwitter.com/oembed.json?desc=Ian%20Klatzco&user=Twitter&link=https%3A//twitter.com/ian5v&ttype=photo" type="application/json+oembed" title="Ian Klatzco"> -->
139+
<meta http-equiv="refresh" content="0; url = {post_url}" />
140+
</head>
141+
<body>
142+
Redirecting you to the tweet in a moment. <a href="{post_url}">Or click here.</a>
143+
</body>
144+
"""
145+
return html
146+
100147

101148
@app.route("/", defaults={'path': ''})
102149
@app.route("/<path:path>")
103150
def index(path):
104-
session = atprototools.Session(USERNAME,APP_PASSWORD)
105151

106152
print(request.url)
107153

@@ -120,49 +166,14 @@ def index(path):
120166
# request comes in with a url
121167
# parse the URL, fetch the corresponding content from bsky, return it formatted
122168

123-
# what is the aiohttp for the url?
169+
print("PATH: ", end='')
170+
print(path)
171+
print("request.path: ", end='')
124172
print(request.path)
125-
bsky_url = "https://bsky.app" + str(request.path)
126-
post_content = session.get_skoot_by_url(bsky_url).json()
127-
post_content = post_content.get("thread").get("post")
128-
129-
author = post_content.get("author")
130-
img_url=""
131-
try:
132-
img_url = post_content.get("embed").get("images")[0].get("fullsize")
133-
except:
134-
pass
135-
record = post_content.get("record")
136-
text = record.get("text")
137-
138-
html = f"""
139-
<html lang="en">
140-
<head>
141-
142-
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type" />
143-
<meta content="#7FFFD4" name="theme-color" />
144-
<meta property="og:site_name" content="psky.app" />
145-
146-
<meta name="twitter:card" content="summary_large_image" />
147-
<meta name="twitter:title" content="{author.get("displayName")} (@{author.get("handle")}) " />
148-
<meta name="twitter:image" content="{img_url}" />
149-
<meta name="twitter:creator" content="@{author.get("displayName")}" />
150-
151-
<meta property="og:description" content="{text}" />
152-
153-
<!-- TODO what on earth is this -->
154-
<!-- <link rel="alternate" href="https://vxtwitter.com/oembed.json?desc=Ian%20Klatzco&user=Twitter&link=https%3A//twitter.com/ian5v&ttype=photo" type="application/json+oembed" title="Ian Klatzco"> -->
155-
<meta http-equiv="refresh" content="0; url = {post_url}" />
156-
</head>
157-
<body>
158-
Redirecting you to the tweet in a moment. <a href="{post_url}">Or click here.</a>
159-
</body>
160-
"""
161-
162-
# import pdb; pdb.set_trace()
163173

174+
html = generate_html("https://bsky.app" + path)
175+
164176
return html
165-
# return web.Response(text=discord_preview_for_that, content_type="text/html")
166177

167178
if __name__ == "__main__":
168179
app.run(port=8081)

tests/__init__.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import unittest
22
import requests
33

4+
from main import *
5+
46
class TestFoo(unittest.TestCase):
57
def test_is_up(self):
68
# how should this work?
@@ -9,7 +11,8 @@ def test_is_up(self):
911
# self.assertIn()
1012
assert requests.get("https://psky.app/").status_code == 200
1113
assert requests.get("https://staging.psky.app/").status_code == 200
12-
13-
def test_returns_correct_html(self):
14-
requests.get("")
15-
pass
14+
15+
def test_url_from_kawarimidoll(self):
16+
url_with_did = "https://bsky.app/profile/did:plc:okalufxun5rpqzdrwf5bpu3d/post/3juagfzruqs2u"
17+
generate_html(url_with_did)
18+
# fails if error

0 commit comments

Comments
 (0)