{"id":9711,"date":"2023-04-14T13:36:00","date_gmt":"2023-04-14T18:36:00","guid":{"rendered":"https:\/\/richard.rathe.org\/?p=9711"},"modified":"2024-09-29T14:36:37","modified_gmt":"2024-09-29T19:36:37","slug":"wisdom-bot-for-mastodon","status":"publish","type":"post","link":"https:\/\/redmangrove.org\/rathe.org\/richard\/2023\/wisdom-bot-for-mastodon","title":{"rendered":"&#8220;Wisdom&#8221; Bot for Mastodon"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">I left the Birdsite about six months ago when the new owner started mucking about.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"600\" height=\"399\" src=\"https:\/\/richard.rathe.org\/wp-content\/uploads\/2023\/04\/this-is-fine-bird.png\" alt=\"\" class=\"wp-image-9723\" style=\"width:290px;height:193px\" srcset=\"https:\/\/redmangrove.org\/rathe.org\/richard\/wp-content\/uploads\/2023\/04\/this-is-fine-bird.png 600w, https:\/\/redmangrove.org\/rathe.org\/richard\/wp-content\/uploads\/2023\/04\/this-is-fine-bird-376x250.png 376w, https:\/\/redmangrove.org\/rathe.org\/richard\/wp-content\/uploads\/2023\/04\/this-is-fine-bird-150x100.png 150w\" sizes=\"auto, (max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/figure>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\">No regrets! <a href=\"https:\/\/universeodon.com\/@richardrathe\" target=\"_blank\" rel=\"noreferrer noopener\">Mastodon<\/a> has been so much better! I decided to try my hand at creating an <em><a href=\"https:\/\/botsin.space\/@wisdom\" target=\"_blank\" rel=\"noreferrer noopener\">automated &#8220;robot&#8221;<\/a><\/em> to post quotations alongside some of my photographs.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><img decoding=\"async\" src=\"https:\/\/richard.rathe.org\/wp-content\/uploads\/2023\/04\/wisdom-in-space.png\" alt=\"\" class=\"wp-image-9712\" style=\"width:705px;height:641px\"\/><figcaption class=\"wp-element-caption\"><em>Wisdom in Space Posts Every Four Hours<\/em><\/figcaption><\/figure>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\">Mastodon is much more civilized and well organized\u2014including best practices for social media bots. There is even a server dedicated to bots called &#8220;botsin.space&#8221;&#8211;so I called my new bot &#8220;<a rel=\"noreferrer noopener\" href=\"https:\/\/botsin.space\/@wisdom\" target=\"_blank\">Wisdom in Space<\/a>&#8220;.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">There are many helpful websites to get started. Most use the <em>mastodon<\/em> interface package for <em>Python<\/em>. This is the first time I&#8217;ve written code in the python language, but fortunately the learning curve isn&#8217;t very steep. The biggest conceptual hurdle for me was understanding and using <em>virtual environments<\/em> on the shared server. Once I got those pesky details worked out I set up a<em> cron jo<\/em>b to post every four hours. The quotes are in a tab-delimited text file and the images are in a nearby directory. Note the development code below works locally on my laptop. The directory paths and other details are different on the server. I hope someone finds this useful!<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#\n#\twisdombot 0.2\n#\tquote of the day service for mastodon\n#\tcopyright richard rathe 2023\n#\t(cc) attrib-noncomm-share-alike\n#\n\nimport re\n\nimport glob\n\nimport random\n\nfrom mastodon import Mastodon\n\nquote_list = &#91;]\n\nwith open('wisdom.dat') as file:\n\n\tfor line in file:\n\n\t\tline = line&#91;:-1]  # remove newline\n\n\t\tquote_list.append(line)\n\n\nqmax = str(len(quote_list))\n\nimage_list = glob.glob('images\/*.jpg')\n\nimax = str(len(image_list))\n\nqrand = random.randrange(int(qmax))\n\nline = quote_list&#91;qrand]\n\nline = line.replace('&amp;mdash;'  , '--')\t# fix entities\nline = line.replace('&amp;quot;'   , '\\\"')\nline = line.replace('&amp;eacute;' , 'e')\nline = line.replace('&amp;oslash;' , 'o')\nline = line.replace('&amp;ouml;'   , 'o')\nline = line.replace('&amp;amp;'    , '&amp;')\n\ndata_list = line.split('\\t\\t')\t\t# double tab delimited\n\nirand = random.randrange(int(imax))\t# pick image at random\n\nimg = image_list&#91;irand]\n\nmatch = re.search(r'^images\/(&#91;a-z0-9-]+)', img, re.I)\n\nif match:\n\tstem = match.group(1)\nelse:\n\tstem = ''\n\nikeyw = ''\n\nfor i in stem.split('-'):\n\ti = i&#91;0].upper() + i&#91;1:]\t# capitalize\n\tikeyw += \" #\" + i\n\nikeyw = ikeyw&#91;1:]\t# nuke leading space\n\nkeyw = ''\n\nfor i in data_list&#91;2].split('\/'):\t# keywords are '\/' delimited\n\ti = i.replace(' ', '')\n\ti = i.replace('-', '')\n\tkeyw += \" #\" + i\n\nkeyw = keyw&#91;1:]\t\t# nuke leading space\n\nquote = data_list&#91;3]\n\nname = data_list&#91;0]\t\t\t\t# author name\n\nntag = re.sub('&#91;^0-9a-zA-Z]+', '', name)\t# author hashtag\nntag = '#' + ntag\n\nother = data_list&#91;1]\t\t\t\t# source info (if available)\n\nif other == 'None':\n\tother = '';\nelse:\n\tother = ' (' + other + ')'\n\nout = quote + '\\n    -- ' + name + other + '\\n\\n'\n\nout += '\u2b06 #Quotes ' + ntag + ' ' + keyw + '\\n\\n\u2b07 #Photography ' + ikeyw\n\nif len(out) &lt; 500:\t\t# limited to 500 chars\n\n\tprint('\\n' + out + '\\n')\n\n\tmastodon = Mastodon(\n\t\taccess_token = 'token.secret',\n\t\tapi_base_url = 'https:\/\/botsin.space\/'\n\t)\n\n\tmedia = mastodon.media_post(img, description=\"photo by richard rathe\")\n \tmastodon.status_post(out, media_ids=&#91;media])\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I left the Birdsite about six months ago when the new owner started mucking about. No regrets! Mastodon has been so much better! I decided to try my hand at creating an automated &#8220;robot&#8221; to post quotations alongside some of my photographs. Mastodon is much more civilized and well organized\u2014including best practices for social media &hellip; <a href=\"https:\/\/redmangrove.org\/rathe.org\/richard\/2023\/wisdom-bot-for-mastodon\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;&#8220;Wisdom&#8221; Bot for Mastodon&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14,32,18],"tags":[516,515,517,513],"class_list":["post-9711","post","type-post","status-publish","format-standard","hentry","category-photography","category-programming","category-technology","tag-mastodon","tag-python-programming","tag-quotations","tag-social-media"],"_links":{"self":[{"href":"https:\/\/redmangrove.org\/rathe.org\/richard\/wp-json\/wp\/v2\/posts\/9711","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/redmangrove.org\/rathe.org\/richard\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/redmangrove.org\/rathe.org\/richard\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/redmangrove.org\/rathe.org\/richard\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/redmangrove.org\/rathe.org\/richard\/wp-json\/wp\/v2\/comments?post=9711"}],"version-history":[{"count":15,"href":"https:\/\/redmangrove.org\/rathe.org\/richard\/wp-json\/wp\/v2\/posts\/9711\/revisions"}],"predecessor-version":[{"id":10151,"href":"https:\/\/redmangrove.org\/rathe.org\/richard\/wp-json\/wp\/v2\/posts\/9711\/revisions\/10151"}],"wp:attachment":[{"href":"https:\/\/redmangrove.org\/rathe.org\/richard\/wp-json\/wp\/v2\/media?parent=9711"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/redmangrove.org\/rathe.org\/richard\/wp-json\/wp\/v2\/categories?post=9711"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/redmangrove.org\/rathe.org\/richard\/wp-json\/wp\/v2\/tags?post=9711"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}