forked from mouredev/roadmap-retos-programacion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mouredev.py
185 lines (135 loc) · 5.23 KB
/
mouredev.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
from datetime import datetime
class SocialNetwork:
def __init__(self):
self.post_id = 0
self.users = {}
self.post = {}
def register_user(self, user_id: str, name: str):
if user_id in self.users:
print(f"El usuario con el ID {user_id} ya existe.")
return
self.users[user_id] = {
"name": name,
"following": set(),
"followers": set(),
"post": []
}
print(
f"Usuario con ID '{user_id}' y nombre '{
name}' registrado correctamente."
)
def follow_user(self, user_id: str, follow_id: str):
if user_id not in self.users or follow_id not in self.users:
print("Alguno de los usuarios no existe. No se puede realizar el follow.")
return
self.users[user_id]["following"].add(follow_id)
self.users[follow_id]["followers"].add(user_id)
print(
f"{self.users[user_id]["name"]} ahora sigue a {
self.users[follow_id]["name"]}."
)
def unfollow_user(self, user_id: str, unfollow_id: str):
if user_id not in self.users or unfollow_id not in self.users:
print("Alguno de los usuarios no existe. No se puede realizar el unfollow.")
return
self.users[user_id]["following"].discard(unfollow_id)
self.users[unfollow_id]["followers"].discard(user_id)
print(
f"{self.users[user_id]["name"]} ha dejado de seguir a {
self.users[unfollow_id]["name"]}."
)
def create_post(self, user_id: str, text: str):
if user_id not in self.users:
print(f"El usuario '{user_id}' no existe.")
return
if len(text) > 200:
print("El post no puede tener más de 200 caracteres.")
return
self.post_id += 1
post_id = self.post_id
self.post[post_id] = {
"user_id": user_id,
"text": text,
"created_at": datetime.now(),
"likes": set()
}
self.users[user_id]["post"].append(post_id)
print("Post creado correctamente.")
def delete_post(self, post_id: int):
if post_id not in self.post:
print(f"El post con ID {post_id} no existe.")
return
user_id = self.post[post_id]["user_id"]
self.users[user_id]["post"].remove(post_id)
del self.post[post_id]
print("Post eliminado correctamente.")
def like_post(self, user_id: str, post_id: int):
if user_id not in self.users:
print(f"El usuario '{user_id}' no existe.")
return
if post_id not in self.post:
print(f"El post '{post_id}' no existe.")
return
self.post[post_id]["likes"].add(user_id)
print(f"Nuevo like en post '{post_id}'.")
def unlike_post(self, user_id: str, post_id: int):
if user_id not in self.users:
print(f"El usuario '{user_id}' no existe.")
return
if post_id not in self.post:
print(f"El post '{post_id}' no existe.")
return
self.post[post_id]["likes"].discard(user_id)
print(f"Like eliminado en post '{post_id}'.")
def view_user_feed(self, user_id: str):
if user_id not in self.users:
print(f"El usuario '{user_id}' no existe.")
return
feed = sorted(
(self.post[post_id] for post_id in self.users[user_id]["post"]),
key=lambda x: x["created_at"],
reverse=True
)[:10]
for post in feed:
print(f"""
ID usuario: {post["user_id"]}
Usuario: {self.users[post["user_id"]]["name"]}
Texto: {post["text"]}
Fecha: {post["created_at"]}
Likes: {len(post["likes"])}
""")
def view_following_feed(self, user_id: str):
if user_id not in self.users:
print(f"El usuario '{user_id}' no existe.")
return
following_post_ids = []
for following_id in self.users[user_id]["following"]:
following_post_ids.extend(self.users[following_id]["post"])
feed = sorted(
(self.post[post_id] for post_id in following_post_ids),
key=lambda x: x["created_at"],
reverse=True
)[:10]
for post in feed:
print(f"""
ID usuario: {post["user_id"]}
Usuario: {self.users[post["user_id"]]["name"]}
Texto: {post["text"]}
Fecha: {post["created_at"]}
Likes: {len(post["likes"])}
""")
social_network = SocialNetwork()
social_network.register_user("mouredev", "Brais Moure")
social_network.register_user("manolo", "Manolo 87")
social_network.create_post("mouredev", "Hola mundo!")
social_network.create_post("mouredev", "Hola mundo 2!")
social_network.create_post("mouredev", "Hola mundo 3!")
social_network.create_post("manolo", "Hola mundo!")
social_network.create_post("manolo", "Hola mundo 2!")
social_network.create_post("manolo", "Hola mundo 3!")
social_network.follow_user("manolo", "mouredev")
social_network.like_post("manolo", 1)
social_network.view_user_feed("mouredev")
social_network.view_following_feed("manolo")
social_network.unlike_post("manolo", 1)
social_network.view_following_feed("manolo")