Advertisement
Guest User

Untitled

a guest
Nov 1st, 2014
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.68 KB | None | 0 0
  1. <?php
  2.  
  3.  
  4. /**
  5. * Implements hook_menu().
  6. */
  7. function mymodule_menu() {
  8. $items = array();
  9.  
  10. $items['mymodule'] = array(
  11. 'title' => 'mymodule',
  12. 'page callback' => 'pull_data',
  13. 'page arguments' => array(),
  14. 'access arguments' => array('access content'),
  15. );
  16. return $items;
  17. }
  18.  
  19.  
  20. function insert_term_if_not_exists($term_name, $vocabulary_name) {
  21. $vocabulary = taxonomy_vocabulary_machine_name_load($vocabulary_name);
  22. $matched_terms = taxonomy_get_term_by_name($term_name, $vocabulary->machine_name);
  23. if(empty($matched_terms)) {
  24. $term = new stdClass();
  25. $term->vid = $vocabulary->vid;
  26. $term->name = $term_name;
  27. taxonomy_term_save($term);
  28. } else {
  29. $term = array_shift($matched_terms);
  30. }
  31. return $term;
  32. }
  33.  
  34.  
  35. function pull_data() {
  36. $str = file_get_contents('http://www.remotesite.tld/news.php?S=left');
  37. $re = "/\?st=\K\S+(?=\s*target)/";
  38. preg_match_all($re, $str, $matches);
  39. $content ='<h3>The following remote URLs have been processed:</h3>';
  40.  
  41. foreach ($matches[0] as $match) {
  42. $last_time = db_query('SELECT created FROM {node} WHERE created = :created', array(':created' => $match))->fetchField();
  43.  
  44. if ($last_time<>$match) {
  45.  
  46. $article = iconv('windows-1251', 'utf-8', file_get_contents('http://www.remotesite.tld/news2.php?st='.$match));
  47.  
  48. $reg_country = "/(?<=target=left class=datn>).*?(?=<\/a> &nbsp;)/";
  49. preg_match_all($reg_country, $article, $country);
  50. $country = $country[0][0];
  51. if ($country == "Europe") {$country="Planet";}
  52.  
  53. $reg_title = "/(?<=\<title>).*?(?= \| Europe)/";
  54. preg_match_all($reg_title, $article, $title);
  55. $title = $title[0][0];
  56. $title = str_replace('(фото)', '', $title);
  57. $title = str_replace('(фрейм)', '', $title);
  58.  
  59. $reg_body = "/(?<=\<p align=justify>).*?(?=<p>)/";
  60. preg_match_all($reg_body, $article, $body);
  61. $body = $body[0][0];
  62.  
  63. $reg_source = "/(?<=target=_blank class=datn>).*?(?=<\/a><br>Permalink)/";
  64. preg_match_all($reg_source, $article, $source);
  65. $source = $source[0][0];
  66.  
  67. $node = new stdClass();
  68. $node->uid = '1';
  69. $node->type = 'article';
  70. node_object_prepare($node);
  71. $node->created = $match;
  72. $node->changed = $match;
  73. $node->title = $title;
  74. $node->status = 0;
  75. $node->promote = 0;
  76. $node->sticky = 0;
  77.  
  78. if ($source == "Europe") { $node->flash=1; } else { $node->flash=0; }
  79.  
  80. $node->language = 'en';
  81. $node->body['und'][0]['value'] = $body;
  82. $node->body['und'][0]['format'] = 'filtered_html';
  83.  
  84. insert_term_if_not_exists($country, 'tags');
  85. $term_tags = taxonomy_get_term_by_name($country);
  86. $tid_tags = key($term_tags);
  87. $lang_tags = field_language('node', $node, 'field_tags');
  88. $node->field_tags[$lang_tags][0]['tid'] = $tid_tags;
  89.  
  90. insert_term_if_not_exists($source, 'source');
  91. $term_source = taxonomy_get_term_by_name($source);
  92. $tid_source = key($term_source);
  93. $lang_source = field_language('node', $node, 'field_source');
  94. $node->field_source[$lang_source][0]['tid'] = $tid_source;
  95.  
  96. $reg_image = "/(?<=\<td><IMG src=).*?(?=border=1)/";
  97. preg_match_all($reg_image, $article, $image);
  98. if (isset($image[0][0])) {
  99. $image_url = 'http://www.remotesite.tld/'.$image[0][0];
  100. $file = file_save_data(file_get_contents($image_url), file_default_scheme().'://field/image/'.basename($image_url));
  101. $file->status = 1;
  102. $node->field_image['und'][0] = (array)$file;
  103. }
  104. node_save($node);
  105. $content .= $title.'<br>';
  106. } else { $content .='http://www.remotesite.tld/news2.php?st='.$match.' had been previously added.<br>'; }
  107.  
  108. } // for each end
  109.  
  110. return $content;
  111.  
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement