Last active
April 12, 2020 15:38
-
-
Save tasosb/7fd8a4138fde63ec33864ed398ba85f5 to your computer and use it in GitHub Desktop.
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
# -*- coding: utf-8 -*- | |
import requests | |
import os.path | |
from os import path | |
import datetime | |
import fileinput | |
import http.server | |
from time import sleep | |
import socketserver | |
from threading import Thread | |
def downloadStatus(local_server_file): | |
dn = datetime.datetime.now(); | |
if int(dn.strftime("%M"))<2 or not path.exists(local_server_file): | |
with open(local_server_file, 'wb') as f: | |
r = requests.get('http://status.vatsim.net') | |
if r.status_code == 200: | |
f.write(r.content) | |
else: | |
print("An error occured while downloading VATSIM Servers.") | |
return False | |
return True | |
def downloadData(local_server_file,local_vatsimdata_file): | |
f = open(local_server_file, encoding="utf-8") | |
for x in f: | |
if x[:5]=="url0=": | |
url=str(x[5:-1]) | |
break | |
print("Downloading from: ",url) | |
r = requests.get(url) | |
with open(local_vatsimdata_file, 'wb') as f: | |
print("Status Code: ",r.status_code) | |
if r.status_code == 200: | |
f.write(r.content) | |
else: | |
print("An error occured while downloading VATSIM Data-File.") | |
return False | |
return True | |
def parseAndPublishData(local_vatsimdata_file): | |
if path.exists(local_vatsimdata_file): | |
with open(local_vatsimdata_file, 'r', encoding="utf-8") as f: | |
fd = f.read() | |
fd = fd.replace("VERSION = 9", "VERSION = 8") | |
with open(local_vatsimdata_file, 'w', encoding="utf-8") as f: | |
f.write(fd) | |
return | |
def run(): | |
PORT = 80 | |
print("Trying to run server on port: ",PORT) | |
Handler = http.server.SimpleHTTPRequestHandler | |
with socketserver.TCPServer(("localhost", PORT), Handler) as httpd: | |
httpd.serve_forever() | |
return | |
def updates(): | |
local_server_file = 'servers.txt'; | |
local_vatsimdata_file = 'vatsim-data.txt'; | |
while True: | |
if downloadStatus(local_server_file): | |
if downloadData(local_server_file,local_vatsimdata_file): | |
parseAndPublishData(local_vatsimdata_file) | |
sleep(60) | |
if __name__ == '__main__': | |
Thread(target = updates).start() | |
Thread(target = run).start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment