PaulMartrenchar commited on
Commit
6b24d80
·
1 Parent(s): 4c11d35

Move the DB to a class

Browse files
Files changed (2) hide show
  1. app.py +3 -2
  2. db.py +45 -40
app.py CHANGED
@@ -7,7 +7,7 @@ from jobspy_indeed import indeed_get_jobs
7
  from WelcomeToTheJungle import wtoj_get_jobs
8
  from jobspy_linkedin import linkedin_get_jobs
9
  from ai_manager import get_extra_information
10
- from db import add_to_db
11
 
12
  def html_format_page(jobs : List[JobDescription]):
13
  result = ["<html><head><style>.job{display: flex;width:70%;margin: 5px auto;border: 1px solid;border-radius: 5px;}.logobox{flex: 1;display: flex;align-items: center;justify-content: center;}.logo{width:100px;height:100px}h4{margin: 2px;}</style></head><body>"]
@@ -56,7 +56,8 @@ def search_jobs(search_term, platform):
56
  for job in selected_jobs:
57
  job.ai_result = get_extra_information(job.company, job.job_description)
58
 
59
- add_to_db(selected_jobs)
 
60
 
61
  return html_format_page(selected_jobs)
62
 
 
7
  from WelcomeToTheJungle import wtoj_get_jobs
8
  from jobspy_linkedin import linkedin_get_jobs
9
  from ai_manager import get_extra_information
10
+ from db import Database
11
 
12
  def html_format_page(jobs : List[JobDescription]):
13
  result = ["<html><head><style>.job{display: flex;width:70%;margin: 5px auto;border: 1px solid;border-radius: 5px;}.logobox{flex: 1;display: flex;align-items: center;justify-content: center;}.logo{width:100px;height:100px}h4{margin: 2px;}</style></head><body>"]
 
56
  for job in selected_jobs:
57
  job.ai_result = get_extra_information(job.company, job.job_description)
58
 
59
+ db = Database()
60
+ db.add_to_db(selected_jobs)
61
 
62
  return html_format_page(selected_jobs)
63
 
db.py CHANGED
@@ -6,47 +6,52 @@ import json
6
 
7
  REPO_ID = "jobsearch_database"
8
  FILE_NAME = "db.json"
9
-
10
- DB : List[JobDescription] = []
11
-
12
  JSON_DATASET_DIR = Path("json_dataset")
13
  JSON_DATASET_DIR.mkdir(parents=True, exist_ok=True)
14
  JSON_DATASET_PATH = JSON_DATASET_DIR / FILE_NAME
15
 
16
- def get_current_db() -> List[JobDescription]:
17
- filepath = hf_hub_download(repo_id=REPO_ID, filename=FILE_NAME, repo_type="dataset")
18
- with open(filepath, 'r') as file:
19
- return json.load(file)
20
-
21
- def merge_dbs(list1 : List[JobDescription], list2 : List[JobDescription]):
22
- unique_urls = set()
23
- merged_list = []
24
-
25
- for job in list1 + list2:
26
- if job.url not in unique_urls:
27
- unique_urls.add(job.url)
28
- merged_list.append(job)
29
-
30
- return merged_list
31
-
32
- def add_to_db(new_jobs : List[JobDescription]):
33
- #Remove descriptions from the JobDescription
34
- for job in new_jobs:
35
- job.job_description = ""
36
-
37
- #get current DB
38
- current_db = DB
39
- #merge
40
- new_db = merge_dbs(current_db, new_jobs)
41
-
42
- #update
43
- api = HfApi()
44
- with open(FILE_NAME, 'w') as file:
45
- json.dump(new_db, file, indent=4)
46
- api.upload_file(
47
- path_or_fileobj=FILE_NAME,
48
- path_in_repo=FILE_NAME,
49
- repo_id=REPO_ID,
50
- repo_type="dataset",
51
- )
52
- DB = new_db
 
 
 
 
 
 
 
 
 
6
 
7
  REPO_ID = "jobsearch_database"
8
  FILE_NAME = "db.json"
 
 
 
9
  JSON_DATASET_DIR = Path("json_dataset")
10
  JSON_DATASET_DIR.mkdir(parents=True, exist_ok=True)
11
  JSON_DATASET_PATH = JSON_DATASET_DIR / FILE_NAME
12
 
13
+ class Database:
14
+ def __init__(self):
15
+ self.DB : List[JobDescription] = []
16
+
17
+
18
+ def get_current_db(self) -> List[JobDescription]:
19
+ filepath = hf_hub_download(repo_id=REPO_ID, filename=FILE_NAME, repo_type="dataset")
20
+ with open(filepath, 'r') as file:
21
+ return json.load(file)
22
+
23
+ def save_db(self, new_db):
24
+ api = HfApi()
25
+ with open(FILE_NAME, 'w') as file:
26
+ json.dump(new_db, file, indent=4)
27
+ api.upload_file(
28
+ path_or_fileobj=FILE_NAME,
29
+ path_in_repo=FILE_NAME,
30
+ repo_id=REPO_ID,
31
+ repo_type="dataset",
32
+ )
33
+
34
+ def merge_dbs(self, list1 : List[JobDescription], list2 : List[JobDescription]):
35
+ unique_urls = set()
36
+ merged_list = []
37
+
38
+ for job in list1 + list2:
39
+ if job.url not in unique_urls:
40
+ unique_urls.add(job.url)
41
+ merged_list.append(job)
42
+
43
+ return merged_list
44
+
45
+ def add_to_db(self, new_jobs : List[JobDescription]):
46
+ #Remove descriptions from the JobDescription
47
+ for job in new_jobs:
48
+ job.job_description = ""
49
+
50
+ #get current DB
51
+ current_db = self.DB
52
+ #merge
53
+ new_db = self.merge_dbs(current_db, new_jobs)
54
+
55
+ #update
56
+ self.save_db(new_db)
57
+ self.DB = new_db