Browse Source

added reddit quotes

Tobias Simetsreiter 4 years ago
parent
commit
20557423a3
5 changed files with 114 additions and 13 deletions
  1. 1 0
      .gitignore
  2. 0 0
      runtime/.gitignore
  3. 35 1
      simpilib/__init__.py
  4. 61 12
      simpilib/eink.py
  5. 17 0
      simpilib/runtime.py

+ 1 - 0
.gitignore

@@ -58,4 +58,5 @@ docs/_build/
 # PyBuilder
 target/
 wavesharelib
+runtime
 config.mk

+ 0 - 0
runtime/.gitignore


+ 35 - 1
simpilib/__init__.py

@@ -32,7 +32,12 @@ def wrapText(text, line_width_factor=5):
         splittext = textwrap.wrap(text, width=iter_width)
         longestline = getLongestLine(splittext)
         print("Wrap:", iter_width, len(splittext))
-    return "\n".join(splittext), longestline
+    return "\n".join(splittext)
+
+def fitText(image, draw, fontpath, text):
+    text = wrapText(text, line_width_factor=4.5)
+    font = loadFontSize(text, draw, image, fontpath)
+    return text, font
 
 
 def getLongestLine(split):
@@ -59,3 +64,32 @@ def getQuote( maxLength=100 ):
     except Exception as ex:
         return
     return text
+
+def getRedditJson(reddit,res,interval):
+    import requests
+    from urllib.parse import urlencode
+    base_url = f"https://www.reddit.com/r/{reddit}/{res}.json"
+    headers = {
+        'User-agent': 'raspi-zero-waveshare'
+    }
+    params = urlencode({
+        "t": interval,
+    })
+    url = base_url + '?' + str(params)
+    print(url)
+    r = requests.get(url, headers=headers)
+    return r.json()
+
+def short_url(text):
+    import requests
+    from urllib.parse import urlencode
+    try:
+        params = urlencode({
+            "url": text,
+        })
+        r = requests.get('https://tinyurl.com/api-create.php?'+str(params))
+        t = r.text
+    except Exception as ex:
+        t = str(ex)[:100]
+    return t
+

+ 61 - 12
simpilib/eink.py

@@ -19,20 +19,69 @@ def eink_qr(args):
     epd.Dev_exit()
 
 def eink_qr_shorturl(args):
-    import requests
-    from urllib.parse import urlencode
-    try:
-        params = urlencode({
-            "url": args.text,
-        })
-        r = requests.get('https://tinyurl.com/api-create.php?'+str(params))
-        args.text = r.text
-    except Exception as ex:
-        args.text = str(ex)[:100]
+    import simpilib
+    args.text = simpilib.short_url(args.text)
     eink_qr(args)
 
 def eink_quote_qr(args):
-    pass
+    import simpilib
+    import random
+    data = simpilib.getRedditJson("quotes","top","day")
+    quotes = data["data"]["children"]
+    q = random.choice(quotes)
+    # args.text = q["data"]["title"]
+    # eink_text(args)
+    text = q["data"]["title"]
+    url = simpilib.short_url(q["data"]["url"])
+
+    smalltext = None
+    if text.find(" - ") > 0:
+        pos = text.rfind("-")
+        text = text[:pos]
+        smalltext = text[pos+1:]
+
+    print(text)
+    print(smalltext)
+    eink_text_qr(q["data"]["title"], url, smalltext)
+
+def eink_text_qr(text, qrdata, smalltext=None):
+    import simpilib
+    from waveshare_epd import epd2in13_V2
+    from PIL import Image,ImageDraw
+    import qrcode
+    epd = epd2in13_V2.EPD()
+
+    base_image = Image.new('1', (epd.height, epd.width), 0)
+    qr = qrcode.QRCode(version=1, box_size=3, border=1)
+    qr.add_data(qrdata)
+    qr.make()
+    pic = qr.make_image().get_image()
+
+    text_image = Image.new('1', (base_image.width-pic.width, base_image.height), 0)
+    small_image = Image.new('1', (pic.width, base_image.height-pic.height), 0)
+
+    base_image.paste(pic, (base_image.width-pic.width,0), pic)
+    draw = ImageDraw.Draw(base_image)
+
+    epd.init(epd.FULL_UPDATE)
+    epd.displayPartBaseImage(epd.getbuffer(base_image))
+
+    epd.init(epd.PART_UPDATE)
+
+    text = simpilib.wrapText(text, line_width_factor=4.5)
+    font = simpilib.loadFontSize(text, draw, text_image, fontpath)
+
+    draw.text((0,0), text, align="center", font = font, fill = 255)
+    epd.displayPartial(epd.getbuffer(base_image))
+
+    if smalltext != None and len(smalltext) > 2:
+        smalltext = simpilib.wrapText(smalltext, line_width_factor=10)
+        smallfont = simpilib.loadFontSize(text, draw, small_image, fontpath)
+        draw.text((base_image.width-pic.width,0), smalltext, align="center", font = font, fill = 255)
+        epd.displayPartial(epd.getbuffer(base_image))
+
+    epd.sleep()
+    epd.Dev_exit()
 
 def eink_quote(args):
     import simpilib
@@ -54,7 +103,7 @@ def eink_text(args):
     image = Image.new('1', (epd.height, epd.width), 0)
     draw = ImageDraw.Draw(image)
 
-    text, longline = simpilib.wrapText(args.text, line_width_factor=7)
+    text = simpilib.wrapText(args.text, line_width_factor=7)
     font = simpilib.loadFontSize(text, draw, image, fontpath)
 
     w, h = draw.textsize(text, font=font)

+ 17 - 0
simpilib/runtime.py

@@ -0,0 +1,17 @@
+
+import os as _os
+runtime_dir = _os.path.realpath(_os.path.join(__file__,'..','..','runtime'))
+config_file = _os.path.join(runtime_dir, "config.json")
+
+def load_config():
+    import json
+    if _os.path.isfile(config_file):
+        with open(config_file) as fd:
+            return json.load(fd)
+    else:
+        return {}
+
+def save_config():
+    import json
+    with open(config_file, "w") as fd:
+        return json.dump(fd)