2011年1月

wininet源代码获取例子

wininet源代码获取例子

#include <windows.h>
#include <wininet.h>
#pragma comment(lib, "wininet")

#define ID_URLEDITBOX 1
#define ID_GETBUTTON 2
#define ID_SHOWEDITBOX 3
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT ("BtnLook") ;
HWND hwnd ;
MSG msg ;
WNDCLASS wndclass ;

wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName = szAppName ;

if (!RegisterClass (&wndclass))
{
MessageBox (NULL, TEXT ("This program requires Windows NT!"),
szAppName, MB_ICONERROR) ;
return 0 ;
}

hwnd = CreateWindow (szAppName, TEXT ("WinINet源代码获取"),
WS_OVERLAPPEDWINDOW,
GetSystemMetrics (SM_CXSCREEN) / 4, GetSystemMetrics (SM_CYSCREEN) / 4,
GetSystemMetrics (SM_CXSCREEN) / 2, GetSystemMetrics (SM_CYSCREEN) / 2,
NULL, NULL, hInstance, NULL) ;

ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;

while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
return msg.wParam ;
}

LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static HWND hwndUrlEdt,hwndGetBtn,hwndShowEdt;
HINTERNET hConnect,hRequest,hSession;
LPTSTR lpszHost;
char szBuffer[1024*1024/10];
TCHAR szStr[100];
DWORD dwSize=0;
DWORD dwDownloaded;
static LPCTSTR pAcceptType[] = { TEXT("Accept: */*"), NULL };

switch (message)
{
case WM_CREATE:
hwndUrlEdt = CreateWindow (TEXT ("EDIT"), NULL,
WS_CHILD | WS_VISIBLE | WS_BORDER | ES_LEFT | ES_AUTOHSCROLL ,
20,20,500,20, hwnd, (HMENU)ID_URLEDITBOX,
((LPCREATESTRUCT) lParam) -> hInstance, NULL) ;

hwndGetBtn = CreateWindow (TEXT ("BUTTON"), TEXT("GO"),
WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON ,
530,20,30,20, hwnd, (HMENU)ID_GETBUTTON,
((LPCREATESTRUCT) lParam) -> hInstance, NULL) ;

hwndShowEdt = CreateWindow (TEXT ("EDIT"), TEXT(""),
WS_CHILD | WS_VISIBLE | WS_BORDER | WS_HSCROLL | WS_VSCROLL | ES_AUTOHSCROLL | ES_AUTOVSCROLL | ES_MULTILINE ,
20,60,600,250, hwnd, (HMENU)ID_SHOWEDITBOX,
((LPCREATESTRUCT) lParam) -> hInstance, NULL) ;

return 0;
case WM_COMMAND:
if(LOWORD(wParam) == ID_GETBUTTON)
{
GetWindowText(hwndUrlEdt,szStr,sizeof(szStr)/sizeof(TCHAR));
hSession = InternetOpen(TEXT("testWinINet"), PRE_CONFIG_INTERNET_ACCESS, NULL, INTERNET_INVALID_PORT_NUMBER, 0);
hConnect = InternetOpenUrl(hSession,szStr,NULL,0,INTERNET_FLAG_DONT_CACHE,0);
ZeroMemory(szBuffer,sizeof(szBuffer));
while (InternetReadFile(hConnect,szBuffer,sizeof(szBuffer),&dwDownloaded))
{
if (0==dwDownloaded) break;
szBuffer[dwDownloaded]=0;
}
SetWindowText(hwndShowEdt,szBuffer);
ZeroMemory(szBuffer,sizeof(szBuffer));
}
return 0;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}

程序缺点很多,1是点击“GO”之后,获取源代码的时候会卡住,因为没有用多线程,
2,存储源代码的内存空间受到了限制,csdn上面的人说受vc编译器堆栈大小的限制,超出了就stack overflow了。目前我只能使用临时文件存储的方法来解决。
3是子窗口都是使用代码写的,所以字体都是默认的fixedsys"system",中文字体十分难看。
4是字符串如果不是gb2312和纯英文的,其他字符串全部乱码,如上图就是一个最好的例证。

说了这么多缺点,说出来以后“深造”后再修正吧。嘎嘎~~

wordpress“快速发布”微博

将wordpress的后台“快速发布”修改为微博消息发布: 因为对wordpress插件机制还不是很熟悉,直接修改源代码,而不是通过插件实现。 方法如下: 修改wp-includes/post.php,将2218行

$post_name = sanitize_title($post_title);

修改为

if(!isset($postarr['quickpress_post_ID']))//快速发布
{
$post_name = sanitize_title($post_title);
}
else//快速发布
{
$post_name = time();
$post_title =mb_substr($post_content, 0,30);
}

修改wp-admin/includes/dashboard.php,455行

<input type="text" name="tags_input" id="tags-input" tabindex="3" value="<?php echo get_tags_to_edit( $post->ID ); ?>" />

修改为

<input type="text" name="tags_input" id="tags-input" tabindex="3" value="<?php echo get_tags_to_edit( $post->ID ); ?>microblogging" />

这样在后台就能发“微博”了。 这次彻底放弃使用twitter等微博的原因是 1.没人气,不好玩。 2.不能写代码片段,发图等问题。 3.字数限制,和2接近的一个原因。

导入twitter数据到wordpress

twitter的备份工具很多,tweet backup,tweettake,导出的格式有txt,csv,没有我想要的sql脚本。所以使用这个使用这个项目的代码:http://code.google.com/p/tbackup/
将proccess.php的第75行到97行的代码修改为:

//$text = iconv('UTF-8', CHARSET, $status->text);
//$text = str_replace('"', '""', $text);
//fwrite($file, $datestr . '$$$' . $status->id . '$$$' . $text . '$$$' . chr(13) . chr(10));
$text = $status->text;
$link_id = mysql_connect("localhost", "user", "pass") or
die("Could not connect: " . mysql_error());
mysql_select_db('wpdb');
mysql_query("set names utf8");
 
$title = substr($text, 0, 60);
 
$sql = "INSERT INTO `jq_posts` (`ID`, `post_author`, `post_date`, `post_date_gmt`, `post_content`, `post_title`,
`post_category`, `post_excerpt`, `post_status`, `comment_status`, `ping_status`, `post_password`, `post_name`, `to_ping`,
`pinged`, `post_modified`, `post_modified_gmt`, `post_content_filtered`, `post_parent`, `guid`, `menu_order`, `post_type`,
`post_mime_type`, `comment_count`)
VALUES (NULL, '1', '$datestr', CONVERT_TZ('$datestr','+08:00','+00:00'), '$text', '$title', '0', '', 'publish', 'open', 'open', '', unix_timestamp('$datestr'), '', '',
'$datestr',CONVERT_TZ('$datestr','+08:00','+00:00') , '', '0', unix_timestamp('$datestr'), '0', 'post', '', '0');";
mysql_query($sql);
$insert_id = mysql_insert_id($link_id);
mysql_query("insert into wp_term_relationships values ($insert_id,770,0) "); //将770改为对应的term_taxonomy_id
mysql_query("update wp_term_taxonomy set count=count+1 where term_taxonomy_id=770 ");//将770改为对应的term_taxonomy_id 

将所有代码上传到网站目录,输入twitter ID,点Start,等待一段时间,所有数据就导入了wordpress。

至此搞了四天,将饭否,嘀咕,twitter的数据都导入到了wordpress博客之中。所有的微博数据在 http://tunps.com/tag/microblogging

整合嘀咕数据到wordpress中

首先使用去年写的嘀咕备份工具(现有出现了乱码的问题,空了再解决)下载所有嘀咕数据(消息,图片),然后导入到本地的mysql数据中,把数据库中的status表导入到你wordpress数据库中:

整合嘀咕数据到wordpress中

然后将以下代码保存为php文件,放在网站上运行一次:

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<?php
$img_new_path = 'http://tunps.com/wp-content/myuploads/2011/01/';
$link_id = mysql_connect("localhost", "user", "pass") or
die("Could not connect: " . mysql_error());
mysql_select_db('wp');
mysql_query("set names utf8");
$result = mysql_query("select * from status");
while ($row = mysql_fetch_assoc($result)) {
if ($row['in_reply_to_status_id'] == '0')
{
$title = substr($row['text'], 0, 60);
if ($row['text'] == '') {
$title = '无标题';
}
$sql = "INSERT INTO `jq_posts` (`ID`, `post_author`, `post_date`, `post_date_gmt`, `post_content`, `post_title`,
`post_category`, `post_excerpt`, `post_status`, `comment_status`, `ping_status`, `post_password`, `post_name`, `to_ping`,
`pinged`, `post_modified`, `post_modified_gmt`, `post_content_filtered`, `post_parent`, `guid`, `menu_order`, `post_type`,
`post_mime_type`, `comment_count`)
VALUES (NULL, '1', '$row[created_at]', CONVERT_TZ('$row[created_at]','+08:00','+00:00'), '$row1', '$title', '0', '', 'publish', 'open', 'open', '', unix_timestamp('$row[created_at]'), '', '',
'$row[created_at]',CONVERT_TZ('$row[created_at]','+08:00','+00:00') , '', '0', unix_timestamp('$row[created_at]'), '0', 'post', '', '0');";
mysql_query($sql);
$insert_id = mysql_insert_id($link_id);
mysql_query("insert into wp_term_relationships values ($insert_id,770,0) "); //将770改为对应的term_taxonomy_id
mysql_query("update wp_term_taxonomy set count=count+1 where term_taxonomy_id=770 ");//将770改为对应的term_taxonomy_id

//如果不含图片/插入一行记录post
if ($row['picPath'] == '') {

} else
{
//http://pic.minicloud.com.cn:80/file/10/02/58/10/201003/7868626c6629a84bbe60828fbaeac0d3_100x75.jpg
$url = $row['picPath'];
$filename = substr($url, strrpos($url, '/') + 1, strlen($url));
$filename = str_replace('100x75', '640x480', $filename);
$url = $img_new_path . $filename;
$sql = "INSERT INTO `jq_posts` (`ID`, `post_author`, `post_date`, `post_date_gmt`, `post_content`, `post_title`,
`post_category`, `post_excerpt`, `post_status`, `comment_status`, `ping_status`, `post_password`, `post_name`, `to_ping`,
`pinged`, `post_modified`, `post_modified_gmt`, `post_content_filtered`, `post_parent`, `guid`, `menu_order`, `post_type`,
`post_mime_type`, `comment_count`)
VALUES (NULL, '1', '$row[created_at]', CONVERT_TZ('$row[created_at]','+08:00','+00:00'), '', '$filename', '0', '', 'inherit', 'open', 'open', '', '$filename', '', '',
'$row[created_at]',CONVERT_TZ('$row[created_at]','+08:00','+00:00') , '', '$insert_id', '$url', '0', 'attachment', 'image/jpeg', '0');";
mysql_query($sql);
$img_id = mysql_insert_id($link_id);
//文章插入图片
$img_tag = '<br /><a href="' . $url . '"><img class="alignleft size-full wp-image-' . $img_id . '" title="' . $filename . '" src="' . $url . '" alt="" /></a>';
$img_tag = addslashes($img_tag);
$sql = "update wp_posts set post_content=concat(post_content,'$img_tag') where ID=$insert_id";
mysql_query($sql);
}
}
}
echo 'ok';
?>

完成!

整合饭否数据到wordpress中

饭否是2007.5上线,2009.7.8饭否关闭,原因大家都知道,从此大量的饭否粉丝苦苦等待饭否的重新上线。饭否是我第一个使用的微博服务,我也在等待之列,在2010.3.1饭否推出了一个数据导出功能。最近做了一个决定:将我的所有微博消息(饭否、嘀咕、twitter)整合到此wordpress博客中,让这里博客、微博进行“合体”。本人先后使用饭否(2007-11-09到2009-7-06),嘀咕(2009-04-16到2010-03-23),twitter(2008-10-08至今)。先把饭否整合到wordpress中,饭否的数据来自备份的文件中:

album.html //照片  
favorite_1.html  //收藏
privatemsg_receive_1.html //私信收件箱
privatemsg_sent_1.html //私信发件箱
**status_1.html **//消息

我在饭否里面没有怎么使用照片,私信不用公开了,所有只备份消息。不知道为啥每个文件都是“_1”,估计1000条消息为一个文件,我消息没有超过1000。分析了一下status_1.html,发现加载了yui 2 javascript框架,但折腾了一下午发现yui 2用得很不上手。只有用jquery,全部代码如下:

$(document).ready(function(){
var list=$("ol li");
list.each(function(i){
if($(".content")[i].innerHTML.substring(0,1) == '@')//忽略@别人的聊天消息
{}
else
{
var gmtTimeStr=$(".stamp")[i].childNodes[0].getAttribute("stime");
var date_arr=gmtTimeStr.split(" ");
var year = date_arr[5];
var month = date_arr[1];
var day = date_arr[2];
var time = date_arr[3];
var time_arr = time.split(":");
var hour = time_arr[0];
var minute = time_arr[1];
var second = time_arr[2];
switch(month)
{
//January February March April May June July August September October November December//默写不出来鸟,老了。
case 'Jan': month = 01; break;
case 'Feb': month = 02; break;
case 'Mar': month = 03; break;
case 'Apr': month = 04; break;
case 'May': month = 05; break;
case 'Jun': month = 06; break;
case 'Jul': month = 07; break;
case 'Aug': month = 08; break;
case 'Sep': month = 09; break;
case 'Oct': month = 10; break;
case 'Nov': month = 11; break;
case 'Dec': month = 12; break;
}
var newGmtTimeStr = year + '-' + month + '-' + day + ' ' + time;
var localTimeObj = new Date(year,month,day,hour,minute,second);
localTimeObj.setHours(localTimeObj.getHours()+8);
var post_name = localTimeObj.getTime();
var date_arr=localTimeObj.toString().split(" ");
var year = date_arr[3];
var month = date_arr[1];
var day = date_arr[2];
var time = date_arr[4];
var time_arr = time.split(":");
var hour = time_arr[0];
var minute = time_arr[1];
var second = time_arr[2];
switch(month)
{
//January February March April May June July August September October November December
case 'Jan': month = 0; break;
case 'Feb': month = 1; break;
case 'Mar': month = 2; break;
case 'Apr': month = 3; break;
case 'May': month = 4; break;
case 'Jun': month = 5; break;
case 'Jul': month = 6; break;
case 'Aug': month = 7; break;
case 'Sep': month = 8; break;
case 'Oct': month = 9; break;
case 'Nov': month = 10; break;
case 'Dec': month = 11; break;
}
var localTime = year + '-' + month + '-' + day + ' ' + hour+':'+minute+':'+second;
var content = $(".content")[i].innerHTML;
content = content.replace(/\'/g,"\\'");
var title = content.substring(0,60);
$("#sql").text( $("#sql").text() + "INSERT INTO `tunps`.`jq_posts` (`ID`, `post_author`, `post_date`, `post_date_gmt`, `post_content`, `post_title`, `post_category`, `post_excerpt`, `post_status`, `comment_status`, `ping_status`, `post_password`, `post_name`, `to_ping`, `pinged`, `post_modified`, `post_modified_gmt`, `post_content_filtered`, `post_parent`, `guid`, `menu_order`, `post_type`, `post_mime_type`, `comment_count`) VALUES (NULL, '1', '"+localTime+"', '"+newGmtTimeStr+"', '"+content+"', '"+title+"', '0', '', 'publish', 'open', 'open', '', '"+post_name+"', '', '', '0000-00-00 00:00:00', '0000-00-00 00:00:00', '', '0', '', '0', 'post', '', '0');" + '\n');
}
});
});

以上js最好在chrome下面运行,因为太消耗内存了,chrome运行5秒钟,cpu占用50%左右,内存100多MB。差点被javascript的Date对象搞晕了,之所以耗内存cpu资源是因为代码的问题,可以解决问题就行,优化就不考虑了。
生成如下的sql脚本:

整合饭否数据到wordpress中

拷贝到phpmyadmin中运行就导入到了wordpress中。wordpress日志默认是按wp_posts.post_date而非post_date_gmt,这点需要注意。 如果你想让所有饭否的数据都放在一个单独的目录并命名为“碎碎念”,你可以在wordpress后台新建一个“碎碎念”的目录(数据库里面会在wp_terms和wp_term_taxonomy新建一行数据,记录下wp_term_taxonomy.term_taxonomy_id,我的是770,记录下饭否数据插入wp_posts的起止ID,我的是1736到2454,然后写个php文件:

$link_id = mysql_connect("localhost", "用户名", "密码") or
die("Could not connect: " . mysql_error());
mysql_select_db('数据库名字');
$i=1736;
while($i<=2454)
{
mysql_query("insert into wp_term_relationships values ($i,770,0) ");
$i++;
}

上传到网站,运行这个php脚本,目的是为了让所有的饭否数据都归入“碎碎念”目录。
然后手动修改wp_term_taxonomy.count为饭否的消息条数就可以了。