aboutsummaryrefslogtreecommitdiff
path: root/app/tasks
diff options
context:
space:
mode:
authorrubenwardy <rw@rubenwardy.com>2018-05-11 12:57:16 +0100
committerrubenwardy <rw@rubenwardy.com>2018-05-11 12:57:16 +0100
commit5e44f3d64c2be1b890e393832efd9fb100adf2c0 (patch)
treeea4a1fd0ce7fe8ed9c97687f6afd8e18b0a393e4 /app/tasks
parenta55b4f84ff8fa9774b1aa3a0363d848b9a9bd252 (diff)
downloadcheatdb-5e44f3d64c2be1b890e393832efd9fb100adf2c0.tar.xz
Move Github import to backend
Fixes #41
Diffstat (limited to 'app/tasks')
-rw-r--r--app/tasks/__init__.py44
-rw-r--r--app/tasks/importtasks.py68
2 files changed, 112 insertions, 0 deletions
diff --git a/app/tasks/__init__.py b/app/tasks/__init__.py
new file mode 100644
index 0000000..c431fae
--- /dev/null
+++ b/app/tasks/__init__.py
@@ -0,0 +1,44 @@
+import flask
+from flask.ext.sqlalchemy import SQLAlchemy
+from celery import Celery
+from app import app
+from app.models import *
+
+class FlaskCelery(Celery):
+ def __init__(self, *args, **kwargs):
+ super(FlaskCelery, self).__init__(*args, **kwargs)
+ self.patch_task()
+
+ if 'app' in kwargs:
+ self.init_app(kwargs['app'])
+
+ def patch_task(self):
+ TaskBase = self.Task
+ _celery = self
+
+ class ContextTask(TaskBase):
+ abstract = True
+
+ def __call__(self, *args, **kwargs):
+ if flask.has_app_context():
+ return TaskBase.__call__(self, *args, **kwargs)
+ else:
+ with _celery.app.app_context():
+ return TaskBase.__call__(self, *args, **kwargs)
+
+ self.Task = ContextTask
+
+ def init_app(self, app):
+ self.app = app
+ self.config_from_object(app.config)
+
+def make_celery(app):
+ celery = FlaskCelery(app.import_name, backend=app.config['CELERY_RESULT_BACKEND'],
+ broker=app.config['CELERY_BROKER_URL'])
+
+ celery.init_app(app)
+ return celery
+
+celery = make_celery(app)
+
+from . import importtasks
diff --git a/app/tasks/importtasks.py b/app/tasks/importtasks.py
new file mode 100644
index 0000000..50b5f5d
--- /dev/null
+++ b/app/tasks/importtasks.py
@@ -0,0 +1,68 @@
+import flask
+from flask.ext.sqlalchemy import SQLAlchemy
+import urllib.request
+from urllib.parse import urlparse
+
+from app import app
+from app.models import *
+from app.tasks import celery
+
+class GithubURLMaker:
+ def __init__(self, url):
+ # Rewrite path
+ import re
+ m = re.search("^\/([^\/]+)\/([^\/]+)\/?$", url.path)
+ if m is None:
+ return
+
+ user = m.group(1)
+ repo = m.group(2)
+ self.baseUrl = "https://raw.githubusercontent.com/" + user + "/" + repo.replace(".git", "") + "/master"
+
+ def isValid(self):
+ return self.baseUrl is not None
+
+ def getModConfURL(self):
+ return self.baseUrl + "/mod.conf"
+
+def parseConf(string):
+ retval = {}
+ for line in string.split("\n"):
+ idx = line.find("=")
+ if idx > 0:
+ key = line[:idx-1].strip()
+ value = line[idx+1:].strip()
+ retval[key] = value
+
+ return retval
+
+@celery.task()
+def getMeta(urlstr):
+ url = urlparse(urlstr)
+
+ urlmaker = None
+ if url.netloc == "github.com":
+ urlmaker = GithubURLMaker(url)
+
+ if not urlmaker.isValid():
+ print("Error! Url maker not valid")
+ return
+
+ print(urlmaker.getModConfURL())
+
+ result = {}
+
+ try:
+ contents = urllib.request.urlopen(urlmaker.getModConfURL()).read().decode("utf-8")
+ conf = parseConf(contents)
+ for key in ["name", "description"]:
+ try:
+ result[key] = conf[key]
+ except KeyError:
+ pass
+
+ print(conf)
+ except OSError:
+ print("mod.conf does not exist")
+
+ return result