creating databases for web applications twitter example classwork/homework: projects
TRANSCRIPT
![Page 1: Creating Databases for Web Applications Twitter example Classwork/homework: Projects](https://reader035.vdocuments.net/reader035/viewer/2022081816/56649efe5503460f94c13c6e/html5/thumbnails/1.jpg)
Creating Databases for Web Applications
Twitter example
Classwork/homework: Projects
![Page 2: Creating Databases for Web Applications Twitter example Classwork/homework: Projects](https://reader035.vdocuments.net/reader035/viewer/2022081816/56649efe5503460f94c13c6e/html5/thumbnails/2.jpg)
Twitter grab
• request from senior• Example of application involving an XML
(non-relational) database • Approach:
– combination of researching what is available and doing your own coding
– I found and could understand a wrapper around the raw Twitter API:http://thinkvitamin.com/code/how-to-get-started-with-the-twitter-api/
![Page 3: Creating Databases for Web Applications Twitter example Classwork/homework: Projects](https://reader035.vdocuments.net/reader035/viewer/2022081816/56649efe5503460f94c13c6e/html5/thumbnails/3.jpg)
Requirements…so far
• grab the latest tweets with a certain hashtag– I used a search on content so will get html5 AND
#html5. Can do the restriction.
• do NOT include retweets and replies– I do a check on the returned results. There MAY be a
better way: filter these out earlier.
• refresh automatically: without user action, every 30 seconds– for debugging, I set this to 5 seconds– I do this strictly 'in' HTML / with JavaScript
![Page 4: Creating Databases for Web Applications Twitter example Classwork/homework: Projects](https://reader035.vdocuments.net/reader035/viewer/2022081816/56649efe5503460f94c13c6e/html5/thumbnails/4.jpg)
So far
• Mine:http://socialsoftware.purchase.edu/jeanine.meyer/twittersearch.html
• Danielle's much prettier version:http://socialsoftware.purchase.edu/danielle.lempp/realtime/twittersearch.html
![Page 5: Creating Databases for Web Applications Twitter example Classwork/homework: Projects](https://reader035.vdocuments.net/reader035/viewer/2022081816/56649efe5503460f94c13c6e/html5/thumbnails/5.jpg)
wrapper (helper)
• Downloaded from thinkvitamin.com: class.twitter.php
• Tester script (form): twittersearch.html
• Working script: twittersearch.php– makes calls to the class.twitter.php functions – filters out the items starting with RT or @– prepares HTML for display– straight html does the refresh
![Page 6: Creating Databases for Web Applications Twitter example Classwork/homework: Projects](https://reader035.vdocuments.net/reader035/viewer/2022081816/56649efe5503460f94c13c6e/html5/thumbnails/6.jpg)
xml versus json
• json is an object oriented format using ->
• The class.twitter.php code converts this.
• I just followed their example….
• Sample of the XML is https://twitter.com/statuses/user_timeline/karipatila.xml
• Note: a tweet is a status element
![Page 7: Creating Databases for Web Applications Twitter example Classwork/homework: Projects](https://reader035.vdocuments.net/reader035/viewer/2022081816/56649efe5503460f94c13c6e/html5/thumbnails/7.jpg)
my twittersearch.php<?phpheader('Content-type: text/html; charset=utf-8');require_once 'class.twitter.php';// from http://thinkvitamin.com/code/how-to-get-started-with-the-twitter-api/?><html><head><title>Finding tweets for a specified tag</title><script>function init() {
window.setTimeout(function() {location.reload(true);},5000);};</script> </head><body onLoad="init();self.focus();">
![Page 8: Creating Databases for Web Applications Twitter example Classwork/homework: Projects](https://reader035.vdocuments.net/reader035/viewer/2022081816/56649efe5503460f94c13c6e/html5/thumbnails/8.jpg)
twittersearch.php, cont.
<?php
$t = new twitter;
$tag = $_GET['key'];
print "Searching for $tag...";
$s = new summize;
$data = $s->search($tag);
$data = $data->results;
![Page 9: Creating Databases for Web Applications Twitter example Classwork/homework: Projects](https://reader035.vdocuments.net/reader035/viewer/2022081816/56649efe5503460f94c13c6e/html5/thumbnails/9.jpg)
Comment
• $data contains any number of items.• the foreach($data as $d) loops over each
one giving each the name $d• Each $d has properties (using the json
arrow notation), including $d->text• Extra credit for someone who decodes the
use of regular expression replace: preg_replace
• Notice going into and out of php
![Page 10: Creating Databases for Web Applications Twitter example Classwork/homework: Projects](https://reader035.vdocuments.net/reader035/viewer/2022081816/56649efe5503460f94c13c6e/html5/thumbnails/10.jpg)
foreach($data as $d){ $st2= substr($d->text,0,2);
$st1 = substr($d->text,0,1); if ((strcasecmp($st2,"RT") !=0) && (strcasecmp($st1,"@")!=0)) { //case insensitive compare to RT
?><li> <img src="<?php echo $d->profile_image_url; ?>" alt="" />
<?php echo preg_replace('/(^|\s)@(\w+)/','\1 <a href="http://twitter.com/\2">@\2</a>', $d->text); ?>
<em>by</em><a href="http://twitter.com/
<?php echo $d->from_user; ?>"><?php echo $d->from_user; ?></a>
<?php echo $d->created_at; ?> <em>from</em><?php echo html_entity_decode($d->source); ?>
</li><?php }} ?></ul> </body> </html>
![Page 11: Creating Databases for Web Applications Twitter example Classwork/homework: Projects](https://reader035.vdocuments.net/reader035/viewer/2022081816/56649efe5503460f94c13c6e/html5/thumbnails/11.jpg)
Classwork / homework
• Work on projects