Add search methods

This commit is contained in:
agatha 2023-09-12 17:11:02 -04:00
parent 1f1f7bc881
commit 68141fbbf2
2 changed files with 25 additions and 14 deletions

View File

@ -1,25 +1,22 @@
from pastebin.client import PastebinClient import time
from pastebin.client import PastebinClient, search_by_language
def main(): def main():
pastebin = PastebinClient() pastebin = PastebinClient()
lang_filters = ['Email', 'Python']
# Fetch public paste list while True:
pastebin.update_paste_list() new_pastes = pastebin.update_paste_list()
print('Updating pastes...')
# Fetch full data from a few pastes to test methods for lang in lang_filters:
pastebin.fetch_paste(0) pastes = search_by_language(new_pastes, lang)
pastebin.fetch_paste(1)
# Test get_pastes_by_language for paste in pastes:
py_pastes = pastebin.get_pastes_by_language("Python") print(f'New {lang} paste: {paste}')
for paste in py_pastes:
print(paste)
# Test if duplication is prevented by Paste.__hash__ time.sleep(60)
print(len(pastebin.pastes))
pastebin.update_paste_list()
print(len(pastebin.pastes))
if __name__ == '__main__': if __name__ == '__main__':

View File

@ -182,3 +182,17 @@ class PastebinClient(PastebinAPI):
self.pastes[i] for i, paste in enumerate(self.pastes) self.pastes[i] for i, paste in enumerate(self.pastes)
if paste.lang.lower() == lang.lower() if paste.lang.lower() == lang.lower()
] ]
def search_by_language(pastes, lang):
return [
pastes[i] for i, paste in enumerate(pastes)
if paste.lang.lower() == lang.lower()
]
def search_by_category(pastes, category):
return [
pastes[i] for i, paste in enumerate(pastes)
if paste.category == category
]