forked from cmccabe/linkulator2
Added search data component
This commit is contained in:
parent
326b889b8e
commit
e10c919b07
30
data.py
30
data.py
|
@ -70,6 +70,15 @@ def parse_ignore_file() -> list:
|
|||
return ignore_names
|
||||
|
||||
|
||||
def get_id_from_parent(parent_id: str, link_data: list) -> int:
|
||||
"""given a parent ID, return the ID for the parent record or -1"""
|
||||
assert parent_id != ""
|
||||
for record in link_data:
|
||||
if record[0] == parent_id.partition("+")[2]:
|
||||
return record[0]
|
||||
return -1
|
||||
|
||||
|
||||
class LinkData:
|
||||
"""Class that contains link_data, categories and categories count tables,
|
||||
plus methods to generate and update these items"""
|
||||
|
@ -173,3 +182,24 @@ class LinkData:
|
|||
cat_record["count"] += 1
|
||||
if cat_record["last_updated"] < timestamp:
|
||||
cat_record["last_updated"] = timestamp
|
||||
|
||||
def search(self, keyword: str) -> list:
|
||||
"""returns a unique list of link_data record IDs for posts that contain
|
||||
the specified keyword"""
|
||||
assert keyword != ""
|
||||
|
||||
query = (record for record in self.link_data if keyword in record)
|
||||
|
||||
if query:
|
||||
search_results: set = set()
|
||||
for record in query:
|
||||
post_id = record[0]
|
||||
parent_id = record[3]
|
||||
if post_id:
|
||||
search_results.add(record[0])
|
||||
else:
|
||||
parent_post_id = get_id_from_parent(parent_id, self.link_data)
|
||||
if parent_post_id >= 0:
|
||||
search_results.add(parent_post_id)
|
||||
|
||||
return sorted(search_results)
|
||||
|
|
Loading…
Reference in New Issue