1
1
from fastapi import FastAPI , HTTPException , Query
2
2
from fastapi .responses import JSONResponse
3
3
from starlette .middleware import Middleware
4
- from middleware import TimeoutMiddleware
4
+ from time_middleware import TimeoutMiddleware
5
5
from typing import Optional
6
+ from pydantic import BaseModel
6
7
7
8
# Import the functions from the other scripts
8
9
from profile_data import retrieve_profile_info
9
10
from user_post import retrieve_post_metrics
10
11
from media import get_post_details
11
12
13
+ # Import the Instagram service
14
+ from instagram_service import instagram_service
15
+ from authentication_middleware import AuthenticationMiddleware
16
+
17
+ class Credentials (BaseModel ):
18
+ username : str
19
+ password : str
20
+
12
21
app = FastAPI (middleware = [
13
- Middleware (TimeoutMiddleware , timeout = 600 )
22
+ Middleware (TimeoutMiddleware , timeout = 600 ),
23
+ Middleware (AuthenticationMiddleware , instagram_service = instagram_service )
14
24
])
15
25
26
+
27
+ @app .post ('/v1/api/login' )
28
+ def login (credentials : Credentials ):
29
+ try :
30
+ username = instagram_service .login (credentials .username , credentials .password )
31
+ return JSONResponse (content = {"meassege" : "Login Successful" , "username" : username })
32
+ except Exception as e :
33
+ raise HTTPException (
34
+ status_code = 500 ,
35
+ detail = str (e )
36
+ )
37
+
38
+
16
39
@app .get ('/v1/api/profile' )
17
40
def profile_info (username : str = Query (..., description = "Username of the profile to retrieve" )):
18
41
try :
@@ -30,8 +53,8 @@ def profile_info(username: str = Query(..., description="Username of the profile
30
53
@app .get ('/v1/api/user_posts' )
31
54
def user_posts (
32
55
username : str = Query (..., description = "Username to retrieve posts for" ),
33
- from_date : Optional [str ] = Query (None , description = "Start date for post retrieval (YYYY-MM-DD)" ),
34
- to_date : Optional [str ] = Query (None , description = "End date for post retrieval (YYYY-MM-DD)" )
56
+ from_date : Optional [str ] = Query (None , description = "Start date for post retrieval (YYYY-MM-DD), Optional " ),
57
+ to_date : Optional [str ] = Query (None , description = "End date for post retrieval (YYYY-MM-DD), Optional " )
35
58
):
36
59
try :
37
60
posts = retrieve_post_metrics (username , from_date , to_date )
@@ -57,6 +80,29 @@ def media_posts(url: str = Query(..., description="URL of the media to retrieve
57
80
except Exception as e :
58
81
raise HTTPException (status_code = 500 , detail = str (e ))
59
82
83
+
84
+
85
+ @app .get ('/v1/api/media_likers' )
86
+ def media_likers (
87
+ media_id : str = Query (..., description = "media_id = {post_id}_{owner_id} to retrieve likers for the post" ),
88
+ username : str = Query (..., description = "Username of the logged-in user" )
89
+ ):
90
+ try :
91
+ likers = instagram_service .get_media_likers (username , media_id )
92
+ if not likers :
93
+ raise HTTPException (
94
+ status_code = 404 ,
95
+ detail = "Likers for this media not found"
96
+ )
97
+ return JSONResponse (content = likers )
98
+ except HTTPException as e :
99
+ raise e
100
+ except Exception as e :
101
+ raise HTTPException (
102
+ status_code = 500 ,
103
+ detail = str (e )
104
+ )
105
+
60
106
if __name__ == '__main__' :
61
107
import uvicorn
62
108
uvicorn .run (app , host = "127.0.0.1" , port = 8000 , log_level = "debug" )
0 commit comments