Browse Source

Initial commit

Petra Lamborn 5 years ago
commit
6df25cb6c4
4 changed files with 13711 additions and 0 deletions
  1. 2
    0
      .gitignore
  2. 41
    0
      Readme.md
  3. 44
    0
      bot.py
  4. 13624
    0
      wl.txt

+ 2
- 0
.gitignore View File

@@ -0,0 +1,2 @@
1
+*.secret
2
+log.txt

+ 41
- 0
Readme.md View File

@@ -0,0 +1,41 @@
1
+# Roboto por esperanta vortaro
2
+
3
+("_Robot for an Esperanto dictionary_")
4
+
5
+This is a Mastodon bot that posts entries from a [English-Esparanto dictionary](http://www.gutenberg.org/ebooks/16967). It is hosted [@vortaro@botsin.space](https://botsin.space/@vortaro), and uses the [Mastodon.py](https://github.com/halcy/Mastodon.py) Python wrapper.
6
+
7
+This is my first bot for either Twitter or Mastodon, and my first python program in a long time. It's not perfect, but it works.
8
+
9
+## Source
10
+
11
+The bot takes its wordlist from the utf-8 version of the above-linked dictionary, editing out the section headings and other elements that do not fit the pattern "<English> = <Esperanto>.", e.g:
12
+
13
+> Bed (river) = kuŝujo.
14
+
15
+A handful of words I'd rather not have appear are removed, along with the first entry which for posterity reads:
16
+
17
+> A = indefinite article, not used in Esperanto.
18
+
19
+This is a very important feature of the language! It just doesn't fit the formulae I use.
20
+
21
+## Usage
22
+
23
+Obviuosly you're more likely to use this to make your own bot, but supposing you were going to deploy an exact copy this is what you would do:
24
+
25
+1. Make an account on [botsin.space](https://botsin.space), or another Mastodon instance. Mark it as a bot and do all the other profile things.
26
+2. Under Settings->Development create a new application, give it a name etc, leave the permissions as they are, and submit.
27
+3. Clone this repository onto whatever computer you want to run the app.
28
+    * If the account you made is not on botsin.space, edit `bot.py` to change the `api_base_url` to the URL of your instance.
29
+4. Using pip, download the Mastodon.py API interface. For example, `pip3 install --user Mastodon.py`.
30
+5. Back on the website, find the application you created under Settings->Development->Your Applications, and copy its access token into a file named `vortaro.secret` in the same directory as `bot.py` _etc_.
31
+6. Run your app with `python3 bot.py`, or equivalent. If all is successful it will print to `stdout` as well as tooting.
32
+
33
+## cron
34
+
35
+I run the bot via `cron`. Remember: if you have a line like
36
+
37
+```
38
+0 * * * * /usr/bin/python3 /home/petra/vortaro/bot.py
39
+```
40
+
41
+in your crontab, it's not going to run the bot in the right directory to find the files it needs; instead it will fail silently. Personally I use a wrapper script that `cd`s into the directory.

+ 44
- 0
bot.py View File

@@ -0,0 +1,44 @@
1
+#! /usr/bin/python3
2
+import random
3
+from mastodon import Mastodon
4
+
5
+dformats = [
6
+		"English: \'{}.\'\nEsperanto: \'{}\'",
7
+		"English: \'{}.\'\nEsperanto: \'{}\'",
8
+		"English: \'{}.\'\nEsperanto: \'{}\'",
9
+		"English: \'{}.\'\nEsperanto: \'{}\'",
10
+		"English: \'{}.\'\nEsperanto: \'{}\'",
11
+		"Instead of \'{}\', try saying \'{}\'",
12
+		"The Esperanto for \'{}\' is \'{}\'",
13
+		"The Esperanto for \'{}\' is \'{}\'",
14
+		"The Esperanto for \'{}\' is \'{}\'",
15
+		"{} = {}",
16
+		"{} = {}",
17
+		"Esperanto: \'{1}\'\nEnglish: \'{0}.\'"
18
+	]
19
+
20
+with open("wl.txt", encoding="utf-8-sig") as f:
21
+    content = f.readlines()
22
+
23
+wl = [x.strip() for x in content]
24
+
25
+l = len(wl)
26
+dl = len(dformats)
27
+r = random.randint(0, l - 1)
28
+dr = random.randint(0, dl - 1)
29
+
30
+wpair = wl[r].split(" = ")
31
+eng = wpair[0]
32
+esp = wpair[1].capitalize()
33
+
34
+fstr = dformats[dr]
35
+tstr = fstr.format(eng, esp)
36
+print(tstr)
37
+
38
+
39
+mastodon = Mastodon(
40
+    access_token = "vortaro.secret",
41
+    api_base_url = "https://botsin.space"
42
+)
43
+
44
+mastodon.toot(tstr)

+ 13624
- 0
wl.txt
File diff suppressed because it is too large
View File