xkcd.drush.inc

  1. 8.0.x examples/xkcd.drush.inc
  2. 6.x examples/xkcd.drush.inc
  3. 7.x examples/xkcd.drush.inc
  4. 4.x examples/xkcd.drush.inc
  5. 5.x examples/xkcd.drush.inc
  6. master examples/xkcd.drush.inc

Example drush command.

To run this *fun* command, execute `drush --include=./examples xkcd` from within your drush directory.

See `drush topic docs-commands` for more information about command authoring.

You can copy this file to any of the following 1. A .drush folder in your HOME folder. 2. Anywhere in a folder tree below an active module on your site. 3. /usr/share/drush/commands (configurable) 4. In an arbitrary folder specified with the --include option. 5. Drupal's /drush or /sites/all/drush folders.

Functions

Namesort descending Description
drush_xkcd_display Retrieve and display a table of metadata for an XKCD cartoon, then retrieve and display the cartoon using a specified image viewer.
drush_xkcd_fetch Example drush command callback. This is where the action takes place.
xkcd_drush_command Implementation of hook_drush_command().
xkcd_drush_help Implementation of hook_drush_help().

File

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