Skip to content

Commit ab3c8d6

Browse files
committed
Add readme, gitignore and get recent posts
1 parent 1408551 commit ab3c8d6

File tree

3 files changed

+57
-5
lines changed

3 files changed

+57
-5
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.env

main.py

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,19 @@
77
from rich.console import Console
88
from rich.table import Table
99
from datetime import datetime, timedelta, timezone
10+
from dotenv import load_dotenv
1011

1112
app = typer.Typer()
1213
console = Console()
14+
load_dotenv() # load environment variables from .env file
1315

14-
ACCESS_TOKEN = ""
16+
ACCESS_TOKEN = os.getenv("ACCESS_TOKEN")
1517
BASE_URL = "https://graph.threads.net/v1.0"
16-
1718
HEADERS = {
1819
'Authorization': f'Bearer {ACCESS_TOKEN}'
1920
}
2021
DRAFTS_FILE = 'drafts.json'
22+
SERVER_PROCESS_TIME = 10
2123

2224
def get_user_id():
2325
response = requests.get(f"{BASE_URL}/me?fields=id", headers=HEADERS)
@@ -227,7 +229,7 @@ def create_text_post(text: str):
227229
container_id = response.json()['id']
228230

229231
# Wait for the server to process the upload
230-
time.sleep(30)
232+
time.sleep(SERVER_PROCESS_TIME)
231233

232234
publish_payload = {
233235
"creation_id": container_id
@@ -253,7 +255,7 @@ def create_image_post(text: str, image_url: str):
253255
container_id = response.json()['id']
254256

255257
# Wait for the server to process the upload
256-
time.sleep(30)
258+
time.sleep(SERVER_PROCESS_TIME)
257259

258260
publish_payload = {
259261
"creation_id": container_id
@@ -309,7 +311,7 @@ def send_reply(media_id: str, text: str):
309311
container_id = response.json()['id']
310312

311313
# Wait for the server to process the upload
312-
time.sleep(30)
314+
time.sleep(SERVER_PROCESS_TIME)
313315

314316
publish_payload = {
315317
"creation_id": container_id
@@ -420,6 +422,52 @@ def send_draft(draft_id: int):
420422

421423
typer.echo(f"Draft with ID {draft_id} sent and removed from drafts.")
422424

425+
def get_post_replies_count(post_id):
426+
"""
427+
Retrieve the number of replies for a specific post.
428+
"""
429+
response = requests.get(f"{BASE_URL}/{post_id}/replies", headers=HEADERS)
430+
response.raise_for_status()
431+
replies = response.json().get('data', [])
432+
return len(replies)
433+
434+
435+
@app.command()
436+
def get_recent_posts(limit: int = 5):
437+
"""
438+
Retrieve the most recent posts.
439+
"""
440+
user_id = get_user_id()
441+
fields = "id,media_product_type,media_type,media_url,permalink,owner,username,text,timestamp,shortcode,thumbnail_url,children,is_quote_post"
442+
response = requests.get(f"{BASE_URL}/{user_id}/threads?fields={fields}&limit={limit}", headers=HEADERS)
443+
response.raise_for_status()
444+
posts = response.json().get('data', [])
445+
446+
table = Table(title="Recent Posts")
447+
table.add_column("ID", style="cyan", no_wrap=True)
448+
table.add_column("Username", style="cyan", no_wrap=True)
449+
table.add_column("Timestamp", style="magenta")
450+
table.add_column("Type", style="green")
451+
table.add_column("Text", style="yellow")
452+
table.add_column("Permalink", style="blue")
453+
table.add_column("Replies", style="red")
454+
455+
for post in posts:
456+
if post.get('media_type') == 'REPOST_FACADE':
457+
continue
458+
timestamp = convert_to_locale(post.get('timestamp', 'N/A'))
459+
replies_count = get_post_replies_count(post['id'])
460+
table.add_row(
461+
post.get('id', 'N/A'),
462+
post.get('username', 'N/A'),
463+
timestamp,
464+
post.get('media_type', 'N/A'),
465+
post.get('text', 'N/A'),
466+
post.get('permalink', 'N/A'),
467+
str(replies_count)
468+
)
469+
470+
console.print(table)
423471

424472
if __name__ == "__main__":
425473
app()

requirements.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
typer==0.12.3
2+
rich==13.7.1
3+
python-dotenv==1.0.1

0 commit comments

Comments
 (0)