Siddhant commited on
Commit
8073ee5
·
unverified ·
1 Parent(s): 01a5bd2

recent posts service added

Browse files
Files changed (2) hide show
  1. main.py +13 -1
  2. utils/GetTopAndRecentQuestions.py +40 -1
main.py CHANGED
@@ -3,7 +3,7 @@ from typing import Annotated
3
  import os
4
  from fastapi.middleware.cors import CORSMiddleware
5
  from dotenv import load_dotenv
6
- from utils.GetTopAndRecentQuestions import return_top_question
7
 
8
  load_dotenv()
9
 
@@ -33,3 +33,15 @@ def get_top_questions_(limit: int = 5, Authorization: Annotated[list[str] | None
33
  except Exception as e:
34
  # return a success message
35
  raise HTTPException(status_code=400, detail=str(e))
 
 
 
 
 
 
 
 
 
 
 
 
 
3
  import os
4
  from fastapi.middleware.cors import CORSMiddleware
5
  from dotenv import load_dotenv
6
+ from utils.GetTopAndRecentQuestions import return_top_question, return_recent_posts
7
 
8
  load_dotenv()
9
 
 
33
  except Exception as e:
34
  # return a success message
35
  raise HTTPException(status_code=400, detail=str(e))
36
+
37
+
38
+ @app.get("/get_recent_posts", status_code=status.HTTP_200_OK)
39
+ def get_recent_posts_(limit: int = 5, strategy: str = 'recent',
40
+ Authorization: Annotated[list[str] | None, Header()] = None):
41
+ if Authorization is None or Authorization[0] != "Bearer {}".format(token):
42
+ raise HTTPException(status_code=401, detail="Unauthorised.")
43
+ try:
44
+ return {'data': return_recent_posts(limit, strategy)}
45
+ except Exception as e:
46
+ # return a success message
47
+ raise HTTPException(status_code=400, detail=str(e))
utils/GetTopAndRecentQuestions.py CHANGED
@@ -134,4 +134,43 @@ def return_top_question(limit=5):
134
 
135
  return message
136
 
137
- # print(return_top_question())
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
134
 
135
  return message
136
 
137
+
138
+ def return_recent_posts(limit=5, strategy='recent'):
139
+ import os
140
+ import requests
141
+ import json
142
+
143
+ HASURA_URL = os.environ['HASURA_URL']
144
+ HASURA_ADMIN_SECRET = os.environ['HASURA_ADMIN_SECRET']
145
+
146
+ url = HASURA_URL
147
+ body = """query homeFeedQuery($strategy: Strategy, $limit: Int){
148
+ feed(strategy: $strategy, limit: $limit) {
149
+ hits {
150
+ link
151
+ title
152
+ date
153
+ author
154
+ }
155
+ }
156
+ }"""
157
+ variables = {'strategy': strategy, 'limit': limit}
158
+ response = requests.post(url=url, json={'query': body, 'variables': variables}, headers={
159
+ 'x-hasura-admin-secret': HASURA_ADMIN_SECRET})
160
+
161
+ message = 'These are the recent Articles/Posts on the platform/twimbit website: \n'
162
+
163
+ if response.status_code == 200:
164
+ data = json.loads(response.content)
165
+ posts = data.get('data').get('feed').get('hits')
166
+
167
+ for key, post in enumerate(posts):
168
+ title = post.get('title')
169
+ link = post.get('link')
170
+ date = post.get('date')
171
+ authors = ','.join(post.get('author'))
172
+ message += 'Post/Article {}:- \n\tPost/Article Title:- {}\n\tPost/Article Link/URL:- {}\n\tPost/Article Publish Date:- {}\n\tPost/Article Author:- {}\n'.format(
173
+ key + 1, title, link, date, authors)
174
+
175
+ return message
176
+