Skip to content

Instantly share code, notes, and snippets.

@gotbadger
Last active May 25, 2018 09:37
Show Gist options
  • Save gotbadger/629009625914af5c98597eacf753cf78 to your computer and use it in GitHub Desktop.
Save gotbadger/629009625914af5c98597eacf753cf78 to your computer and use it in GitHub Desktop.
Open Graph Receipt Printer
import sys
import os
import time
from ftfy import fix_text
from opengraph import OpenGraph
import urllib.request
from escpos import printer
if len(sys.argv) != 2:
sys.exit('Usage: python3 main.py https://www.instagram.com/p/some_image')
og = OpenGraph(url=sys.argv[1])
file_name, headers = urllib.request.urlretrieve(og.image)
print(file_name)
# call image magick to update file in place
# cmd = 'mogrify -colorspace Gray -ordered-dither h4x4a -resize 512 %s' % file_name
cmd = 'mogrify -colorspace Gray -resize 512 -unsharp 0x1 %s' % file_name
os.system(cmd)
# make an attempt to shorten the title and make it printable
title = ascii(fix_text(og.title.split('Instagram post by ').pop().split('Instagram: ').pop())).replace('\\u2022','-').replace('\'','')
print(title)
print(og.url)
p = printer.File("/dev/usb/lp0")
p.text(title)
p.text("\n")
time.sleep(2)
p.image(file_name)
time.sleep(2)
p.text("\n")
p.text(og.url)
p.cut()
urllib.request.urlcleanup()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment