<?php include '../src/gwtdata.php'; try { $email = "*****@*****.**"; $passwd = "******"; # Dates must be in valid ISO 8601 format. $daterange = array("2012-01-10", "2012-01-12"); $gdata = new GWTdata(); if ($gdata->LogIn($email, $passwd) === true) { $sites = $gdata->GetSites(); foreach ($sites as $site) { $gdata->SetDaterange($daterange); $gdata->DownloadCSV($site); } } } catch (Exception $e) { die($e->getMessage()); }
/** function/mdp_gwt_query_function * Usage: download csv from google and insert data into wp_mdp_gwt_queries table * also attached to the wp_cron by mdp_gwt_hook * Arg(0): null * Return: void */ public static function mdp_gwt_query_function() { global $wpdb; $mdpWebmasterTools = new mdpWebmasterTools(); $dir = MDP_GWT_DIR . 'csv/' . get_current_blog_id() . '/'; // test directory exists if not create if (!file_exists($dir)) { if (!mkdir($dir, 0775, true)) { echo 'Error making directory check your permissions'; } } include MDP_GWT_DIR . 'gwt_data.php'; //erase old files to keep directory clean if (count(glob("{$dir}*.csv")) > 0) { array_map('unlink', glob("{$dir}*.csv")); } //download the csv files from google try { /* If hardcoded, don't forget trailing slash! */ $gdata = new GWTdata(); if ($gdata->LogIn(get_option('mdp_gwt_email'), get_option('mdp_gwt_password')) === true) { $sites = $gdata->GetSites(); if (get_option('mdp_gwt_recency')) { $gdata->SetDaterange(array(date('Y-m-d', time() - 86400 * get_option('mdp_gwt_recency')), date('Y-m-d', time()))); } foreach ($sites as $site) { $gdata->DownloadCSV($site, $dir); } /* uncomment to see downloaded files $files = $gdata->GetDownloadedFiles(); foreach ($files as $file) { print "Saved $file\n"; } */ } } catch (Exception $e) { die($e->getMessage()); } //assign the table name $table_name_queries = $wpdb->base_prefix . "mdp_gwt_query"; $table_name_pages = $wpdb->base_prefix . "mdp_gwt_pages"; $table_name_keywords = $wpdb->base_prefix . "mdp_gwt_keywords"; $table_name_external_links = $wpdb->base_prefix . "mdp_gwt_external_links"; $table_name_internal_links = $wpdb->base_prefix . "mdp_gwt_internal_links"; //scan the csv directory for files if ($files = scandir($dir)) { //iterate through the file line by line foreach ($files as $file) { if (preg_match('#QUERIES#is', $file)) { $mdpWebmasterTools->mdp_truncate($table_name_queries); $thistype = 'queries'; } elseif (preg_match('#PAGES#is', $file)) { $mdpWebmasterTools->mdp_truncate($table_name_pages); $thistype = 'pages'; } elseif (preg_match('#KEYWORDS#is', $file)) { $mdpWebmasterTools->mdp_truncate($table_name_keywords); $thistype = 'keywords'; } elseif (preg_match('#EXTERNAL_LINKS#is', $file)) { $mdpWebmasterTools->mdp_truncate($table_name_external_links); $thistype = 'external'; } elseif (preg_match('#INTERNAL_LINKS#is', $file)) { $mdpWebmasterTools->mdp_truncate($table_name_internal_links); $thistype = 'internal'; } else { continue; } $query_file = $dir . $file; $row_query = 1; if (($handle_query = fopen($query_file, "r")) !== FALSE) { while (($data_query = fgetcsv($handle_query, 2000, ",")) !== FALSE) { if ($row_query++ === 1) { $allrows = count($data_query) > 6; continue; } switch ($thistype) { case 'queries': $mdpWebmasterTools->mdp_replace_table_pages_query($data_query, $table_name_queries, $allrows); break; case 'pages': $mdpWebmasterTools->mdp_replace_table_pages_query($data_query, $table_name_pages, $allrows); break; case 'keywords': $mdpWebmasterTools->mdp_replace_table_keywords($data_query, $table_name_keywords); break; case 'external': $mdpWebmasterTools->mdp_replace_table_external_links($data_query, $table_name_external_links); break; case 'internal': $mdpWebmasterTools->mdp_replace_table_internal_links($data_query, $table_name_internal_links); break; } } } fclose($handle_query); } } }