update_paste_list and get_pastes_by_language return Paste objects

This commit is contained in:
agatha 2023-09-12 16:56:37 -04:00
parent 1b0d47c196
commit 1f1f7bc881
2 changed files with 16 additions and 8 deletions

View File

@ -5,7 +5,7 @@ def main():
pastebin = PastebinClient() pastebin = PastebinClient()
# Fetch public paste list # Fetch public paste list
pastebin.populate_pastes() pastebin.update_paste_list()
# Fetch full data from a few pastes to test methods # Fetch full data from a few pastes to test methods
pastebin.fetch_paste(0) pastebin.fetch_paste(0)
@ -13,12 +13,12 @@ def main():
# Test get_pastes_by_language # Test get_pastes_by_language
py_pastes = pastebin.get_pastes_by_language("Python") py_pastes = pastebin.get_pastes_by_language("Python")
for i in py_pastes: for paste in py_pastes:
print(pastebin.pastes[i]) print(paste)
# Test if duplication is prevented by Paste.__hash__ # Test if duplication is prevented by Paste.__hash__
print(len(pastebin.pastes)) print(len(pastebin.pastes))
pastebin.populate_pastes() pastebin.update_paste_list()
print(len(pastebin.pastes)) print(len(pastebin.pastes))

View File

@ -157,9 +157,14 @@ class PastebinClient(PastebinAPI):
super().__init__(headers, proxies, debug) super().__init__(headers, proxies, debug)
self.pastes = [] self.pastes = []
def populate_pastes(self): def update_paste_list(self):
pastes_catalog = set(self.get_public_paste_list()) fetched_pastes = set(self.get_public_paste_list())
self.pastes = list(set(self.pastes).union(pastes_catalog)) existing_pastes = set(self.pastes)
new_pastes = fetched_pastes - existing_pastes
self.pastes = list(existing_pastes.union(fetched_pastes))
return list(new_pastes)
def fetch_paste(self, paste_index): def fetch_paste(self, paste_index):
if paste_index >= len(self.pastes): if paste_index >= len(self.pastes):
@ -173,4 +178,7 @@ class PastebinClient(PastebinAPI):
return len(self.pastes) return len(self.pastes)
def get_pastes_by_language(self, lang): def get_pastes_by_language(self, lang):
return [i for i, paste in enumerate(self.pastes) if paste.lang.lower() == lang.lower()] return [
self.pastes[i] for i, paste in enumerate(self.pastes)
if paste.lang.lower() == lang.lower()
]