Linux C程序操作Mysql 調用PHP採集淘寶商品

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include "/usr/local/include/mysql/mysql.h"
#define MAX_COLUMN_LEN 32
#define THREAD_NUM 20//線程數
int threads = 0;
pthread_t thread[THREAD_NUM];
pthread_mutex_t mut;//線程鎖
int count=0,vod_count=0,number = 0;
int *goods_id[1000000];
void *thread1(int thread_id)
{
int sleepsec;
while (number < count){;
char shell_cmd[50];
printf("number:%d\tthread_id=%d\tid=%s\n", number, thread_id, goods_id[number]);
sprintf(shell_cmd, "/usr/local/bin/php /var/www/9384shop/cron/goodsupdate.php %s", goods_id[number]);//生成shell命令
system(shell_cmd);//調用shell
pthread_mutex_lock(&mut);
number++;
pthread_mutex_unlock(&mut);
}
pthread_exit(NULL);
}

void create_thread(void){
int i,temp;
for (i = 0; i < THREAD_NUM; i++){
if (thread[i] == 0){
if ((temp = pthread_create(&thread[i], NULL, thread1, i)) != 0){
}
else{
threads++;
}
break;
}
}
sleep(1);
}
void thread_wait(void)
{
int i;
/*等待線程結束*/
for (i = 0; i < THREAD_NUM; i++){
if (thread[i] != 0) {
pthread_join(thread[i], NULL);
}
}
}
int main(int argc, char *argv[]){
MYSQL my_connection;
MYSQL_RES *result;
MYSQL_ROW sql_row;
MYSQL_FIELD *fd;
char column[MAX_COLUMN_LEN][MAX_COLUMN_LEN];
int res,flag;
mysql_init(&my_connection);
if (mysql_real_connect(&my_connection, "localhost"
, "root", "202.133", "shop", 3306, NULL, 0)){
printf("connected to mysql.\n");
res = mysql_query(&my_connection, "select id from s_goods where is_off_sale=0 order by id desc limit 1000000");//查詢
printf("select id from s_goods where is_off_sale=0 order by id desc limit 1000000\n");
if (!res){
int i = 0, j;
result = mysql_store_result(&my_connection);//保存查詢到的數據到result
printf("the result number is %lu\n", (unsigned long)mysql_num_rows(result));
count = (unsigned long)mysql_num_rows(result);
while (sql_row = mysql_fetch_row(result))//獲取具體的數據
{
goods_id[i] = (unsigned long)sql_row[0];
i++;
}
}
mysql_close(&my_connection);//斷開連接
while (threads < THREAD_NUM)
create_thread();
thread_wait();
}
else{
mysql_close(&my_connection);//斷開連接
printf("ERROR:can not connect to mysql\n");
}

}

PHP:



<?php
define("OTHER",true);
$host='localhost';
$username='root';
$password='123456';
$db_name='taobao';
$s=microtime(1);
$id=$argv[1];


$con=mysql_connect($host,$username,$password);
mysql_select_db($db_name, $con);
$r=mysql_fetch_array(mysql_query('SELECT url,price FROM s_goods where id='.$id),MYSQL_ASSOC);
mysql_close($con);
$oldprice=$r['price'];
$rs=getPrice($r['url']);
$t=microtime(1)-$s;
$r=array();
$r[]=date('Y-m-d H:i:s');
$r[]=$id;
$r[]=ceil($t*1000)/1000;
if($rs=='soldout'){
$r[]="OutStock";
$con=mysql_connect($host,$username,$password);
mysql_select_db($db_name, $con);
mysql_query("UPDATE s_goods SET is_off_sale=1 WHERE id=".$id);
mysql_close($con);
}
elseif($rs===false) $r[]= 'FALSE';
else{
$r[]=$oldprice;
$r[]=isset($rs['price'])?$rs['price']:'';
$r[]=isset($rs['seller_nick'])?$rs['seller_nick']:'';
$r[]=isset($rs['taobao_shop_id'])?$rs['taobao_shop_id']:'';
$r[]=isset($rs['shop_name'])?$rs['shop_name']:'';
$r[]=isset($rs['sales'])?$rs['sales']:'';
$r[]=isset($rs['taobao_cid'])?$rs['taobao_cid']:'';
$r[]=isset($rs['merchandis_score'])?$rs['merchandis_score']:'';
$r[]=isset($rs['merchandis_total'])?$rs['merchandis_total']:'';
$a=array();
//$rs['is_off_sale']=0;
foreach ($rs as $k=>$v){
if(!empty($v)){
$a[]="$k='$v'";
}
}
$a[]="update_time='".date('Y-m-d H:i:s')."'";
$con=mysql_connect($host,$username,$password);
mysql_select_db($db_name, $con);
mysql_query("set names utf8");
mysql_query("UPDATE s_goods SET ".implode(',',$a)." WHERE id=".$id);
mysql_close($con);
}
$h=fopen('/home/staff/www/9384shop/cron/goodsUpdate.log','a+');

fputcsv($h,$r);
fclose($h);

function getPrice($url){
$rs=array();
preg_match('/[&|\?]id=(\d+)/',$url,$id);
$id=$id[1];
$c=curls($url,true);
$content = $c['content'];
if(empty($content)) exit;
$content=mb_convert_encoding($content,"UTF-8","gbk");
$lastredirectaddr = $c['lastredirectaddr'];
if(preg_match('/noitem\.htm/',$content)||preg_match('/<strong>此寶貝已下架<\/strong>|您查看的商品找不到了|您查看的寶貝不存在,可能已下架或者被轉移/',$content)){
return 'soldout';
}elseif(preg_match("/'reservePrice'\s*:\s*'([\d\.]+?)',/",$content,$price)){
$price = (float)$price[1];
}elseif(preg_match('/price:([\d\.]+?),/',$content,$price)){
$price = (float)$price[1];
}
if(preg_match('/"sellerNickName"\s*:\s*"(.*?)",/',$content,$nick)){
$rs['seller_nick'] = urldecode($nick[1]);
}elseif(preg_match('/sellerNick\s*:\s*"(.*?)",/',$content,$nick)){
$rs['seller_nick'] = $nick[1];
}
if(preg_match('/shopId:"(\d+?)",/',$content,$shopid)){
$rs['taobao_shop_id']=$shopid[1];
}elseif(preg_match('/&shopId=(\d+)&/',$content,$shopid)){
$rs['taobao_shop_id']=$shopid[1];
}
if(preg_match("/'categoryId'\s*:\s*'(\d+?)',/",$content,$cid)){
$rs['taobao_cid'] = (float)$cid[1];
}elseif(preg_match('/"categoryId"\s*:\s*"(\d+?)",/',$content,$cid)){
$rs['taobao_cid'] = (float)$cid[1];
}elseif(preg_match("/\scid:'(\d+?)',/",$content,$cid)){
$rs['taobao_cid'] = (float)$cid[1];
}
if(OTHER){
if(preg_match('/tmall\.com/',$lastredirectaddr)){
if(preg_match('/slogo-shopname.*?>(.*?)<\/a>/',$content,$shopname)){
$rs['shop_name']=json_decode('"'.$shopname[1].'"');
}
if(empty($rs['shop_name'])&&!empty($shopname[1])) $rs['shop_name']=$shopname[1];
if(empty($rs['shop_name'])&&!empty($rs['seller_nick'])) $rs['shop_name']=$rs['seller_nick'];
$url2='http://mdskip.taobao.com/core/initItemDetail.htm?itemId='.$id;
$tmall_info = curls($url2);
preg_match('/"sellCount"\s*:\s*(\d+)/',$tmall_info,$temp);
if ($temp[1]!='') $rs['sales']=$temp[1];
$merchandis=curls("http://dsr.rate.tmall.com/list_dsr_info.htm?callback=a&itemId=".$id);
if(preg_match('/gradeAvg"\s*:\s*([0-9\.]+)/',$merchandis,$m_t))
$rs['merchandis_score']=$m_t[1];
if(preg_match('/rateTotal"\s*:\s*([0-9]+)/',$merchandis,$m_t2))
$rs['merchandis_total']=$m_t2[1];
}else{
if(preg_match('/shopName\s*:\s*"(.*?)",/',$content,$shopname)){

$rs['shop_name']=json_decode('"'.$shopname[1].'"');
}
if(empty($rs['shop_name'])&&!empty($rs['seller_nick'])) $rs['shop_name']=$rs['seller_nick'];
if(preg_match('/sellerId\s*:\s*"(.*?)"/',$content,$sellerid)||preg_match('/userId\':\'(\d+)\'/',$content,$sellerid)){
$sellerid = $sellerid[1];
}
if(preg_match('/sbn=([0-9a-z]+)/',$content,$sbn))
$sbn=$sbn[1];
$url2='http://detailskip.taobao.com/json/ifq.htm?id='.$id.'&sid='.$sellerid.'&sbn='.$sbn.'&q=1&callback=a';
$count_rs = curls($url2);
preg_match('/quanity\s*:\s*(\d+)/',$count_rs,$temp);
if ($temp[1]!='') $rs['sales']=$temp[1];
$merchandis=curls("http://rate.taobao.com/detail_rate.htm?userNumId=$sellerid&auctionNumId=$id&currentPage=1&rateType=1");
if(preg_match('/merchandisScore"\s*:\s*"([0-9\.]+)/',$merchandis,$m_t)) $rs['merchandis_score']=$m_t[1];
else $rs['merchandis_score']=6;
if(preg_match('/merchandisTotal"\s*:\s*([0-9]+)/',$merchandis,$m_t)) $rs['merchandis_total']=$m_t[1];
else $rs['merchandis_total']=0;
}
}
if(!$price){
if(!isset($tmall_info)){
$url2="http://mdskip.taobao.com/core/initItemDetail.htm?itemId=".$id;
$tmall_info=curls($url2);
}
$price_content=json_decode(iconv('gbk','utf-8',preg_replace('/(\d{10,}):/','"${1}":',$tmall_info)),true);
$priceinfo=$price_content['defaultModel']['itemPriceResultDO']['priceInfo'];
$price=array();
if(is_array($priceinfo)){
foreach ($priceinfo as $v){
if($v['price']>0)
$price[]=$v['price'];
if(is_array($v['promotionList'])){
foreach ($v['promotionList'] as $v2){
$p=$v2['extraPromPrice']?$v2['extraPromPrice']:$v2['price'];
if($p>0) $price[]=$p;
}
}
if(is_array($v['suggestivePromotionList'])){
foreach ($v['suggestivePromotionList'] as $v2){
$p=$v2['extraPromPrice']?$v2['extraPromPrice']:$v2['price'];
if($p>0) $price[]=$p;
}
}
}
}
$price=count($price)>0?min($price):false;
}
$rs['price']=$price;
if(count($rs)) return $rs;
else return false;
}
function curls($url,$lastredirectaddr=false,$head=false,$times=1){
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows NT 6.2; WOW64; rv:26.0) Gecko/20100101 Firefox/26.0');
curl_setopt($ch, CURLOPT_REFERER,'http://www.tmall.com/');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//設置輸出方式, 0爲自動輸出返回的內容, 1爲返回輸出的內容,但不自動輸出.
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30); //timeout on connect
curl_setopt($ch, CURLOPT_TIMEOUT, 30); //timeout on response
curl_setopt($ch, CURLOPT_HEADER, $head);//是否輸出頭信息,0爲不輸出,非零則輸出
curl_setopt($ch, CURLOPT_MAXREDIRS, 50 );
curl_setopt($ch, CURLOPT_URL, $url);
$count_rs = curl_exec($ch);
if($count_rs === false){
echo 'Curl error: ' . curl_error($ch)."\n";
exit;
}
if($lastredirectaddr) $count_rs=array('content'=>$count_rs,'lastredirectaddr'=>curl_getinfo($ch,CURLINFO_EFFECTIVE_URL));
curl_close($ch);
if($count_rs!=''||$count_rs['content']!='') return $count_rs;
elseif($times<3) return curls($url,$lastredirectaddr,$head,$times+1);
else return false;
}










轉自:http://www.cnblogs.com/lywy510/p/3696177.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章