diff options
author | rubenwardy <rw@rubenwardy.com> | 2018-05-11 12:57:16 +0100 |
---|---|---|
committer | rubenwardy <rw@rubenwardy.com> | 2018-05-11 12:57:16 +0100 |
commit | 5e44f3d64c2be1b890e393832efd9fb100adf2c0 (patch) | |
tree | ea4a1fd0ce7fe8ed9c97687f6afd8e18b0a393e4 /app/tasks/importtasks.py | |
parent | a55b4f84ff8fa9774b1aa3a0363d848b9a9bd252 (diff) | |
download | cheatdb-5e44f3d64c2be1b890e393832efd9fb100adf2c0.tar.xz |
Move Github import to backend
Fixes #41
Diffstat (limited to 'app/tasks/importtasks.py')
-rw-r--r-- | app/tasks/importtasks.py | 68 |
1 files changed, 68 insertions, 0 deletions
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 |