From 388948b9e2c118288c0503b298dbef904fb6900a Mon Sep 17 00:00:00 2001 From: prehensile Date: Sat, 1 Sep 2018 11:04:13 +0100 Subject: [PATCH] stories app, first decent version --- stories/footer.gif | Bin 0 -> 1206 bytes stories/header.gif | Bin 0 -> 1543 bytes stories/main.py | 167 +++++++++++++++++++++++++++++++++++++++++++ stories/stories.json | 1 + 4 files changed, 168 insertions(+) create mode 100644 stories/footer.gif create mode 100644 stories/header.gif create mode 100755 stories/main.py create mode 100644 stories/stories.json diff --git a/stories/footer.gif b/stories/footer.gif new file mode 100644 index 0000000000000000000000000000000000000000..570f7d6e9f5f95aea5761081d6dbcb5c72fdad5b GIT binary patch literal 1206 zcmZ?wbhEHb{J%P|NsA2{K>)q0y-c9WCjDvKYvCT2 zE6_P#l6${m-TRvN{|_uE@X*=E!hY1LP(Wh-g%xTGHCPxLo0JsaR&_SAIywnV7QfE@ z=%WRT=$k|3b-_KV+{crg0#-=6vhx>}v>LK-2s1VF`w4NBi}2;y^E3#tHn+{z6JVBLj=+>2SW@%`q&dk{Jyz5ALgbmGJi0v+QKns()}B|ld42)J&p)Bo6pgx+?b-O5wumCC)QJ- zS4ihs?n*^wL5LgtFi>I)TTb7(FR z5(+5kSoMTy=5{x(bq}_!JGD~pvDt)~p}~h%^fMoq<#~O8ky%qz(jYfcaAgzIr>)AN ziNy_*%Reo-nIo{!sX4PFD9TvvvZk(J=H#}i3NozB9jYnG%z`rvlJ=cT`m}37!*#}k zH&{OG(%rbV;H_NA_tFI3ot-&5M6~zYT(i`U;L;I@-oO7mgQt!{p@Y;v(ToR;9BL~b zH1U|dc+f21rtz>vBrM}$tHdM$Mh3#8IXksPAt^OIGtXA( z{qFrr3YjUkO5vuy2EGN(sTr9bRYj@6RemAKRoTgwDN6QsTs9R}6}bhusU?XD6}dTi z#a0!zN{K1?NvT#qHb_`sNdc^+B->WW5hS4iveP-gC{@8!&rCPj(AYx3+)~fb)Y8Jp zL`T8Mz|c(Jz*67PMAyL3%D~dfz)%4SlHCStb$zJpq2r7wn`Z#B?VUc`sL;2 zdgaD?`9*7iAWdWaj57fXqxx z$}cUkRZ`LiS)vcM1?W9}xc`70cnGED0fVIo=zqrl|Nj2@{p;tC@87({PcxqRv3h4bgmo;iK$#u35cm<%;FYmM&SmXyJnS^XAT(J!|HS>C>i8nLKIY zg#NzXp6;&Bj`p_Jmgc6$hWfhNn(C^`it@73lH#Jmg8aPPob0U3jP$hBl;otug!s7F znCPg;i14t`kl>)e0DnJUA8#*D4|g|L7iT9&2YWkP8*3{|3v)A56JsMo1ARSR9c?X5 v4RtkD6=fww1$jAH8EGj=32`w|5n&-g0e(JS9&Rp94t6$H7G@?!1_o;Y@rU8l literal 0 HcmV?d00001 diff --git a/stories/header.gif b/stories/header.gif new file mode 100644 index 0000000000000000000000000000000000000000..79dcccf775b72a808e5c155ee238cc31b3ebd22c GIT binary patch literal 1543 zcmX|7c{r2_9RB8VjA1nH`!l7>jJX^$6CoXrNTlRy#>_Yx#%D~-2s@}G5@~aWC{$9> zv1x@;t`12pom0-px>B^etl919_s8#ipZ9&<-!_-6bOzf2W(3S)0UC|w;^Gn*7$^`3 z5)%_MGcyYd3#+QC>g(&9nwq-0x?a9~DU->@$H(X9=9ZS0Oc#)VJP7h6=Kzq4>g_I< z8tLPY?<}#_W^uzy!Yaf`e7lddy7=L1M6~u`G6?auc={fF~wvy;T&lho0pqs_^rJYNuuQ&m8#9jyqP)%S=O8^g)6HaHc-rVyy|c35gqJFZb{8pR8Ap+51XaPEIyE2wB~Gq z3xLokUnF;7=rBPKsL?I!ZqHqfuyja`8Ag&jz8m7cE0o#p9Vr&>zuKGuPb=0}h}qjB zCFPs*ZzQmU($uSJYPw4G8CaLxATx|_`p&eisaRLyXvfHoch_QTJ=9e)_UlV5pEh=0 z-y_IZmb!j0f}j)@8Zl&*A=2yiwy(9_ak@L?JR-f`Pmrswed^G2jYs5$?S)a?X2soz zGJl=6Kv*);Fl@C0J&4&7rtt)IAt#UbjJ__UznV3y+&ir4Ym}Y?FA;e2?E~vCyRyz>iSrsPRfP3G zAv|H>a6WI0IP%s<`Hk}jQB8nSN%r%;e8)7eBxk%^ZT6@sRC%6m8pxNJo~Gg^zBWe~ zP$8wv8h7>KEF11@4>DlPavws6SeA#7SWfxSPJTMaK{2lFHrs{+;3f$1Omvw+=Ba@* z<(#38HH>2tt|EkUr2F`oJsFhpi?dDwE2CHRIT#0MLE0vH9uR*0ah$UhUiENZtC$9~ zJ~*QCXx(!ptcH_g5paEaeAg>swU@aC-O!rphlX*Vr`y0Z!c6+T6|W{cR%)z)4Nu{nI62n^<&5c!or>7g*@+gk6oO2K8L}@+iyhMikad_D3T{)gW|{t zK@3xDhi7x6f_cp4UcM&b!T%s4z8&6CegNf~PEkT0Xk}qdsS4Kf4qFr`@H8__tPhjA9Zzhw6{Hc(0cz~OLNoR#^3KW-2Uy>%^TP2>#kkB zQhT}P(!~qaRh8$@Rg|ACD=j%wTvS+)pLhD#Q@JN|vQK0k&&)WMel+dK;X|n@$w!wz8yADP)obkzl@l-P$#)%~q|%n}R0BMurCZdb&E= bTACWT73x?uRg4N+SxHd=g+#z%0I=a-T+)JP literal 0 HcmV?d00001 diff --git a/stories/main.py b/stories/main.py new file mode 100755 index 0000000..a9100bc --- /dev/null +++ b/stories/main.py @@ -0,0 +1,167 @@ +"""A big "thank you" to all our Sponsors who made this year's badge possible!""" + +___name___ = "twenty-three million stories" +___license___ = "MIT" +___categories___ = ["Art"] +___dependencies___ = [ "app", "dialogs", "http", "ugfx_helper", "sleep" ] +___bootstrapped___ = False + + +from app import * +from dialogs import * + +import ugfx_helper, ugfx, http, sleep, os, json +from tilda import Buttons + + +_STORY_URL = "http://twentythreemillionstories.org/api/story/generate" +_LOADING_TEXT = "Fetching a story..." +_FAIL_TEXT = "An artist wants to write all the stories in the world. She meets a cartographer and an inventor. The inventor makes a machine to make old stories new. The new stories are stranger than the originals, but the machine needs an internet connection. This is a great pity and the reader is sad." + +_LABEL = None + + +def fetch_story(): + + # use for testing when wifi fails + # return "A man falls in love with two twin sisters and is unable to decide which one to marry. Only the children survive. Through the window, the boy can see two big cranes in a construction site. But he loves her so much by now that he goes on with the marriage and then, he just waits." + + # print( "fetch_story" ) + story = None + + with http.get( _STORY_URL ) as response: + if response.status != 200: + return _FAIL_TEXT + story = response.raise_for_status().json() + + # { + # number: "61,957", + # story: "A man falls in love with two twin sisters and is unable to decide which one to marry. Only the children survive. Through the window, the boy can see two big cranes in a construction site. But he loves her so much by now that he goes on with the marriage and then, he just waits." + # } + + return story["story"] + + +def draw_furniture(): + ugfx.display_image( 0, 0, "stories/header.gif" ) + ugfx.display_image( 0, 320-29, "stories/footer.gif" ) + + +def display_story( story ): + global _LABEL + #_LABEL = init_label( win ) + # print( "display_story: %s" % story ) + ugfx.clear( ugfx.WHITE ) + draw_furniture() + _LABEL.text( story ) + + + +def fetch_and_display(): + display_story( _LOADING_TEXT ) + story = fetch_story() + display_story( story ) + + +def init_label(): + + ugfx.set_default_font( ugfx.FONT_MEDIUM ) + + margin_v = 29 + margin_h = 18 + + screen_w = 240 + screen_h = 320 + + return ugfx.Label( + margin_h, margin_v, #x, y + screen_w - (margin_h*2) , screen_h - (margin_v*2), # width, height + _LOADING_TEXT + ) + + +def init(): + # Background stuff + ugfx_helper.init() + ugfx.clear( ugfx.WHITE ) + + # Colour stuff + color = ugfx.html_color(333333) + style = ugfx.Style() + # [text_colour, edge_colour, fill_colour, progress_colour] + style.set_enabled([color,color,ugfx.WHITE,ugfx.GREY]) + style.set_background( ugfx.WHITE ) + ugfx.set_default_style( style ) + + +_CONFIG_FILE = "stories/stories.json" + + +def write_config( config ): + with open( _CONFIG_FILE, "wt") as file: + file.write( json.dumps(config) ) + file.flush() + os.sync() + + +def read_config(): + config = None + with open( _CONFIG_FILE, "rt") as file: + r = file.read() + print( r ) + config = json.loads( r ) + return config + + +def check_warning(): + config = read_config() + + if config["has_warned"]: + return config["is_ok"] + + content_ok = prompt_boolean( + """Some (not many) of the Twenty-three million stories contain potentially triggering content.\n +If that's OK by you, press A to start reading.""", + title="Content Warning" + ) + + config = { + "has_warned" : True, + "is_ok" : content_ok + } + + write_config( config ) + + return content_ok + + +## +# MAIN RUNLOOP +# + +init() + +if check_warning(): + + + _LABEL = init_label() + fetch_and_display() + + while True: + + sleep.wfi() + + if Buttons.is_pressed( Buttons.BTN_A ): + fetch_and_display() + + elif Buttons.is_pressed( Buttons.BTN_Menu ) or \ + Buttons.is_pressed( Buttons.BTN_B ) or \ + Buttons.is_pressed( Buttons.JOY_Center): + break + + # print ("Stories ded...") + if _LABEL: + _LABEL.destroy() + +ugfx.clear() + diff --git a/stories/stories.json b/stories/stories.json new file mode 100644 index 0000000..2660cf9 --- /dev/null +++ b/stories/stories.json @@ -0,0 +1 @@ +{ "has_warned" : false, "is_ok" : false } \ No newline at end of file