Advertisement
tpaper

fb get data v0.7 provvisorio

Jan 6th, 2017
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 11.24 KB | None | 0 0
  1. <?php
  2. //Version 0.7 - 20 Dic 2016
  3. /*
  4.     This script was made by Enrico `tpaper' Ronconi in Nov 2014
  5.     <ronconi.enrico@yahoo.it>
  6. */
  7.  
  8. /*
  9.     This program is free software: you can redistribute it and/or modify it
  10.     under the terms of the GNU General Public License as published by the
  11.     Free Software Foundation, either version 3 of the License, or (at your
  12.     option) any later version.
  13.  
  14.     This program is distributed in the hope that it will be useful, but
  15.     WITHOUT ANY WARRANTY; without even the implied warranty of
  16.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17.     General Public License for more details.
  18.  
  19.     You should have received a copy of the GNU General Public License along
  20.     with this program. If not, see <http://www.gnu.org/licenses/>.
  21. */
  22.  
  23. if( $argc < 2 ){
  24.     die("Usage: php ".$argv[0]." group-id [mysql-table-name]\n");
  25. }
  26.  
  27. $prev_newline = true;
  28.  
  29. //-------------------------------------
  30. //  GENERAL SETTINGS
  31. //-------------------------------------
  32.  
  33. $verbose = true;                        //If true, displays debug messages
  34. $show_urls = true;                      //If true, displays url of every https request
  35. $log_filename = 'get_data.log';         //Log file name, IF EXISTING, IT WILL BE DELETED
  36. $out_filename = 'out.csv';              //Output file name
  37. $mysql_auto = true;                     //If true, the script imports this data into a mysql table automatically
  38. $post_limit = false;                    //Max number of post stored. If set to false, it will continue until the feed ends
  39.  
  40. if( $mysql_auto && ($argc < 3)) {
  41.     die('Since $mysql_auto is true, you must provide the table name or set $mysql_auto to false'."\nUsage: php ".$argv[0]." group-id [mysql-table-name]\n");
  42. }
  43.  
  44. //You must provide a valid Facebook access token of a user that have access to the group
  45. $fb_access_token = '';
  46. $fb_group_id = $argv[1];    //Target group id
  47. $fb_api_ver = '2.8';        //Graph API version to use. IT'S VERY IMPORTANT, please read the 'permissions' part of the readme file
  48.  
  49. if(floatval($fb_api_ver) > 2.59) {
  50.     $reactions = true;
  51.     logthis("Reactions count enabled");
  52. } else $reactions = false; //If api >= 2.6 enables reaction support
  53.  
  54. //-------------------------------------
  55. //  MySQL OPTIONS
  56. //-------------------------------------
  57. //  Necessary only if $mysql_auto is set to true.
  58.  
  59. $MYSQL_host = 'localhost';                  //Where the MySQL server is hosted
  60. $MYSQL_port = '3306';                       //MySQL server port
  61. $MYSQL_username = 'php';                    //MySQL username. Obviously that user must have permission to insert data into $MYSQL_table
  62. $MYSQL_password = '';       //If you need explanation for this var, you have to consider the idea of deleting this script from your pc and go swimming
  63. $MYSQL_db = 'fbstat';                       //MySQL database name
  64. //$MYSQL_table = '';                        //MySQL table name - now from argument 2
  65. $MYSQL_table = $argv[2];
  66.  
  67. $MYSQL_fields_list = ['ID','uid','uname','type','date','ln','cn','fc_uid','fc_uname']; //Fields name in the MySQL table. This order MUST be the same of the order in the parse_and_write function.
  68. /*
  69.     Table recommended format:
  70.    
  71.     +----------+-------------+------+-----+---------+-------+
  72.     | Field    | Type        | Null | Key | Default | Extra |
  73.     +----------+-------------+------+-----+---------+-------+
  74.     | ID       | varchar(33) | NO   | PRI | NULL    |       | post id
  75.     | uid      | varchar(17) | NO   |     | NULL    |       | user id
  76.     | uname    | tinytext    | NO   |     | NULL    |       | user name
  77.     | type     | varchar(10) | NO   |     | NULL    |       | post type
  78.     | date     | datetime    | NO   |     | NULL    |       | date
  79.     | ln       | smallint(6) | NO   |     | NULL    |       | likes/reactions count
  80.     | cn       | smallint(6) | NO   |     | NULL    |       | comments count
  81.     | fc_uid   | varchar(17) | YES  |     | NULL    |       | first comment user id
  82.     | fc_uname | tinytext    | YES  |     | NULL    |       | first comment user name
  83.     +----------+-------------+------+-----+---------+-------+
  84. */ 
  85.  
  86. //-------------------------------------
  87. //  ADVANCED SETTINGS
  88. //-------------------------------------
  89. //  If not sure, just don't touch this. In most cases you don't have to
  90. //  modify this in order to run the script.
  91.  
  92. /*
  93. List of the fields that the script will ask to Facebook. Notice that ADD a
  94. field DOESN'T MAKE the script save that field value. If you want to store
  95. another field you have to modify this parameter and the functions
  96. parse_and_write and mysql_import.
  97. */
  98. if($reactions){
  99.     $fb_fileds = ['id','from','created_time','type','reactions.summary(true).limit(0)','comments.summary(true).limit(1)']; //with reactions
  100. } else {
  101.     $fb_fileds = ['id','from','created_time','type','likes.summary(true).limit(0)','comments.summary(true).limit(1)']; //without reactions
  102.    
  103. }
  104. //default: $fb_fileds = ['id','from','created_time','type','reactions.summary(true).limit(0)','comments.summary(true).limit(1)']; with reactions,
  105. // $fb_fileds = ['id','from','created_time','type','likes.summary(true).limit(0)','comments.summary(true).limit(1)']; without reactions
  106.  
  107. /*
  108. Riscrivi
  109. */
  110. $fb_time_step = 60*60*12;
  111.  
  112. $http_tries = 50;           //Number of http tries before give up
  113. $http_delay = 750;          //Delay between http request tries (mills)
  114.  
  115. $table_record_sep = ',';    //Fields separator in the csv file
  116. $table_row_sep = "\n";      //Row separator in the csv file
  117.  
  118. //Escaped version of previous separators: (for MySQL importing)
  119. $table_record_sep_esc = ',';
  120. $table_row_sep_esc = "\\n";
  121.  
  122. //-------------------------------------
  123. //  END OF SETTINGS
  124. //-------------------------------------
  125.  
  126. function logthis($message,$newline = true){
  127.     /*
  128.     Log the message with timestamp into the file handled by $log_hande.
  129.     If $log_handle is null, no log is written.
  130.     If $verbose is set to true, it also displays the message.
  131.     */
  132.    
  133.     global $verbose,$log_handle,$prev_newline;
  134.    
  135.     $micros = microtime();
  136.     $micros = explode(" ",$micros);
  137.     $micros = explode('.',$micros[0]);
  138.     $micros = $micros[1];
  139.     $timestamp = date("d M Y H:i:s");
  140.     $timestamp = $timestamp.'.'.substr($micros,0,1);
  141.    
  142.     if($prev_newline) $message = $timestamp." :: ".$message;
  143.     if($newline) $message = $message."\n";
  144.     $prev_newline = $newline;
  145.     if($verbose) echo($message);
  146.     if(is_null($log_handle)) return;
  147.     fwrite($log_handle,$message);
  148.     return;
  149. }
  150.  
  151. function build_url($token,$fields,$group_id,$until,$since){
  152.     /*
  153.     Given an access token, an array with fields list, an integer limit and
  154.     a group id this function builds an URL to query the graph API.
  155.     */
  156.  
  157.     global $fb_api_ver,$fb_time_step;
  158.  
  159.     return("https://graph.facebook.com/v$fb_api_ver/$group_id/feed?fields=".implode(',',$fields)."&limit=1250&access_token=$token&until=$until&since=$since");
  160. }
  161.  
  162. function http_request($URL){
  163.     /*
  164.     Send HTTP request and retrieve the response. If get an http error, it
  165.     tries again $max_tries times every $delay milliseconds
  166.     */
  167.     global $curl_handle;
  168.     $out = false;
  169.  
  170.     curl_setopt($curl_handle, CURLOPT_URL, $URL);
  171.     $out = curl_exec($curl_handle);
  172.     $resp =curl_getinfo($curl_handle,CURLINFO_HTTP_CODE);
  173.     logthis("Resp code: $resp, ",false);
  174.  
  175.     if($resp == 200) {
  176.         logthis("success!");
  177.         return $out;
  178.     }
  179.  
  180.     logthis("failed to open URL :(");
  181.         return false;
  182. }
  183.  
  184. function writeout($record,$out_handle){
  185.     /*
  186.     Write out on a file a record;
  187.     */
  188.     global $table_record_sep,$table_row_sep;
  189.     fwrite($out_handle,implode($table_record_sep,$record).$table_row_sep);
  190. }
  191.  
  192. function parse_and_write($json_response,$out_handle){
  193.         /*
  194.         This function parse the json code and writes on file a CVS table
  195.         with the data.
  196.         Returns the URL pointer to the next "page"
  197.         */
  198.         global $stored,$post_limit,$table_record_sep,$reactions;
  199.        
  200.         if(!$json_response) return false;
  201.         $out = json_decode($json_response);
  202.  
  203.         foreach($out->{'data'} as $post){
  204.             $post_id = $post->{'id'};
  205.             if(property_exists($post,'from')){
  206.                 $user_id = $post->{'from'}->{'id'};
  207.                 $user_name = str_replace($table_record_sep,' ',$post->{'from'}->{'name'});
  208.             } else {
  209.                 $user_id = 'N/A';
  210.                 $user_name = 'N/A';
  211.             }
  212.             $type = $post->{'type'};
  213.             $created = explode('+',$post->{'created_time'});
  214.             if($reactions){
  215.                 $likes = $post->{'reactions'}->{'summary'}->{'total_count'};
  216.             } else {
  217.                 $likes = $post->{'likes'}->{'summary'}->{'total_count'};
  218.             }
  219.             $comments = $post->{'comments'}->{'summary'}->{'total_count'};
  220.             if(isset($post->{'comments'}->{'data'}[0])) {
  221.                 $fc = $post->{'comments'}->{'data'}[0];
  222.                 $fc_user_id = $fc->{'from'}->{'id'};
  223.                 $fc_user_name = $fc->{'from'}->{'name'};
  224.             } else {
  225.                 $fc_user_id = null;
  226.                 $fc_user_name = null;
  227.             }
  228.             writeout([$post_id,$user_id,$user_name,$type,$created[0],$likes,$comments,$fc_user_id,$fc_user_name],$out_handle);
  229.             $stored++;
  230.             if(($stored >= $post_limit) && $post_limit) return false;
  231.         }
  232.        
  233.         if(isset($out->{'paging'}->{'next'})) return $out->{'paging'}->{'next'};
  234.         return false;
  235. }
  236.  
  237. function mysql_import($input_filename){
  238.     global $MYSQL_host, $MYSQL_username, $MYSQL_password, $MYSQL_db, $MYSQL_port, $MYSQL_table, $table_record_sep_esc, $table_row_sep_esc, $MYSQL_fields_list;
  239.    
  240.     logthis("Opening MySql connection...");
  241.     $MyHandle = @new mysqli($MYSQL_host, $MYSQL_username, $MYSQL_password, $MYSQL_db, $MYSQL_port);
  242.     if($MyHandle->connect_error) {
  243.         $mess = 'Error while connection (ErrNo: '.$MyHandle->connect_errno.' - '.$MyHandle->connect_error.")!\nCannot connect to database!";
  244.         logthis($mess);
  245.         return;
  246.     }
  247.     $MyHandle->set_charset("utf8");
  248.    
  249.     logthis("Executing LOAD DATA query...");
  250.    
  251.     $out = $MyHandle->query("LOAD DATA LOCAL INFILE '$input_filename' INTO TABLE $MYSQL_table FIELDS TERMINATED BY '$table_record_sep_esc' LINES TERMINATED BY '$table_row_sep_esc' (".implode(",",$MYSQL_fields_list).")");
  252.    
  253.     if(!$out) {
  254.         logthis("Error while importing data into MySQL table! ".$MyHandle->error);
  255.         return;
  256.     }
  257.    
  258.     logthis("Imported! ".$MyHandle->warning_count." warnings.");
  259.    
  260.     $MyHandle->close();
  261. }
  262.  
  263. $log_handle = fopen($log_filename,"w");     //Open log file
  264. $outfile = fopen($out_filename,"w");        //Open csv out file
  265. $curl_handle = curl_init();
  266. curl_setopt($curl_handle, CURLOPT_HEADER, 0);  
  267. curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true);  
  268.  
  269. logthis("Hi!");                             //Welcome message
  270.  
  271. logthis("Collecting data and saving to file...");
  272. $stored = 0;
  273. $cursor = intval($argv[3]);
  274. $to = time();
  275. $p_URL = '';
  276. $curr_step = $fb_time_step;
  277. $ok = 0;
  278. while($cursor<=$to) {
  279.     $tries = 0; $resp = false;
  280.     if($ok > 5) $curr_step = $fb_time_step*2; else $curr_step = $curr_step*2;
  281.     while(($tries < $http_tries) && (!$resp)){
  282.         $curr_step = intval($curr_step/2);
  283.         if($curr_step < $fb_time_step/4) $curr_step = $fb_time_step/4;
  284.         $tries++;  
  285.         $curr_url = build_url($fb_access_token,$fb_fileds,$fb_group_id,$cursor+$curr_step,$cursor);
  286.         if($show_urls) $p_URL = " ".$curr_url." ";
  287.         logthis("Tryng to get data (try $tries of $http_tries, step = ".round($curr_step/60.0,1)." mins)$p_URL... ",false);
  288.         $resp = http_request($curr_url);
  289.         if($resp == false) $ok = 0; else $ok++;
  290.         usleep($http_delay*1000);
  291.     }
  292.     if(!$resp) break;
  293.     parse_and_write($resp,$outfile);
  294.     $cursor = $cursor+$curr_step;
  295.     logthis("Stored $stored posts. Cursore: ".date("c",$cursor)."\n");
  296. }
  297.  
  298. fclose($outfile);                                                       //Close output file
  299. logthis("$stored posts was saved successfully");
  300.  
  301. if($mysql_auto) mysql_import($out_filename);
  302.  
  303. logthis("bye!");        //Goodbye message
  304. curl_close($curl_handle);
  305. fclose($log_handle);    //Close log file
  306. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement