Advertisement
Guest User

course_tree_markdown.lua

a guest
Jun 1st, 2019
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.22 KB | None | 0 0
  1. local acc, scr_name = ...
  2.  
  3. local host = "https://www.duolingo.com"
  4. local from_lang, learning_lang = nil, nil
  5. do
  6.     from_lang, learning_lang = scr_name:match("^[^,]+,([^,]+),([^,]+)%.lua$")
  7.     if not from_lang then
  8.         acc.output().print("Failed to get params from the script name")
  9.         acc.output().print("For example: ctm,en,de.lua")
  10.         return {}
  11.     end
  12. end
  13.  
  14. local user   = acc.user()
  15. local output = acc.output()
  16. local utils  = acc.bot().utils()
  17.  
  18. local loc_table = {
  19.     ["Vocab per Skill Unit for the %s / %s Course"] = {
  20.         ["ru"] = "Список навыков со списком изучаемых слов %s / %s курса",
  21.         ["de"] = "Vokabeln per Fähigkeit für den %s / %s Kurs"
  22.     },
  23.     ["Tips and notes"] = {
  24.         ["ru"] = "Подсказки и заметки",
  25.         ["de"] = "Tipps und Anmerkungen"
  26.     },
  27.     ["Number of lessons"] = {
  28.         ["ru"] = "Количество уроков",
  29.         ["de"] = "Anzahl Lektionen"
  30.     },
  31.     ["Words"] = {
  32.         ["ru"] = "Слова",
  33.         ["de"] = "Wörter"
  34.     },
  35.     ["Skills"] = {
  36.         ["ru"] = "Навыки",
  37.         ["de"] = "Fähigkeiten"
  38.     },
  39.     ["Lessons"] = {
  40.         ["ru"] = "Уроки",
  41.         ["de"] = "Lektionen"
  42.     },
  43.     ["no"] = {
  44.         ["ru"] = "нет",
  45.         ["de"] = "Nein"
  46.     },
  47.     ["yes"] = {
  48.         ["ru"] = "да",
  49.         ["de"] = "Ja"
  50.     },
  51.     ["test"] = {
  52.         ["ru"] = "тест",
  53.         ["de"] = "testen"
  54.     },
  55.     ["Total: %d skills, %s lessons, %d/%d words"] = {
  56.         ["ru"] = "Всего: %d навыков, %s уроков, %d/%d слов",
  57.         ["de"] = "Insgesamt: %d Fähigkeiten, %s Lektionen, %d/%d Wörter"
  58.     },
  59.     ["Total"] = {
  60.         ["ru"] = "Всего",
  61.         ["de"] = "Insgesamt"
  62.     },
  63.     ["Another lists:"] = {
  64.         ["ru"] = "Другие списки:",
  65.         ["de"] = "Andere Listen:"
  66.     },
  67.     ["Last update: %s"] = {
  68.         ["ru"] = "Последнее обновление: %s",
  69.         ["de"] = "Letzte Aktualisierung: %s"
  70.     }
  71. }
  72.  
  73. local tr = function(str, lang)
  74.     return str and lang and loc_table[str] and loc_table[str][lang] or str
  75. end
  76.  
  77. local display_error_status = function(err)
  78.     if err then output.print(err.tostring()) end
  79.     output.set_stream()
  80.     output.printf("********** %s ERROR **********", scr_name:upper())
  81. end
  82.  
  83. if not acc.course().switch(from_lang, learning_lang) then
  84.     display_error_status(acc.error)
  85.     return {}
  86. end
  87.  
  88. local scnt = user.course_info().skills_count()
  89. local idx  = 0
  90. local skt  = {}
  91. local frm  = nil
  92. do
  93.     local sz = math.floor(math.log10(scnt) + 1)
  94.     frm = string.format("[%%%dd/%%%dd]", sz, sz)
  95. end
  96. for s, d in user.course_info().each_skill() do
  97.     if idx > 0 then
  98.         acc.bot().utils().pause(2, 5)
  99.     end
  100.     idx = idx + 1
  101.     local t = {}
  102.     t.title = s.name
  103.     t.url   = s.urlName
  104.     output.printf(frm .. " Getting skill \"%s\"", idx, scnt, t.title)
  105.     local sd = acc.course().get_skill_data(t.url)
  106.     if not sd then
  107.         display_error_status(acc.error)
  108.         return {}
  109.     end
  110.     t.id      = s.id
  111.     t.short   = s.shortName
  112.     t.nless   = s.lessons
  113.     if d.sectionName then -- It is the new style of Tips&Notes
  114.         t.expl  = (type(s.explanation) == "table") and (type(s.explanation.url) == "string") and (s.explanation.url ~= "")
  115.         t.sname = d.sectionName  
  116.     else
  117.         t.expl  = (type(s.tipsAndNotes) == "string") and (s.tipsAndNotes ~= "")
  118.     end
  119.     t.chkp    = d.checkpoint
  120.     t.levels  = s.levels
  121.     t.flevels = s.finishedLevels
  122.     t.words   = {}
  123.     for j = 1, #sd.lessonWords do
  124.         local w = sd.lessonWords[j]
  125.         if type(w) == "table" then
  126.             for k = 1, #w do table.insert(t.words, w[k]) end
  127.         end
  128.     end
  129.     table.insert(skt, t)
  130. end
  131.  
  132. output.set_stream(string.format("tree_markdown_%s_%s.md", from_lang, learning_lang), "w")
  133.  
  134. local function get_original_lessons_count(lessons, ftable)
  135.     local res = nil
  136.     for i = 1, #lessons do
  137.         if lessons[i] ~= -1 and ftable[i] then
  138.             res = lessons[i] / ftable[i]
  139.             break
  140.         end
  141.     end
  142.     if not res then
  143.         local l = lessons.total
  144.         if l then
  145.             local f = 0
  146.             for i = 1, #ftable do f = f + ftable[i] end
  147.             res = l / f
  148.         end
  149.     end
  150.     if res and res > 0 and math.floor(res) == res then
  151.         return res
  152.     end
  153.     return nil
  154. end
  155.  
  156. local function ensure_lessons_count(lessons)
  157.     local f = { 1, 1, 2, 3, 5 }
  158.     local b = get_original_lessons_count(lessons, f)
  159.     if b then
  160.         for i = 1, #lessons do
  161.             if lessons[i] == -1 then
  162.                 lessons[i] = b * f[i]
  163.             end
  164.         end
  165.     end
  166. end
  167.  
  168. local function lessons_to_string(lessons)
  169.     local t = {}
  170.     local a = 0
  171.     for i = 1, #lessons do
  172.         local l = lessons[i]
  173.         if l == -1 then
  174.             table.insert(t, "?")
  175.             a = 0
  176.         else
  177.             table.insert(t, l)
  178.             if a then a = a + l end
  179.         end
  180.     end
  181.     return table.concat(t, " | ") .. " || " .. (a and tostring(a) or "?")
  182. end
  183.  
  184. local lcnt, wcnt = {}, 0
  185. local tips = 0
  186. local course_id  = string.format("%s/%s", from_lang, learning_lang)
  187. local course_str = acc.user().get("currentCourse.title")
  188. local section_name = nil
  189. output.printf("TITLE: " .. string.format(tr("Vocab per Skill Unit for the %s / %s Course", from_lang), course_str, string.upper(from_lang)))
  190. output.print("")
  191. output.print("")
  192. for i = 1, #skt do
  193.     local t = skt[i]
  194.     if t.chkp then
  195.         output.print("----------")
  196.     end
  197.     if i > 1 then
  198.         output.print("")
  199.     end
  200.  
  201.     if t.sname and t.sname ~= section_name then
  202.         section_name = t.sname
  203.         output.printf("## **%s**", section_name)
  204.         output.print("")
  205.     end
  206.  
  207.     local s = string.format("[%s](%s/skill/%s/%s/practice)", t.short, host, learning_lang, t.url)
  208.     if t.short ~= t.title then s = s .. " - " .. t.title end
  209.     s = s .. string.format(" \\[[%s](%s/skill/%s/%s/test)\\]", tr("test", from_lang), host, learning_lang, t.url)
  210.     output.print("## " .. s)
  211.     output.print("")
  212.  
  213.     local expl = nil
  214.     if t.expl then
  215.         tips = tips + 1
  216.         expl = string.format("[%s](%s/skill/%s/%s/tips-and-notes)", tr("yes", from_lang), host, learning_lang, t.url)
  217.     else
  218.         expl = tr("no", from_lang)
  219.     end
  220.     output.printf("* %s: %s", tr("Tips and notes", from_lang), expl)
  221.     local nl = {}
  222.     do
  223.         local nl_map = {}
  224.         nl_map[1] = t.nless
  225.  
  226.         for k = 0, t.levels - 1 do
  227.             local n = nl_map[k] or -1
  228.             table.insert(nl, n)
  229.         end
  230.         if nl_map[t.levels] then nl.total = nl_map[t.levels] end
  231.     end
  232.     ensure_lessons_count(nl)
  233.     output.printf("* %s: %s", tr("Number of lessons", from_lang), lessons_to_string(nl))
  234.     output.printf("* %s: %s", tr("Words", from_lang), table.concat(t.words, ", "))
  235.     output.print("")
  236.  
  237.     for k = 1, #nl do
  238.         if nl[k] == -1 then
  239.             lcnt[k] = -1
  240.         elseif not lcnt[k] then
  241.             lcnt[k] = nl[k]
  242.         elseif lcnt[k] ~= -1 then
  243.             lcnt[k] = lcnt[k] + nl[k]
  244.         end
  245.     end
  246.     wcnt = wcnt + #t.words
  247. end
  248.  
  249. local vcnt = user.course_info().vocabulary_size() or -1
  250.  
  251. output.print("")
  252. output.print("----------")
  253. output.print("")
  254. output.printf("## **%s**", tr("Total", from_lang))
  255. output.print("")
  256. output.printf("- %s: %d", tr("Skills", from_lang), #skt)
  257. output.printf("- %s: %d/%d", tr("Words", from_lang), vcnt, wcnt)
  258. output.printf("- %s: %s", tr("Lessons", from_lang), lessons_to_string(lcnt))
  259. output.printf("- %s: %d (%.1f%%)", tr("Tips and Notes", from_lang), tips, tips / #skt * 100)
  260. output.print("")
  261. output.print("----------")
  262. output.print("")
  263.  
  264. output.print(tr("Other links:", from_lang))
  265. output.print("")
  266. output.print("- [List of Vocabularies for Language Courses of Duolingo](https://forum.duolingo.com/comment/31074292)")
  267. output.print("")
  268. output.print("----------")
  269. output.print("")
  270. output.printf(tr("Last update: %s", from_lang), os.date("%Y-%m-%d"))
  271.  
  272. output.set_stream()
  273.  
  274. return {}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement