1
+ # SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries
2
+ #
3
+ # SPDX-License-Identifier: Unlicense
4
+ import os
5
+ import time
6
+ from adafruit_magtag .magtag import MagTag
7
+
8
+ # Set up where we'll be fetching data from
9
+ DATA_SOURCE = "https://www.adafruit.com/api/quotes.php"
10
+ QUOTE_LOCATION = [0 , "text" ]
11
+ AUTHOR_LOCATION = [0 , "author" ]
12
+
13
+ # the current working directory (where this file is)
14
+ try :
15
+ cwd = os .path .dirname (os .path .realpath (__file__ ))
16
+ except AttributeError :
17
+ cwd = ("/" + __file__ ).rsplit ("/" , 1 )[0 ]
18
+
19
+ magtag = MagTag (
20
+ url = DATA_SOURCE ,
21
+ json_path = (QUOTE_LOCATION , AUTHOR_LOCATION ),
22
+ default_bg = 0x000000 ,
23
+ )
24
+
25
+ def add_quote_marks (quote_text ):
26
+ return f"\" { quote_text } \" "
27
+
28
+ def add_hyphen (author_name ):
29
+ return f"- { author_name } "
30
+
31
+ magtag .add_text (
32
+ text_position = (
33
+ (magtag .graphics .display .width // 2 ) - 1 ,
34
+ (magtag .graphics .display .height // 2 ) - 20 ,
35
+ ),
36
+ text_color = 0xFFFFFF ,
37
+ text_wrap = 44 ,
38
+ text_maxlen = 180 ,
39
+ line_spacing = 1.0 ,
40
+ text_transform = add_quote_marks ,
41
+ text_anchor_point = (0.5 , 0.5 ),
42
+ )
43
+
44
+ magtag .add_text (
45
+ text_position = (
46
+ (magtag .graphics .display .width // 2 ) - 120 ,
47
+ (magtag .graphics .display .height // 2 ) + 34 ,
48
+ ),
49
+ text_color = 0xFFFFFF ,
50
+ text_wrap = 0 ,
51
+ text_maxlen = 30 ,
52
+ text_transform = add_hyphen ,
53
+ )
54
+
55
+ while True :
56
+ try :
57
+ value = magtag .fetch ()
58
+ print ("Response is" , value )
59
+ except (ValueError , RuntimeError ) as e :
60
+ print ("Some error occured, retrying! -" , e )
61
+ time .sleep (180 )
0 commit comments