Spaces:
Build error
Build error
TruongTrongTien
commited on
Commit
•
480d518
1
Parent(s):
f55c453
Phase1/TienTT: Update more functions in database.py
Browse files- app/configs/database.py +22 -0
app/configs/database.py
CHANGED
@@ -44,6 +44,14 @@ def extract_file_url(file_dict):
|
|
44 |
file_urls.append(value['file_url'])
|
45 |
return file_urls
|
46 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
# Read file content, given its url
|
48 |
def read_from_file_url(file_url_list):
|
49 |
temp_content_list = []
|
@@ -56,3 +64,17 @@ def read_from_file_url(file_url_list):
|
|
56 |
file_content = ref.download_as_string().decode('utf-8')
|
57 |
temp_content_list.append(file_content)
|
58 |
return temp_content_list
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
file_urls.append(value['file_url'])
|
45 |
return file_urls
|
46 |
|
47 |
+
# Extract file's url with target description from a bunch of things
|
48 |
+
def extract_file_url_by_description(file_dict, target_description):
|
49 |
+
file_urls = []
|
50 |
+
for key, value in file_dict.items():
|
51 |
+
if value.get('description') == target_description:
|
52 |
+
file_urls.append(value.get('file_url'))
|
53 |
+
return file_urls
|
54 |
+
|
55 |
# Read file content, given its url
|
56 |
def read_from_file_url(file_url_list):
|
57 |
temp_content_list = []
|
|
|
64 |
file_content = ref.download_as_string().decode('utf-8')
|
65 |
temp_content_list.append(file_content)
|
66 |
return temp_content_list
|
67 |
+
|
68 |
+
# Download file content, given its url
|
69 |
+
def download_from_file_url(file_url_list, local_directory):
|
70 |
+
temp_content_list = []
|
71 |
+
bucket = storage.bucket(storageBucket)
|
72 |
+
for file_url in file_url_list:
|
73 |
+
parts = []
|
74 |
+
parts = file_url.split('/')
|
75 |
+
legal_file_url = parts[3:][0]
|
76 |
+
ref = bucket.blob(legal_file_url)
|
77 |
+
local_filename = f"{local_directory}/{legal_file_url}"
|
78 |
+
ref.download_to_filename(local_filename)
|
79 |
+
print(f"Downloaded {legal_file_url} to {local_filename}")
|
80 |
+
return True
|