ctucx.git: travelynx2fedi

Automaticly post travelynx checkins on the fediverse

1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
27 
28 
29 
30 
31 
32 
33 
34 
35 
36 
37 
38 
39 
40 
41 
42 
43 
44 
45 
46 
47 
48 
49 
50 
import std/[nativesockets, options]
import std/[parsecfg, parseutils, strutils]

import threadvars, types

proc parseConfig* (configFile: string): types.Config =
  try:
    let config = loadConfig(configFile)

    if config.getSectionValue("server", "Port") != "":
      result.ServerPort = Port(config.getSectionValue("server", "Port").parseInt)
    else:
      result.ServerPort = Port(8080)

    if config.getSectionValue("server", "accessToken") != "":
      result.ServerAccessToken = some(config.getSectionValue("server", "accessToken"))

    if config.getSectionValue("travelynx", "username") != "":
      result.TravelynxuserName = some(config.getSectionValue("travelynx", "username"))

    if config.getSectionValue("fedi", "url") != "":
      result.FediURL = config.getSectionValue("fedi", "url")
    else:
      echo "Configparser: [fedi]url not specified!"
      quit(1)

    if config.getSectionValue("fedi", "accessToken") != "":
      result.FediAccessToken = config.getSectionValue("fedi", "accessToken")
    else:
      echo "Configparser: [fedi]accessToken not specified!"
      quit(1)

    if config.getSectionValue("fedi", "visibility") != "":
      result.FediVisibility = some(parseEnum[PostVisibility](config.getSectionValue("fedi", "visibility")))

    if config.getSectionValue("fedi", "spoilerText") != "":
      result.FediSpoilerText = some(config.getSectionValue("fedi", "spoilerText"))

    if config.getSectionValue("fedi", "useMarkdown") != "":
      result.FediUseMarkdown = config.getSectionValue("fedi", "useMarkdown").parseBool
    else:
      result.FediUseMarkdown = false

  except CatchableError:
    let
      e   = getCurrentException()
      msg = getCurrentExceptionMsg()

    echo "Couldn't parse config file:"
    echo "Got exception ", repr(e), " with message ", msg