{"id":1842,"date":"2024-03-21T00:48:21","date_gmt":"2024-03-21T00:48:21","guid":{"rendered":"https:\/\/soliloquyforthefallen.net\/?p=1842"},"modified":"2024-03-21T01:18:06","modified_gmt":"2024-03-21T01:18:06","slug":"puking-up-a-code-sample-in-python","status":"publish","type":"post","link":"https:\/\/soliloquyforthefallen.net\/?p=1842","title":{"rendered":"Puking up a Code Sample in Python"},"content":{"rendered":"<p>I hate python, it&#8217;s not friendly to my eyes.\u00a0 That being said the compiler is super helpful.\u00a0 And yes, I&#8217;m still alive despite the best efforts of &#8230;. myself.<\/p>\n<p>I&#8217;m throwing together a raspberry pi zero powered uptime checker and an <a href=\"https:\/\/www.amazon.com\/gp\/product\/B08MPTND1V\/\">i2c backpack lcd display<\/a>. I didn&#8217;t want to write my own library, and I wanted this to get thrown together as fast and easily as possible. Thus, I followed <a href=\"https:\/\/www.circuitbasics.com\/raspberry-pi-i2c-lcd-set-up-and-programming\/\">this guide using python<\/a>. Python, ick.<\/p>\n<p>But I&#8217;m not posting this to hate on anything. If I post more info on this, then I&#8217;ll share my logic for things. Anyway, one of the reasons for this display is an easy was to check the status of my DMR repeater. Here is the basic code I banged together to use the Project Halligan API that Brandmeister provides to check the status of my repeater.<\/p>\n<p>Of note: Brandmeister does not provide an API call that tells you if the repeater is up or down. The website checks the last seen time and if it&#8217;s been more then 15 minutes it assumes that repeater is down. Checking against &#8220;status&#8221; or &#8220;status_text&#8221; will not tell you if the repeater is up or down merely what timeslots are linked &#8211; whether the repeater is connected or not.<\/p>\n<pre>\r\n<pre><code >\r\nimport urllib.request, json\r\nfrom datetime import datetime, timezone, timedelta\r\nfrom email.utils import formatdate\r\n\r\ntry:\r\n\turl = urllib.request.urlopen(\"https:\/\/api.brandmeister.network\/v2\/device\/313051\")\r\nexcept urllib.error.URLError as e:\r\n\tResponseData = e.reason\r\n\tprint (ResponseData)\r\nelse:\r\n\tlastSeen= (json.load(url)['last_seen'])\r\n\tprint (lastSeen)\r\n\ttimeOutTime = datetime.now(timezone.utc) - timedelta(minutes=15)\r\n\ttimeOutString = timeOutTime.strftime(\"%Y-%m-%d %H:%M:%S\")\r\n\tprint (timeOutString)\r\n\r\n\tif timeOutString < lastSeen:\r\n\t\tprint (\"Repeater up\")\r\n\telse:\r\n\t\tprint (\"Repeater Down\")\r\n<\/pre>\n<p><\/code><\/pre><\/p>\n<p>This code isn't pretty but it does work, on my machine at least. The post however, is most certainly honest.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I hate python, it&#8217;s not friendly to my eyes.\u00a0 That being said the compiler is super helpful.\u00a0 And yes, I&#8217;m still alive despite the best efforts of &#8230;. myself. I&#8217;m throwing together a raspberry pi zero powered uptime checker and an i2c backpack lcd display. I didn&#8217;t want to write my own library, and I&#8230; <\/p>\n<div class=\"read-more navbutton\"><a href=\"https:\/\/soliloquyforthefallen.net\/?p=1842\">Read More<i class=\"fa fa-angle-double-right\"><\/i><\/a><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[117,116],"tags":[],"_links":{"self":[{"href":"https:\/\/soliloquyforthefallen.net\/index.php?rest_route=\/wp\/v2\/posts\/1842"}],"collection":[{"href":"https:\/\/soliloquyforthefallen.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/soliloquyforthefallen.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/soliloquyforthefallen.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/soliloquyforthefallen.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1842"}],"version-history":[{"count":7,"href":"https:\/\/soliloquyforthefallen.net\/index.php?rest_route=\/wp\/v2\/posts\/1842\/revisions"}],"predecessor-version":[{"id":1851,"href":"https:\/\/soliloquyforthefallen.net\/index.php?rest_route=\/wp\/v2\/posts\/1842\/revisions\/1851"}],"wp:attachment":[{"href":"https:\/\/soliloquyforthefallen.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1842"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/soliloquyforthefallen.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1842"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/soliloquyforthefallen.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1842"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}