Author Topic: Feature request: offer flags for languages on the main page  (Read 1551 times)

January 13, 2021, 12:30:07 AM
Read 1551 times

BoredPanda

  • *
  • Information
  • Novice reader
  • Posts: 7
Feature request: offer flags for languages on the main page
« on: January 13, 2021, 12:30:07 AM »
As this thread: http://kx5thpx2olielkihfyo4jgjqfb7zx7wxr3sd4xzt26ochei4m6f7tayd.onion/forum/index.php?topic=3151.0 shows, while English is dominating, there are books available in other languages.

While one can use search find them, it might not be obvious to less experienced/proficient users.

Therefore, I'd love to suggest a bar on the homepage, under the main navigation, that would offer flags for any language having more than X entries (10? 100?) that would just redirect to search with that language selected.

January 13, 2021, 06:11:06 PM
Reply #1

BoredPanda

  • *
  • Information
  • Novice reader
  • Posts: 7
Re: Feature request: offer flags for languages on the main page
« Reply #1 on: January 13, 2021, 06:11:06 PM »
I got bored enough to do a quick PoC of the list (linking to clearweb): https://trantor-is-languages.vercel.app/

February 08, 2021, 07:48:54 PM
Reply #2

Las Zenow

  • *****
  • Information
  • Avid reader
  • Chief Librarian
  • Posts: 273
  • You just leave it to the Library.
    • Imperial Library of Trantor
Re: Feature request: offer flags for languages on the main page
« Reply #2 on: February 08, 2021, 07:48:54 PM »
Ups, the PoC gives me a 404. I guess is because being so late on looking at it (sorry).

The idea sounds great. How proficient are you with html? Do you feel like trying to send a merge request?
https://gitlab.com/trantor/trantor/-/tree/master/templates

I'm happy to help you to navigate the code and understand what is what, or if you give me access to the PoC again I can try to integrate it in our code.
Happy reading!!!

February 08, 2021, 11:34:40 PM
Reply #3

BoredPanda

  • *
  • Information
  • Novice reader
  • Posts: 7
Re: Feature request: offer flags for languages on the main page
« Reply #3 on: February 08, 2021, 11:34:40 PM »
Nah, that's due to the fact that it rebuilds every night and sometimes the clearweb is not accessible.

The code is crude, but hey:
package.json:
Code: [Select]
{
  "name": "trantor.is-langs",
  "version": "1.0.0",
  "main": "index.js",
  "author": "X",
  "license": "MIT",
  "dependencies": {
    "axios": "^0.21.1",
    "axios-cookiejar-support": "^1.0.1",
    "tough-cookie": "^4.0.0"
  },
  "scripts": {
    "build": "node index.js"
  }
}

index.js:
Code: [Select]
const fs = require('fs')
const axios = require('axios').default
const axiosCookieJarSupport = require('axios-cookiejar-support').default
const tough = require('tough-cookie')

const codes = [
  'af',
  'ar',
  'bg',
  'br',
  'bs',
  'ca',
  'ceb',
  'cs',
  'cy',
  'da',
  'de',
  'el',
  'en',
  'eo',
  'es',
  'et',
  'fa',
  'fi',
  'fr',
  'fy',
  'ga',
  'gd',
  'gl',
  'he',
  'hi',
  'hr',
  'hu',
  'ia',
  'id',
  'is',
  'it',
  'ja',
  'ko',
  'la',
  'lt',
  'mi',
  'ml',
  'ms',
  'nl',
  'nn',
  'no',
  'oc',
  'pl',
  'po',
  'pt',
  'ro',
  'ru',
  'sa',
  'sco',
  'sk',
  'sl',
  'sr',
  'sv',
  'ta',
  'te',
  'tl',
  'tr',
  'uk',
  'und',
  'vi',
  'xh',
  'zh',
]

const baseQueryParams = '?num=1&fmt=json&q=lang%3A'

axiosCookieJarSupport(axios)
const cookieJar = new tough.CookieJar()
const client = axios.create({
  baseURL: 'https://trantor.is/search/',
  jar: cookieJar,
  withCredentials: true,
  headers: {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36',
  },
})

let cache = {}
const cachePath = __dirname + '/dist/data.json'

if (fs.existsSync(cachePath)) {
  cache = require(cachePath)
}

const saveLangs = (langs, refreshTime) => {
  let langsHtml = ''

  langs.forEach((lang) => {
    langsHtml += `<div class="languageBox"><a href="https://trantor.is/search/?q=lang:${lang.code}" target="_blank" class="langLink">${lang.code} (${lang.count})</a></div>`
  })

  const bases = {
    onion: 'http://kx5thpx2olielkihfyo4jgjqfb7zx7wxr3sd4xzt26ochei4m6f7tayd.onion/',
    clearweb: 'https://trantor.is/',
  }

  const html = `<!doctype html>

  <html lang="en">
  <head>
    <meta charset="utf-8">
 
    <title>Languages of Trantor.is</title>
    <meta name="description" content="Languages of Trantor.is">
    <link rel="preconnect" href="https://fonts.gstatic.com">
    <link href="https://fonts.googleapis.com/css2?family=Roboto:wght@300&display=swap" rel="stylesheet">

    <style>
      *, html, body {
        font-family: 'Roboto', sans-serif;
      }

      .languageBox {
        padding: 10px;
        border: 1px solid #000;
        margin: 10px;
      }

      .container {
        max-width: 800px;
        margin: 20px auto;
      }

      .languageSwitch {
        cursor: pointer;
      }
    </style>
    <script>
      const bases = {
        onion: 'http://kx5thpx2olielkihfyo4jgjqfb7zx7wxr3sd4xzt26ochei4m6f7tayd.onion/',
        clearweb: 'https://trantor.is/',
      }

      const switchBase = (to) => {
       
      }
    </script>
  </head>
 
    <body>
    <div class="container">
      <h2>Languages available on <a href="https://trantor.is/" class="langLink" target="_blank">Imperial Library of Trantor</a> [sorted by frequency]</h2>
      <div>
        Select link version:<br />
        * <a class="languageSwitch" data-to="onion">Onion [${bases.onion}]</a><br />
        * <a class="languageSwitch" data-to="clearweb">Clearweb [${bases.clearweb}]</a>
      </div>
      <div style="display: flex; flex-wrap: wrap; justify-content: space-between;">
        ${langsHtml}
      </div>
      ${refreshTime ? `<div>Last refresh: ${new Date().toUTCString()}</div>` : ''}
      <div>If you want the data, see: <a href="data.json" target="_blank">data.json</a> file</div>
    </div>

    <script>
      document.querySelectorAll('.languageSwitch').forEach(languageSwitch =>
        languageSwitch.addEventListener('click', (e) => {
          const to = languageSwitch.dataset.to
          const from = to === 'onion' ? 'clearweb' : 'onion'
          document.querySelectorAll('.langLink').forEach(link => link.href = link.href.replace(bases[from], bases[to]))
        })
      )
    </script>
  </body>
  </html>`

  fs.writeFileSync(__dirname + '/dist/index.html', html)
}

const saveCache = () => {
  fs.writeFileSync(cachePath, JSON.stringify(cache, null, 2))
}

const generateLanguages = async () => {
  let langs = []

  for (let i = 0; i < codes.length; i++) {
    const code = codes[i]

    if (cache[code]) {
      if (cache[code].time > new Date().getTime() - 3600 * 1000) {
        langs.push(cache[code].data)
        continue
      }
    }

    console.log(`Obtaining data for language: ${code}`)

    let json
    try {
      json = await client.get(baseQueryParams + code)
    } catch (err) {
      console.error(err.message)
      throw err
    }

    langs.push({ code, count: json.data.found })
    cache[code] = {
      time: new Date().getTime(),
      data: { code, count: json.data.found },
    }

    saveCache()
  }

  saveCache()

  langs.sort((a, b) => b.count - a.count)
  saveLangs(langs, true)
}

generateLanguages()

February 09, 2021, 07:33:49 PM
Reply #4

Las Zenow

  • *****
  • Information
  • Avid reader
  • Chief Librarian
  • Posts: 273
  • You just leave it to the Library.
    • Imperial Library of Trantor
Re: Feature request: offer flags for languages on the main page
« Reply #4 on: February 09, 2021, 07:33:49 PM »
Thanks for the mock up. I don't think we'll do it in javascript, but I'll copy some of the ideas/code from it.
Happy reading!!!

February 11, 2021, 12:01:52 AM
Reply #5

BoredPanda

  • *
  • Information
  • Novice reader
  • Posts: 7
Re: Feature request: offer flags for languages on the main page
« Reply #5 on: February 11, 2021, 12:01:52 AM »
Sure, I understand that. Keep in mind I have no access to the DB, so I had to use the search.

BTW. Here's how it look like: