From 38ed03f49a6f2cd5696c7781ee92bf8e80625b9e Mon Sep 17 00:00:00 2001 From: rubenwardy Date: Sat, 12 May 2018 18:50:09 +0100 Subject: [PATCH] Add Krock's mod search to meta importer to find forum topic ID --- app/static/package_create.js | 4 ++- app/tasks/importtasks.py | 63 +++++++++++++++++++++++++++++++++++- app/views/tasks.py | 4 ++- 3 files changed, 68 insertions(+), 3 deletions(-) diff --git a/app/static/package_create.js b/app/static/package_create.js index 0cad6eb..771ae35 100644 --- a/app/static/package_create.js +++ b/app/static/package_create.js @@ -24,13 +24,15 @@ $(function() { $(".pkg_repo").hide() performTask("/tasks/getmeta/new/?url=" + encodeURI(repoURL)).then(function(result) { - console.log(result) $("#name").val(result.name || "") $("#title").val(result.title || "") $("#repo").val(result.repo || repoURL) $("#issueTracker").val(result.issueTracker || "") $("#desc").val(result.description || "") $("#shortDesc").val(result.short_description || "") + if (result.forumId) { + $("#forums").val(result.forumId) + } finish() }).catch(function(e) { alert(e) diff --git a/app/tasks/importtasks.py b/app/tasks/importtasks.py index 7940a17..ba19eba 100644 --- a/app/tasks/importtasks.py +++ b/app/tasks/importtasks.py @@ -52,6 +52,62 @@ class GithubURLMaker: return "https://github.com/" + self.user + "/" + self.repo + "/archive/" + commit + ".zip" +krock_list_cache = None +krock_list_cache_by_name = None +def getKrockList(): + global krock_list_cache + global krock_list_cache_by_name + + if krock_list_cache is None: + contents = urllib.request.urlopen("http://krock-works.16mb.com/MTstuff/modList.php").read().decode("utf-8") + list = json.loads(contents) + + def h(x): + if not ("title" in x and "author" in x and \ + "topicId" in x and "link" in x and x["link"] != ""): + return False + + import re + m = re.search("\[([A-Za-z0-9_]+)\]", x["title"]) + if m is None: + return False + + x["name"] = m.group(1) + return True + + def g(x): + return { + "title": x["title"], + "author": x["author"], + "name": x["name"], + "topicId": x["topicId"], + "link": x["link"], + } + + krock_list_cache = [g(x) for x in list if h(x)] + krock_list_cache_by_name = {} + for x in krock_list_cache: + if not x["name"] in krock_list_cache_by_name: + krock_list_cache_by_name[x["name"]] = [] + + krock_list_cache_by_name[x["name"]].append(x) + + return krock_list_cache, krock_list_cache_by_name + +def findModInfo(author, name, link): + _, lookup = getKrockList() + + if name in lookup: + if len(lookup[name]) == 1: + return lookup[name][0] + + for x in lookup[name]: + if x["author"] == author: + return x + + return None + + def parseConf(string): retval = {} for line in string.split("\n"): @@ -63,8 +119,9 @@ def parseConf(string): return retval + @celery.task() -def getMeta(urlstr): +def getMeta(urlstr, author): url = urlparse(urlstr) urlmaker = None @@ -108,6 +165,10 @@ def getMeta(urlstr): cutIdx = min(len(desc), 200 if idx < 5 else idx) result["short_description"] = desc[:cutIdx] + info = findModInfo(author, result["name"], result["repo"]) + if info is not None: + result["forumId"] = info["topicId"] + return result @celery.task() diff --git a/app/views/tasks.py b/app/views/tasks.py index 72d7d1d..c5a508a 100644 --- a/app/views/tasks.py +++ b/app/views/tasks.py @@ -13,7 +13,9 @@ from .utils import * @app.route("/tasks/getmeta/new/", methods=["POST"]) @login_required def new_getmeta_page(): - aresult = getMeta.delay(request.args.get("url")) + author = request.args.get("author") + author = current_user.forums_username if author is None else author + aresult = getMeta.delay(request.args.get("url"), author) return jsonify({ "poll_url": url_for("check_task", id=aresult.id), }) -- 2.44.0