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 XKCD 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, or in the /drush folder in the directory above the Drupal root.

Functions

Namesort descending Description
drush_xkcd_display Display a given XKCD cartoon.
drush_xkcd_fetch Implements drush_hook_COMMAND().
xkcd_drush_command Implements hook_drush_command().
xkcd_drush_help Implements hook_drush_help().

File

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