1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Yahoo answer wordpress plugin

Discussion in 'Blogging' started by nixnash, Jan 3, 2010.

  1. nixnash

    nixnash Power Member

    Joined:
    Oct 26, 2009
    Messages:
    581
    Likes Received:
    204
    Occupation:
    Student
    Location:
    BHW
    Dont know if this has been shared here...But heres the code..
    Code:
    <?php
    
    // split comma separated string into array
    function getCSVValues($string) {
       // split the string at double quotes "
       $bits = split('"',$string);
       $elements = array();
       for ($i=0;$i<count($bits);$i++) {
         /*
             odd numbered elements would have been
             enclosed by double quotes
             even numbered elements would not have been
         */
         if (($i%2) == 1) {
             /* if the element number is odd add the
                 whole string  to the output array */
             $elements[] = $bits[$i];
         } else {
             /* otherwise split the unquoted stuff at commas
                 and add the elements to the array */
             $rest = $bits[$i];
             $rest = preg_replace("/^,/","",$rest);
             $rest = preg_replace("/,$/","",$rest);
             $elements = array_merge($elements,split(',',$rest));
         }
       }
       return $elements;
    }
    
    function generate_post($title, $content) {
    // post to blog if a post..
          $_POST['post_name'] = sanitize_title(addslashes($title));
          $_POST['post_title'] = addslashes($title);
          $_POST['content'] = addslashes($content);
          $_POST['publish'] = 'publish';
          $_POST['comment_status'] = 'open';
      
          $post_id = write_post();
          return $post_id;
    }
    
    function generate_comment($author, $content, $post_id) {
          $commentdata = array();
          $commentdata['comment_post_ID'] = $post_id;
          $commentdata['comment_approved'] = 1;
          $commentdata['comment_author'] = addslashes($author);
          $commentdata['comment_content'] = addslashes($content);
    
        $comment_id = wp_insert_comment($commentdata);
    
        return $comment_id;    
    }
    
    // given a keyword and appid, it queries and 
    function post_answers_by_keyword($keyword, $appid, $max_results) {
        if (!isset($max_results)) { $max_results = 15; }
         $kw_url = 'http://answers.yahooapis.com/AnswersService/V1/questionSearch?appid=' . $appid . '&query=' . $keyword . '&results=' . $max_results;
        $num_questions = 0;
        $num_answers = 0;
         foreach(simplexml_load_file($kw_url)->Question as $ques) {
    
    // post question content as the post
             $post_id = generate_post($ques->Subject, $ques->Content);
             $num_questions++;
        
    // grab the answers
             $ques_url = 'http://answers.yahooapis.com/AnswersService/V1/getQuestion?appid=' . $appid . '&question_id=' . $ques['id'];
    
             foreach (simplexml_load_file($ques_url)->Question->Answers->Answer as $ans) {
                    $num_answers++;
    // post each answer..
                    $comment_id = generate_comment($ans->UserNick, $ans->Content, $post_id);
             } //foreach answer
        
         } // foreach question
      $results = "questions: " . $num_questions . " / answers: " . $num_answers;
      return $results;
    }
    
    
    function my_options_subpanel() {
    
    // if data has been posted, take action
      if (isset($_POST['info_update'])) {
        
        // if appid defined use that one, else default to "YahooDemo"
        if (!isset($_POST['appid'])) {
            $_POST['appid'] = 'YahooDemo';
        } 
    
        
        // set max questions to 15 unless otherwise set
        if (!isset($_POST['max_ques'])) {
            $_POST['max_ques'] = 15;
        }
    
        
        // if keyword text has been entered, split if comma-separated data..
        if ((isset($_POST['kw_list'])) && ($_POST['kw_list'] <> "")) {
               $kw_array = getCSVValues($_POST['kw_list']);
               $num_keywords = sizeof($kw_array);
              
        }
    
      
           if (sizeof($kw_array) > 0) { 
              foreach ($kw_array as $keyword) { 
                 $results .= "<li>'" . $keyword . 
                  "' - ". post_answers_by_keyword($keyword, $_POST['appid'],$_POST['max_ques']) . "</li>";  
               } //foreach
           }    // if sizeof > 0
    
    
        ?>
        
    <?php
    // the blue box.
        if (isset($num_keywords)) { 
        ?><div class="updated"><p><strong><?php
            if ($num_keywords > 1) { $kwstr = "keywords"; } else { $kwstr = "keyword"; }
            if ($num_keywords > 0) { 
               _e('Processed ' . $num_keywords . ' ' . $kwstr . '..<br/>'); 
               ?><ul><?php echo $results; ?></ul><?php
             } else {
               _e('ERROR: No keywords to process!'); 
             } // else
      ?></strong></p></div><?php
      } // if isset keywords
      
    } // if posted info ?>
    <div class=wrap>
      <form method="post">
        <h2>Import Content From Yahoo Answers</h2>
         <fieldset name="kw_list">
        <legend><?php _e('Enter keyword(s), comma separated', 'Localization name') ?></legend>
        <input type="text" name="kw_list" size="40">
     </fieldset>
         <fieldset name="max_ques">
        <legend><?php _e('Questions per keyword. (50 max, less if you get server timeout problems.)', 'Localization name') ?></legend>
    <?php if (!isset($_POST['max_ques'])) { $_POST['max_ques'] = 15; } ?>
        <input type="text" name="max_ques" value="<?php echo $_POST['max_ques']; ?>" size="20">
     </fieldset>
         <fieldset name="appid">
        <legend><?php _e('Yahoo Application ID (<a href="http://api.search.yahoo.com/webservices/register_application">get one here</a>)', 'Localization name') ?></legend>
    <?php if (!isset($_POST['appid'])) { $_POST['appid'] = 'YahooDemo'; } ?>
        <input type="text" name="appid" value="<?php echo $_POST['appid']; ?>" size="20">
         </fieldset>
      <div class="submit">
        <div style="float: left; margin-top: 10px; padding: 5px;">
       
        </div>
      <input type="submit" name="info_update" value="<?php
        _e('Process!', 'Localization name')
        ?> ยป" /></div>
      </form>
     </div><?php
    } // function
    
    function add_pages() {
       add_submenu_page('import.php', 'Yahoo! Answers', 'Yahoo! Answers', 8, __FILE__, 'my_options_subpanel');
    }
    
    add_action('admin_menu', 'add_pages');
    
    ?> 
     
    • Thanks Thanks x 1
  2. AgentOrange_MkUltra

    AgentOrange_MkUltra Junior Member

    Joined:
    May 29, 2009
    Messages:
    180
    Likes Received:
    44
    Occupation:
    Hijacking Your System
    Location:
    Many steps ahead & Behind 4 Proxies + double VPN!
    I presume this has been taken from Caffeinated COntent source code. Anyway in all cases it can do that work better than the above code. The above code may be a nice starting point for a rss aggregator working independently by Wordpress of course. On the other hand, there are a few already out there too. I'm thinking to do a new one sooner or later as well...

    Thx for sharing anyway