1
- # Welcome to Cloud Functions for Firebase for Python!
2
- # To get started, simply uncomment the below code or create your own.
3
- # Deploy with `firebase deploy`
4
-
5
- from firebase_functions import https_fn
6
- from firebase_admin import initialize_app
7
-
8
- # initialize_app()
9
- #
10
- #
11
- # @https_fn.on_request()
12
- # def on_request_example(req: https_fn.Request) -> https_fn.Response:
13
- # return https_fn.Response("Hello world!")
1
+ import json
2
+ import os
3
+ import tempfile
4
+
5
+ import google .cloud .firestore
6
+ import requests
7
+ # The Firebase Admin SDK to access Cloud Firestore.
8
+ from firebase_admin import firestore , initialize_app
9
+ # The Cloud Functions for Firebase SDK to create Cloud Functions and set up triggers.
10
+ from firebase_functions import firestore_fn , https_fn
11
+ from git import Repo
12
+
13
+ app = initialize_app ()
14
+
15
+ BASE_URL = os .environ .get ("BASE_URL" )
16
+ PASSWORD_HEADER = os .environ .get ("PASSWORD_HEADER" )
17
+
18
+ def make_api_request (request_data ):
19
+
20
+ # Make an HTTP POST request to the API
21
+ url = f"{ BASE_URL } /api/serverless/questions"
22
+ try :
23
+ print (request_data )
24
+ response = requests .post (url , json = request_data , headers = {"PASSWORD_HEADER" : PASSWORD_HEADER })
25
+ response .raise_for_status () # Raise an exception for HTTP errors (4xx and 5xx)
26
+
27
+ print (f"API response status: { response .status_code } " )
28
+ print (f"API response: { response .json ()} " )
29
+ return 1
30
+
31
+ except requests .exceptions .RequestException as e :
32
+ print (f"Error making the API request: { e } " )
33
+ return 0
34
+
35
+ def load_problems ():
36
+ with tempfile .TemporaryDirectory () as base_path :
37
+ Repo .clone_from ("https://github.com/kyle8998/Practice-Coding-Questions.git" , base_path )
38
+
39
+ # Define the path to the "repo/leetcode" directory
40
+ base_path += "/leetcode"
41
+ problems = []
42
+
43
+ # Iterate over the folders in the base directory
44
+ for folder_name in os .listdir (base_path ):
45
+ folder_path = os .path .join (base_path , folder_name )
46
+
47
+ # Check if the item is a directory
48
+ if os .path .isdir (folder_path ):
49
+ problem_md_path = os .path .join (folder_path , "problem.md" )
50
+
51
+ # Check if "problem.md" exists in the folder
52
+ if os .path .exists (problem_md_path ):
53
+ # Open and read the contents of "problem.md"
54
+ with open (problem_md_path , "r" , encoding = "utf-8" ) as problem_file :
55
+ problem_contents = problem_file .read ()
56
+ problems .append (problem_contents )
57
+ else :
58
+ print (f"No problem.md found in { folder_name } " )
59
+ print (f"Total number of questions found: { len (problems )} " )
60
+ return problems
61
+
62
+ tags_to_search = ["Stack" , "Array" , "Tree" , "Hash Table" , "Binary Search" ]
63
+
64
+ def parse (qn ):
65
+ try :
66
+ qn = qn .split ("\n " )
67
+
68
+ title = qn [0 ][2 :]
69
+ qn .pop (0 )
70
+
71
+ while qn [0 ].strip () == "" :
72
+ qn .pop (0 )
73
+
74
+ difficulty = qn [0 ].split (" " )[2 ].strip ().upper ()
75
+ difficulty = difficulty [0 ].upper () + difficulty [1 :].lower ()
76
+
77
+ qn .pop (0 )
78
+ while qn [0 ].strip () == "" :
79
+ qn .pop (0 )
80
+
81
+ qn = '\n ' .join (qn )
82
+
83
+ if difficulty not in ["Easy" , "Medium" , "Hard" ]:
84
+ return None
85
+
86
+ if title .split (" " )[0 ] != "Problem" and title .split (" " )[1 ][- 1 ] != ":" :
87
+ return None
88
+ else :
89
+ title = " " .join (title .split (" " )[2 :])
90
+
91
+
92
+ search_space = title + qn
93
+ tags = []
94
+ for tag in tags_to_search :
95
+ if tag .lower () in search_space .lower ():
96
+ tags .append (tag )
97
+
98
+ if tags == []:
99
+ return None
100
+
101
+ return { "title" : title , "difficulty" : difficulty , "description" : qn , "tags" : tags }
102
+ except :
103
+ return None
104
+
105
+
106
+ @https_fn .on_request ()
107
+ def addmessage (req : https_fn .Request ) -> https_fn .Response :
108
+ if req .headers .get ("PASSWORD_HEADER" ) != PASSWORD_HEADER :
109
+ return https_fn .Response (status = 403 )
110
+
111
+ problems = load_problems ()
112
+ problems = [parse (qn ) for qn in problems ]
113
+ problems = [qn for qn in problems if qn is not None ]
114
+ print (f"Total number of questions parsed: { len (problems )} " )
115
+ countOfQuestionsAdded = sum ([make_api_request (qn ) for qn in problems ])
116
+ print (f"Total number of questions added: { countOfQuestionsAdded } " )
117
+ return https_fn .Response (f"Total number of questions added: { countOfQuestionsAdded } " )
0 commit comments