|
6 | 6 | from twilio.rest import Client
|
7 | 7 |
|
8 | 8 | load_dotenv(os.path.join(os.path.abspath(os.path.dirname(__file__)), '.env'))
|
| 9 | +latestImgUrl = "" |
9 | 10 |
|
10 | 11 | class inky_display:
|
11 | 12 | HEIGHT = 104
|
@@ -37,46 +38,40 @@ def recolor(img):
|
37 | 38 | return inky_img
|
38 | 39 |
|
39 | 40 | def main():
|
40 |
| - #threading.Timer(120, main).start() |
| 41 | + threading.Timer(120, main).start() |
| 42 | + global latestImgUrl |
41 | 43 | try:
|
42 | 44 | incomingMsgs = client.messages.list(to=os.getenv('TWILIO_PHONE_NUMBER'))
|
43 | 45 | sortedMsgList = sorted(incomingMsgs, key=lambda msg:msg.date_sent, reverse=True)
|
44 | 46 | latestMediaMsg = getLatestMediaMsg(sortedMsgList)
|
45 |
| - latestImg = client.messages(latestMediaMsg.sid).media.list(limit=1) |
46 |
| - if (latestImg == client.messages(latestMediaMsg.sid).media.list(limit=1)): |
47 |
| - pass |
48 |
| - else: |
49 |
| - latestImg = client.messages(latestMediaMsg.sid).media.list(limit=1) |
50 |
| - latestImgUrl = ("https://api.twilio.com" + str(latestImg[0].uri)).strip(".json'").strip("u'") |
| 47 | + latestImg = client.messages(latestMediaMsg.sid).media.list(limit=1)[0] |
| 48 | + if (latestImgUrl != ("https://api.twilio.com" + str(latestImg.uri)).strip(".json'").strip("u'")): |
| 49 | + latestImgUrl = ("https://api.twilio.com" + str(latestImg.uri)).strip(".json'").strip("u'") |
51 | 50 | img = requests.get(latestImgUrl, stream=True).raw
|
| 51 | + # Resize and crop incoming image |
| 52 | + img = Image.open(img) |
| 53 | + img = img.rotate(90, expand=1) |
| 54 | + imgW = float(img.size[0]) |
| 55 | + imgH = float(img.size[1]) |
| 56 | + imgRatio = imgW/imgH |
| 57 | + if (imgRatio < float(inky_display.WIDTH)/float(inky_display.HEIGHT)): |
| 58 | + img = img.resize((inky_display.WIDTH, int(inky_display.WIDTH/imgRatio)), resample=Image.BILINEAR) |
| 59 | + imgHeightMargin = int((img.size[1]-inky_display.HEIGHT)/2) |
| 60 | + img = img.crop((0, imgHeightMargin, inky_display.WIDTH, inky_display.HEIGHT+imgHeightMargin)) |
| 61 | + else: |
| 62 | + img = img.resize((int(imgRatio*inky_display.HEIGHT), inky_display.HEIGHT), resample=Image.BILINEAR) |
| 63 | + imgWidthMargin = int((img.size[0]-inky_display.WIDTH)/2) |
| 64 | + img = img.crop((imgWidthMargin, 0, inky_display.WIDTH+imgWidthMargin, inky_display.HEIGHT)) |
| 65 | + img = img.convert(mode="P", dither=1, palette="ADAPTIVE", colors=256) |
| 66 | + img = img.convert(mode="RGB") |
| 67 | + img = ImageOps.posterize(img, bits=1) |
| 68 | + img = recolor(img) |
| 69 | + img.show() |
52 | 70 | except requests.exceptions.RequestException as e:
|
53 | 71 | print("Could not grab latest image:")
|
54 | 72 | print(e)
|
55 | 73 | sys.exit(1)
|
56 |
| - |
57 |
| - # Resize and crop incoming image |
58 |
| - img = Image.open(img) |
59 |
| - img = img.rotate(90, expand=1) |
60 |
| - imgW = float(img.size[0]) #1994 |
61 |
| - imgH = float(img.size[1]) #1496 |
62 |
| - imgRatio = imgW/imgH #1.33 |
63 |
| - |
64 |
| - if (imgRatio < float(inky_display.WIDTH)/float(inky_display.HEIGHT)): |
65 |
| - img = img.resize((inky_display.WIDTH, int(inky_display.WIDTH/imgRatio)), resample=Image.BILINEAR) |
66 |
| - imgHeightMargin = int((img.size[1]-inky_display.HEIGHT)/2) |
67 |
| - img = img.crop((0, imgHeightMargin, inky_display.WIDTH, inky_display.HEIGHT+imgHeightMargin)) |
68 |
| - else: |
69 |
| - img = img.resize((int(imgRatio*inky_display.HEIGHT), inky_display.HEIGHT), resample=Image.BILINEAR) |
70 |
| - imgWidthMargin = int((img.size[0]-inky_display.WIDTH)/2) |
71 |
| - img = img.crop((imgWidthMargin, 0, inky_display.WIDTH+imgWidthMargin, inky_display.HEIGHT)) |
72 |
| - |
73 |
| - img = img.convert(mode="P", dither=1, palette="ADAPTIVE", colors=256) |
74 |
| - img = img.convert(mode="RGB") |
75 |
| - img = ImageOps.posterize(img, bits=1) |
76 |
| - img = recolor(img) |
77 |
| - img.show() |
78 | 74 |
|
79 |
| - |
80 | 75 | # Initialize Twilio client
|
81 | 76 | client = Client(os.getenv('TWILIO_ACCOUNT_SID'), os.getenv('TWILIO_AUTH_TOKEN'))
|
82 | 77 |
|
|
0 commit comments