Last active
February 15, 2022 00:29
-
-
Save algb12/aded529f779f872cb6469c52e3b58e01 to your computer and use it in GitHub Desktop.
Poetizer File-System (PFS) - Receiver
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# Poetizer File-System (PFS) | |
# Receiver | |
# | |
# LICENSE: Public Domain | |
# AUTHOR: Anonymous | |
# | |
# NOTE: Do not take this seriously please, | |
# it's just a little fun project by a web-dev | |
# with way too much time on their hands! | |
# All files always saved in command-line working directory. | |
# Link to transmitter script will be added here when it is more user-friendly. | |
# | |
# USAGE: Set the POEM_ID, and if it is a valid data-poem, it'll download. | |
# | |
# DEPENDENCIES: requests, base64 | |
# The ID of the poem with the stored data. | |
# Try 3557733 for instance! | |
POEM_ID = 3557733 | |
POEMS_ENDPOINT = 'https://api.poetizer.com/poems' | |
import requests | |
import base64 | |
def main(): | |
print('Requesting from server...') | |
try: | |
r = requests.get(f'{POEMS_ENDPOINT}/{POEM_ID}') | |
if r.status_code == 404: | |
raise ValueError('The URL seems to be incorrect! Check if the POEM_ID is valid.') | |
if r.status_code != 200: | |
raise ValueError('The request was not successful!') | |
print('All good. Decoding file contents...') | |
f_name, f_data = map(base64.b64decode, r.json()['text'].split('^')[:2]) | |
if f_data == bytes('0', 'utf-8'): | |
print('File was in hidden mode, joining tags to reconstruct original file...') | |
f_data = r.json()['tags'] | |
f_data_reconstructed = [None] * len(f_data) | |
for chunk in f_data: | |
k, v = chunk.split(':') | |
f_data_reconstructed[int(k)] = v | |
f_data = base64.b64decode(''.join(f_data_reconstructed)) | |
print('Decoding filename...') | |
f_name = f_name.decode('utf-8') | |
print('Writing to file...') | |
with open(f'{POEM_ID}_{f_name}', 'wb') as f_stream: | |
f_stream.write(f_data) | |
print(f'File saved as: {POEM_ID}_{f_name}') | |
except Exception as e: | |
print('We have encountered an error!') | |
raise e | |
finally: | |
print(f'Server returned status code: {r.status_code}') | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment