Last active
November 27, 2024 21:32
-
-
Save onyx-and-iris/de2255ab3a849cc6f8a8d1b5eb470f32 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import logging | |
import time | |
from pathlib import Path | |
import requests | |
from watchdog.events import FileSystemEventHandler | |
from watchdog.observers import Observer | |
logging.basicConfig(level=logging.INFO) | |
class Handler(FileSystemEventHandler): | |
logger = logging.getLogger("handler") | |
APIKEYS = ("<file.io apikey>",) | |
def on_modified(self, event): | |
p = Path(event.src_path) | |
if p.is_file() and p.name == "name.log": | |
filepath = self.get_filepath(p) | |
self.logger.info(f"posting file {filepath}") | |
for apikey in self.APIKEYS: | |
self.post_file(filepath, apikey) | |
def get_filepath(self, logfile): | |
with open(logfile, "r") as f: | |
for line in f: | |
pass | |
return line.split()[2] | |
def post_file(self, filepath, apikey): | |
url = "https://file.io/" | |
headers = { | |
"accept": "application/json", | |
"Authorization": f"Bearer {apikey}", | |
} | |
data = { | |
"expires": "1d", | |
"maxDownloads": 1, | |
"autoDelete": True, | |
} | |
with open(filepath, "rb") as f: | |
response = requests.post( | |
url, | |
headers=headers, | |
files={ | |
"file": f, | |
}, | |
data=data, | |
) | |
if response.ok: | |
self.logger.info("SUCCESS: name: {name}, URL: {link}".format(**response.json())) | |
else: | |
self.logger.error(response.status_code) | |
def main(): | |
event_handler = Handler() | |
observer = Observer() | |
observer.schedule(event_handler, path="/path/to/working/directory/", recursive=False) | |
observer.start() | |
observer.join() | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A small script demonstrating how to upload a file to file.io via its API using python.
The implementation here monitors a logfile at "/path/to/working/directory/name.log" for changes. Assuming each line resembles:
post_file() will post the last file appended to the log.
Multiple accounts can be uploaded to in a single run by adding more apikeys to APIKEYS.