Last active
December 14, 2015 08:59
-
-
Save fernferret/5061939 to your computer and use it in GitHub Desktop.
Logfile uploader for logs.tf written in python! Because I'm too lazy to copy files over ssh and upload them...
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 python | |
''' | |
Log file uploader for http://logs.tf. | |
This should work with python 2.x (and probably 3.x) | |
Make sure you have the dependencies installed: | |
``` | |
pip install requests argparse | |
``` | |
You'll also need an API key for logs.tf. You can get one here: | |
http://logs.tf/uploader | |
This script is Public Domain (unless some other licens prevents it from being such!) | |
''' | |
import sys | |
import requests | |
import argparse | |
# Put your API Key here! | |
API_KEY = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' | |
LOGS_TF_URL = "http://logs.tf/upload" | |
def parse_args(): | |
''' | |
Parse 3 arguments from the command line: | |
- logfile The name of the logfile on disk. This can be absolute or | |
relative from this script. | |
- map *OPTIONAL* The name of the map | |
- title *OPTIONAL* The title of the match | |
''' | |
parser = argparse.ArgumentParser(description="Upload a log to logs.tf") | |
parser.add_argument('logfile', metavar='LOGFILE', | |
help="Standard TF2 Logfile. Should be located in " | |
"your tf/logs directory.") | |
parser.add_argument('-t', '--title', metavar='TITLE', | |
help="The title of this log.") | |
parser.add_argument('-m', '--map', metavar='MAP', | |
help="The map that this log was recorded on.") | |
return parser.parse_args() | |
def do_post(logfile, title=None, map=None): | |
''' | |
Perform the actual POST. Only include fields that are not None. | |
''' | |
payload = {'key': API_KEY} | |
try: | |
logfiles = {'logfile': open(logfile, 'r')} | |
except IOError: | |
print "FAILED to upload log:" | |
print "File '%s' did not exist!" % logfile | |
sys.exit(1) | |
if map: | |
payload['map'] = map | |
if title: | |
payload['title'] = title | |
results = requests.post(LOGS_TF_URL, files=logfiles, data=payload).json() | |
if not results.get('success', False): | |
print "FAILED to upload log:" | |
msg = results.get('error', 'UNKNOWN') | |
print msg | |
if msg == "Invalid API key": | |
print "You can get an API Key here: http://logs.tf/uploader" | |
sys.exit(1) | |
print "Great success!" | |
print "Log ID: %s" % results.get('log_id', 'UNKNOWN') | |
print "URL: http://logs.tf%s" % results.get('url', 'UNKNOWN') | |
def main(): | |
'''Parse the arguments, then upload the log to logs.tf!''' | |
args = parse_args() | |
do_post(args.logfile, args.title, args.map) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment