Medusa

How To Send And Receive Online Data By Using Livecode

<p><b><i>Make your own apps that can upload to the web in LiveCode<&sol;i><&sol;b><&sol;p>&NewLine;<p>Getting and sending data around the internet can be a useful trick to be able to do in your own software&period; We’re going to show you how to grab the content of an HTML file and extract something from it for use in your apps&comma; and we’ll also make a simple tool to update a text file on a web server using FTP&period; With this you could&comma; for example&comma; create a tool that lets someone safely update one single part of a website with full confidence even if they have zero technical understanding&period;<&sol;p>&NewLine;<p>The URL keyword is used to refer to a file&comma; both online and locally on your own computer&period; We use it to fetch the contents of a web page&comma; then take a quick look at extracting something from the results&period; Click on File &gt&semi; New Mainstack&comma; then drag in a Push Button and a Scrolling Field object from the Tools palette&period;<&sol;p>&NewLine;<p>Fetching a HTML page is a very straightforward process&colon; saying put URL &&num;8220&semi;http&colon;&sol;&sol;www&period;billboard&period;com&sol;charts&sol;hot-100&&num;8221&semi; into field 1 does the trick &lpar;bear with our musical selection for this tutorial&rpar;&comma; so select the button&comma; click the Code button in the Toolbar&comma; and add that to the on mouseUp script of your button&period; If you want to check for hiccups&comma; for example&comma; if the page isn’t available&comma; you need to do a little more&comma; such as putting the URL into a variable &lpar;variables are temporary storage for keeping track of stuff – see part 2 of our series&rpar; and checking the result to see if it’s &OpenCurlyQuote;empty’ &lpar;good&rpar; or has an error &lpar;not good&rpar;&period; Finally&comma; if there’s an error&comma; then tell the user what it is&period;<&sol;p>&NewLine;<p><i>on mouseUp<&sol;i><&sol;p>&NewLine;<p><i>put empty into field 1<&sol;i><&sol;p>&NewLine;<p><i>put URL &&num;8220&semi;http&colon;&sol;&sol;www&period;billboard&period;<&sol;i><&sol;p>&NewLine;<p><i>com&sol;charts&sol;hot-100&&num;8243&semi; into theData<&sol;i><&sol;p>&NewLine;<p><i>if the result is empty<&sol;i><&sol;p>&NewLine;<p><i>then<&sol;i><&sol;p>&NewLine;<p><i>put theData into field 1<&sol;i><&sol;p>&NewLine;<p><i>else<&sol;i><&sol;p>&NewLine;<p><i>put the result into field 1<&sol;i><&sol;p>&NewLine;<p><i>end if<&sol;i><&sol;p>&NewLine;<p><i>end mouseUp<&sol;i><&sol;p>&NewLine;<p>This creates a pile of HTML that isn’t useful to you&comma; so here’s a way to dig through the contents and find something within it&period; Click the Dictionary icon in the Toolbar&comma; and search for offset &period; This tells you where something is within some text&semi; offset finds the number of characters in&comma; wordOffset finds the number of words&comma; and lineOffset the number of lines&period; Let’s use the latter to find where the first &lt&semi;h1&gt&semi; tag is in all that HTML code&comma; so we can grab anything in that particular tag&period;<&sol;p>&NewLine;<p>The lineOffset function needs two bits of information&colon; the text you’re searching for&comma; and the container to search&period; If you &OpenCurlyQuote;get’ the line offset&comma; the result is automatically stored in the &OpenCurlyQuote;it’ variable&comma; ready for you to use when you need to say which line you mean&period; Put these two lines of code after the end if in the above example to extract just the line with the &lt&semi;h1&gt&semi; tag&colon;<&sol;p>&NewLine;<p><i>get lineOffset&lpar;&&num;8220&semi;&lt&semi;h1&gt&semi;&&num;8221&semi;&comma; field 1&rpar;<&sol;i><i> <&sol;i><i>set the HTMLtext of field 1 to<&sol;i><i> <&sol;i><i>line it of field 1<&sol;i><&sol;p>&NewLine;<p>Setting the HTMLtext of a field to something instead of just putting the data into it strips out any HTML tags and attempt to apply styles&comma; if relevant&period; If you have control over the web page you’re grabbing&comma; you could customise it so it supports what you want to do in your app as well as what you want to present in a browser&period; For example&comma; you could put custom tags or tag elements into key parts of the HTML to make it easier to find and extract data&period; If that’s not possible &lpar;maybe you want to find something in a page you don’t control&rpar;&comma; you just need to find a logical way to identify the things you want within the HTML code of your nominated page&period;<&sol;p>&NewLine;

Exit mobile version