From 4b8a8856debbc66770ede4d779dc451a03f656bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paul=20W=C3=BCrtz?= Date: Fri, 24 Apr 2020 03:42:16 +0200 Subject: [PATCH] closes #28 - adding a .py script to download the original problems texts, saving it localy as a json and displaying it in the translation view --- app/controllers/euler_problem_texts.py | 20 +++++++++++++++++++ app/controllers/translations_controller.rb | 6 ++++++ .../_translation-tips.de.html.erb | 14 ++++++++----- 3 files changed, 35 insertions(+), 5 deletions(-) create mode 100755 app/controllers/euler_problem_texts.py diff --git a/app/controllers/euler_problem_texts.py b/app/controllers/euler_problem_texts.py new file mode 100755 index 0000000..af4345b --- /dev/null +++ b/app/controllers/euler_problem_texts.py @@ -0,0 +1,20 @@ +import requests, json +from bs4 import BeautifulSoup + +URL = "https://projecteuler.net/problem={}" + +def getBS(url): + r = requests.get(url) + data = r.text + return BeautifulSoup(data,"lxml") + +problems = {} +for i in range(1, 701): + print(i) + problem = getBS(URL.format(i)) + problem_tag = problem.find("div", {"class":"problem_content"}) + problem_title = problem.find("h2").text + problem_html = str(problem_tag).replace("project/images/", "https://projecteuler.net/project/images/") + problems[i] = {"title":problem_title, "problem":problem_html} + +open("problems.json", "w"). write(json.dumps(problems, ensure_ascii=False, indent=4)) \ No newline at end of file diff --git a/app/controllers/translations_controller.rb b/app/controllers/translations_controller.rb index 63495e9..e6702db 100644 --- a/app/controllers/translations_controller.rb +++ b/app/controllers/translations_controller.rb @@ -1,9 +1,15 @@ +require "json" + class TranslationsController < ApplicationController before_action :set_problem, only: [:new, :create] before_action :set_accept, only: [:create] # GET /translations/new def new + file = File.open "app/controllers/problems.json" + problem_json = JSON.load file + @original_title = problem_json[@problem.id.to_s]["title"] + @original_problem = problem_json[@problem.id.to_s]["problem"] @translation = @problem.translations.build if @problem.is_translated? @translation.title = @problem.translation.title diff --git a/app/views/translations/_translation-tips.de.html.erb b/app/views/translations/_translation-tips.de.html.erb index e0bb687..d02d798 100644 --- a/app/views/translations/_translation-tips.de.html.erb +++ b/app/views/translations/_translation-tips.de.html.erb @@ -4,15 +4,19 @@
+ +
+
+ Originaler Text der Aufgabe "<%= @original_title %>" +
+
+ <%= raw(@original_problem) %> +
\ No newline at end of file