Skip to content

Commit 3f5f511

Browse files
Rachel WeilRachel Weil
authored andcommitted
Re-upping test to match file
1 parent d7b0d41 commit 3f5f511

File tree

1 file changed

+25
-30
lines changed

1 file changed

+25
-30
lines changed

yesme-test.py

Lines changed: 25 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from twilio.rest import Client
77

88
load_dotenv(os.path.join(os.path.abspath(os.path.dirname(__file__)), '.env'))
9+
latestImgUrl = ""
910

1011
class inky_display:
1112
HEIGHT = 104
@@ -37,46 +38,40 @@ def recolor(img):
3738
return inky_img
3839

3940
def main():
40-
#threading.Timer(120, main).start()
41+
threading.Timer(120, main).start()
42+
global latestImgUrl
4143
try:
4244
incomingMsgs = client.messages.list(to=os.getenv('TWILIO_PHONE_NUMBER'))
4345
sortedMsgList = sorted(incomingMsgs, key=lambda msg:msg.date_sent, reverse=True)
4446
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'")
5150
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()
5270
except requests.exceptions.RequestException as e:
5371
print("Could not grab latest image:")
5472
print(e)
5573
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()
7874

79-
8075
# Initialize Twilio client
8176
client = Client(os.getenv('TWILIO_ACCOUNT_SID'), os.getenv('TWILIO_AUTH_TOKEN'))
8277

0 commit comments

Comments
 (0)