Last active
August 29, 2015 14:11
-
-
Save alnutile/ada597c7292cebab4bc0 to your computer and use it in GitHub Desktop.
[kid snippets] PHP and Mac needed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/php | |
| <?php | |
| function get_image($query){ | |
| $url = 'http://ajax.googleapis.com/ajax/services/search/images?v=1.0&q='; | |
| $url .= urlencode("site:www.thebrickfan.com " . $query . " lego"); | |
| $curl = curl_init(); | |
| curl_setopt($curl, CURLOPT_URL, $url); | |
| curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); | |
| $data = curl_exec($curl); | |
| curl_close($curl); | |
| $result = json_decode($data, true); | |
| return $result; | |
| } | |
| function get_name($image) | |
| { | |
| $name = explode('/', $image); | |
| return array_pop($name); | |
| } | |
| exec("say \"what is your favorite lego guy\""); | |
| echo 'what is your favorite lego guy?'; | |
| $guy = fgets(STDIN); | |
| exec("say \"Going to get your image of {$guy} be right back\""); | |
| //Get Guy from Image Site | |
| $image = get_image($guy); | |
| //Get random image | |
| $max = count($image['responseData']['results']); | |
| if($max == 0) | |
| { | |
| exec("say 'No image found, sorry'"); | |
| echo "No image found :("; | |
| } | |
| else | |
| { | |
| $image = $image['responseData']['results'][rand(0, $max - 1)]['url']; | |
| $name = get_name($image); | |
| file_put_contents('/tmp/' . $name, file_get_contents($image)); | |
| exec("open /tmp/$name"); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment