-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathdatabase.py
More file actions
62 lines (47 loc) · 1.61 KB
/
Copy pathdatabase.py
File metadata and controls
62 lines (47 loc) · 1.61 KB
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
import redis.asyncio as Redis
from os import getenv
from typing import Union
from pydantic import BaseModel
# Define Redis connection.
con = Redis.from_url(str(getenv("REDIS_URL_POST")))
# Define the prefix for the keys.
prefix = "hn:"
# Define the model for the post.
class Post(BaseModel):
id: str
title: str
url: str
score: int
time: int
comments: int
author: str
distance: Union[float, None] = None
test = Post(id="1", title="test", url="https://example.com",
score=1, time=1, comments=1, author="test")
async def get_posts_from_database(ids: list[str]) -> list[Post]:
"""
Fetch the posts from the database using a Redis pipeline.
Args:
ids (list[str]): A list of IDs of the posts to fetch.
Returns:
list[Post]: The list of posts.
"""
# We create a pipeline.
pipeline = con.pipeline()
for id in ids:
pipeline.hmget(prefix + id, "title", "url",
"score", "time", "comments", "by")
# We execute the pipeline.
results = await pipeline.execute()
# We create a list of posts.
posts = []
for i in range(len(results)):
try:
post = Post(id=ids[i], title=results[i][0].decode("utf-8"), url=results[i][1].decode("utf-8"),
score=int(results[i][2]), time=int(results[i][3]), comments=int(results[i][4]), author=results[i][5].decode("utf-8"))
posts.append(post)
except:
print("Error while parsing the post with ID: {}".format(ids[i]))
print("The post is: {}".format(results[i]))
continue
return posts