xkcd.drush.inc 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. /**
  3. * @file
  4. * Example drush command.
  5. *
  6. * To run this *fun* command, execute `drush --include=./examples xkcd`
  7. * from within your drush directory.
  8. *
  9. * See `drush topic docs-commands` for more information about command authoring.
  10. *
  11. * You can copy this file to any of the following
  12. * 1. A .drush folder in your HOME folder.
  13. * 2. Anywhere in a folder tree below an active module on your site.
  14. * 3. /usr/share/drush/commands (configurable)
  15. * 4. In an arbitrary folder specified with the --include option.
  16. */
  17. /**
  18. * Implementation of hook_drush_command().
  19. *
  20. * In this hook, you specify which commands your
  21. * drush module makes available, what it does and
  22. * description.
  23. *
  24. * Notice how this structure closely resembles how
  25. * you define menu hooks.
  26. *
  27. * See `drush topic docs-commands` for a list of recognized keys.
  28. *
  29. * @return
  30. * An associative array describing your command(s).
  31. */
  32. function xkcd_drush_command() {
  33. $items = array();
  34. // The 'xkcd' command
  35. $items['xkcd-fetch'] = array(
  36. 'description' => "Retrieve and display xkcd cartoons.",
  37. 'arguments' => array(
  38. 'search' => 'Optional argument to retrive the cartoons matching an index number, keyword search or "random". If omitted the latest cartoon will be retrieved.',
  39. ),
  40. 'options' => array(
  41. 'image-viewer' => 'Command to use to view images (e.g. xv, firefox). Defaults to "display" (from ImageMagick).',
  42. 'google-custom-search-api-key' => 'Google Custom Search API Key, available from https://code.google.com/apis/console/. Default key limited to 100 queries/day globally.',
  43. ),
  44. 'examples' => array(
  45. 'drush xkcd' => 'Retrieve and display the latest cartoon.',
  46. 'drush xkcd sandwich' => 'Retrieve and display cartoons about sandwiches.',
  47. 'drush xkcd 123 --image-viewer=eog' => 'Retrieve and display cartoon #123 in eog.',
  48. 'drush xkcd random --image-viewer=firefox' => 'Retrieve and display a random cartoon in Firefox.',
  49. ),
  50. 'aliases' => array('xkcd'),
  51. 'bootstrap' => DRUSH_BOOTSTRAP_DRUSH, // No bootstrap at all.
  52. );
  53. return $items;
  54. }
  55. /**
  56. * Implementation of hook_drush_help().
  57. *
  58. * This function is called whenever a drush user calls
  59. * 'drush help <name-of-your-command>'. This hook is optional. If a command
  60. * does not implement this hook, the command's description is used instead.
  61. *
  62. * This hook is also used to look up help metadata, such as help
  63. * category title and summary. See the comments below for a description.
  64. *
  65. * @param
  66. * A string with the help section (prepend with 'drush:')
  67. *
  68. * @return
  69. * A string with the help text for your command.
  70. */
  71. function xkcd_drush_help($section) {
  72. switch ($section) {
  73. case 'drush:xkcd-fetch':
  74. return dt("A command line tool (1) for a web site tool (2), that emulates
  75. (badly) a web based tool (3) that emulates (badly) a command line tool (4) to
  76. access a web site (5) with awesome geek humor.\n
  77. (1) Drush
  78. (2) Drupal
  79. (3) http://uni.xkcd.com/
  80. (4) BASH
  81. (5) http://xkcd.com/");
  82. }
  83. }
  84. /**
  85. * Example drush command callback. This is where the action takes place.
  86. *
  87. * The function name should be same as command name but with dashes turned to
  88. * underscores and 'drush_commandfile_' prepended, where 'commandfile' is
  89. * taken from the file 'commandfile.drush.inc', which in this case is 'sandwich'.
  90. * Note also that a simplification step is also done in instances where
  91. * the commandfile name is the same as the beginning of the command name,
  92. * "drush_example_example_foo" is simplified to just "drush_example_foo".
  93. * To also implement a hook that is called before your command, implement
  94. * "drush_hook_pre_example_foo". For a list of all available hooks for a
  95. * given command, run drush in --debug mode.
  96. *
  97. * If for some reason you do not want your hook function to be named
  98. * after your command, you may define a 'callback' item in your command
  99. * object that specifies the exact name of the function that should be
  100. * called. However, the specified callback function must still begin
  101. * with "drush_commandfile_" (e.g. 'callback' => "drush_example_foo_execute")
  102. * if you want that all hook functions are still called (e.g.
  103. * drush_example_pre_foo_execute, and so on).
  104. *
  105. * In this function, all of Drupal's API is (usually) available, including
  106. * any functions you have added in your own modules/themes.
  107. *
  108. * @see drush_invoke()
  109. * @see drush.api.php
  110. *
  111. * @param
  112. * An optional string with search keyworks, cartoon ID or "random".
  113. */
  114. function drush_xkcd_fetch($search = '') {
  115. if (empty($search)) {
  116. drush_xkcd_display('http://xkcd.com');
  117. }
  118. elseif (is_numeric($search)) {
  119. drush_xkcd_display('http://xkcd.com/' . $search);
  120. }
  121. elseif ($search == 'random') {
  122. $xkcd_response = @json_decode(file_get_contents('http://xkcd.com/info.0.json'));
  123. if (!empty($xkcd_response->num)) {
  124. drush_xkcd_display('http://xkcd.com/' . rand(1, $xkcd_response->num));
  125. }
  126. }
  127. else {
  128. // This uses an API key with a limited number of searches per
  129. $search_response = @json_decode(file_get_contents('https://www.googleapis.com/customsearch/v1?key=' . drush_get_option('google-custom-search-api-key', 'AIzaSyDpE01VDNNT73s6CEeJRdSg5jukoG244ek') . '&cx=012652707207066138651:zudjtuwe28q&q=' . $search));
  130. if (!empty($search_response->items)) {
  131. foreach ($search_response->items as $item) {
  132. drush_xkcd_display($item->link);
  133. }
  134. }
  135. else {
  136. drush_set_error('DRUSH_XKCD_SEARCH_FAIL', dt('The search failed or produced no results.'));
  137. }
  138. }
  139. }
  140. /**
  141. * Retrieve and display a table of metadata for an XKCD cartoon,
  142. * then retrieve and display the cartoon using a specified image viewer.
  143. *
  144. * @param
  145. * A string with the URL of the cartoon to display.
  146. */
  147. function drush_xkcd_display($url) {
  148. $xkcd_response = @json_decode(file_get_contents($url . '/info.0.json'));
  149. if (!empty($xkcd_response->num)) {
  150. $data = (array)$xkcd_response;
  151. $data['date'] = $data['year'] . '/' . $data['month'] . '/' . $data['day'];
  152. unset($data['safe_title'], $data['news'], $data['link'], $data['year'], $data['month'], $data['day']);
  153. drush_print_table(drush_key_value_to_array_table($data));
  154. $img = realpath(_drush_download_file($data['img']));
  155. drush_register_file_for_deletion($img);
  156. drush_shell_exec(drush_get_option('image-viewer', 'display') . ' ' . $img);
  157. }
  158. else {
  159. drush_set_error('DRUSH_XKCD_METADATA_FAIL', dt('Unable to retrieve cartoon metadata.'));
  160. }
  161. }