forked from Chaosthebot/Chaos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchaos.py
82 lines (60 loc) · 1.97 KB
/
chaos.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import time
import os
import sys
import logging
import threading
import http.server
import random
import subprocess
import settings
import patch
import schedule
from os.path import dirname, abspath, join
import cron
import github_api as gh
import github_api.prs
import github_api.voting
import github_api.repos
import github_api.comments
from github_api import exceptions as gh_exc
class HTTPServerRequestHandler(http.server.BaseHTTPRequestHandler):
def __init__(self):
# Load fortunes
self.fortunes = []
with open("data/fortunes.txt", "r", encoding="utf8") as f:
self.fortunes = f.read().split("\n%\n")
# Call superclass constructor
super(HTTPServerRequestHandler, self).__init__()
def do_GET(self):
self.send_response(200)
self.send_header("Content-type", "text/html; charset=utf-8")
self.end_headers()
self.wfile.write(random.choice(self.fortunes).encode("utf8"))
def http_server():
s = http.server.HTTPServer(('', 8080), HTTPServerRequestHandler)
s.serve_forever()
def start_http_server():
http_server_thread = threading.Thread(target=http_server)
http_server_thread.start()
def main():
logging.basicConfig(level=logging.DEBUG)
logging.getLogger("requests").propagate = False
logging.getLogger("sh").propagate = False
log = logging.getLogger("chaosbot")
api = gh.API(settings.GITHUB_USER, settings.GITHUB_SECRET)
log.info("starting up and entering event loop")
os.system("pkill chaos_server")
server_dir = join(dirname(abspath(__file__)), "server")
subprocess.Popen([sys.executable, "server.py"], cwd=server_dir)
#log.info("starting http server")
#start_http_server()
# Schedule all cron jobs to be run
cron.schedule_jobs()
while True:
# Run any scheduled jobs on the next second.
schedule.run_pending()
time.sleep(1)
if __name__ == "__main__":
main()