csdn博客搬家后处理

最近在搞csdn的搬家,博客已经搬完,但是这样csdn和我网站会有同样的博客内容,因此需要更新csdn每篇博客的内容,

博客已搬家至360converter,此篇博文的链接:xxxxxxx

因为文章数共达500多,近600,手动让人受不了,因此打算使用程序完成,最后写csdn博客更新程序,一个晚上搞定,当然是机器自己搞,我不用管了。

其思想是使用脚本从wordpress的数据库中得到所有博文的链接和标题,存在一个文件中,然后再通过一个脚本获取csdn所有博文的链接和标题,然后使用鼠标事件,完成

  1. 访问csdn博文
  2. 点击编辑
  3. 合成更新内容
  4. 拷贝粘贴
  5. 保存
  6. 访问下一篇

然后循环往复,直到所有文章完成。

代码列出如下:

// csdn-updateDlg.h

// csdn-updateDlg.h : header file
//

#pragma once
#include <map>


// CcsdnupdateDlg dialog
class CcsdnupdateDlg : public CDialog
{
// Construction
public:
    CcsdnupdateDlg(CWnd* pParent = NULL);    // standard constructor

// Dialog Data
    enum { IDD = IDD_CSDNUPDATE_DIALOG };

    protected:
    virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support


// Implementation
protected:
    HICON m_hIcon;

    // Generated message map functions
    virtual BOOL OnInitDialog();
    afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
    afx_msg void OnPaint();
    afx_msg HCURSOR OnQueryDragIcon();
    DECLARE_MESSAGE_MAP()
public:
    afx_msg void OnBnClickedStart();
    afx_msg void OnBnClickedStop();
    void SetClipBoardContent( std::string source );
    void PasteEvent();
    void CopyEvent();
    void ClickEvent( int x, int y );
    void GotoURL( std::string url );
    void UpdateProc( void );
    void GetWPlist( void );
    void GetCSDNlist( void );
    void ScrollDown();
    void SelectALL();
    bool VerifyReadPageLoaded();
    bool VerifyEditPageLoaded( std::string& );
    bool VerifySavePageLoaded( void );
    void Log( std::string& );
    static unsigned int __stdcall UpdateProcProxy( void* );
    void MonitorCursorProc( void );
    static unsigned int __stdcall MonitorCursorProcProxy( void* );

private:
    HANDLE m_hUpdateThread;
    HANDLE m_hMonitorThread;
    std::map<std::string, std::string> m_wplist;
    std::map<std::string, std::string> m_csdnlist;
    std::string m_hLogMessage;

    afx_msg void OnBnClickedButton3();
};

 

// csdn-updateDlg.cpp

// csdn-updateDlg.cpp : implementation file
//

#include "stdafx.h"
#include "csdn-update.h"
#include "csdn-updateDlg.h"
#include <process.h> 
#include <fstream>
#include <string>
#include <locale>


#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
    CAboutDlg();

// Dialog Data
    enum { IDD = IDD_ABOUTBOX };

    protected:
    virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support

// Implementation
protected:
    DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()


// CcsdnupdateDlg dialog

 


CcsdnupdateDlg::CcsdnupdateDlg(CWnd* pParent /*=NULL*/)
    : CDialog(CcsdnupdateDlg::IDD, pParent)
{
    m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CcsdnupdateDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CcsdnupdateDlg, CDialog)
    ON_WM_SYSCOMMAND()
    ON_WM_PAINT()
    ON_WM_QUERYDRAGICON()
    //}}AFX_MSG_MAP
    ON_BN_CLICKED(IDC_START, &CcsdnupdateDlg::OnBnClickedStart)
    ON_BN_CLICKED(IDC_STOP, &CcsdnupdateDlg::OnBnClickedStop)
    ON_BN_CLICKED(IDC_BUTTON3, &CcsdnupdateDlg::OnBnClickedButton3)
END_MESSAGE_MAP()


// CcsdnupdateDlg message handlers

BOOL CcsdnupdateDlg::OnInitDialog()
{
        
    CDialog::OnInitDialog();

    // Add "About…" menu item to system menu.

    // IDM_ABOUTBOX must be in the system command range.
    ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
    ASSERT(IDM_ABOUTBOX < 0xF000);

    CMenu* pSysMenu = GetSystemMenu(FALSE);
    if (pSysMenu != NULL)
    {
        CString strAboutMenu;
        strAboutMenu.LoadString(IDS_ABOUTBOX);
        if (!strAboutMenu.IsEmpty())
        {
            pSysMenu->AppendMenu(MF_SEPARATOR);
            pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
        }
    }

    // Set the icon for this dialog.  The framework does this automatically
    //  when the application's main window is not a dialog
    SetIcon(m_hIcon, TRUE);            // Set big icon
    SetIcon(m_hIcon, FALSE);        // Set small icon

    // TODO: Add extra initialization here

    unsigned uid = 0;
    m_hMonitorThread = ( HANDLE )_beginthreadex(NULL, 0, MonitorCursorProcProxy, (void*)this, 0, &uid );
    if ( m_hMonitorThread <= 0 )
    {
        AfxMessageBox( L"failed to create update thread" );
    }
    SetWindowPos( &wndTopMost, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);

    // read list
    GetWPlist();
    GetCSDNlist();

    return TRUE;  // return TRUE  unless you set the focus to a control
}

void CcsdnupdateDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
    if ((nID & 0xFFF0) == IDM_ABOUTBOX)
    {
        CAboutDlg dlgAbout;
        dlgAbout.DoModal();
    }
    else
    {
        CDialog::OnSysCommand(nID, lParam);
    }
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CcsdnupdateDlg::OnPaint()
{
    if (IsIconic())
    {
        CPaintDC dc(this); // device context for painting

        SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

        // Center icon in client rectangle
        int cxIcon = GetSystemMetrics(SM_CXICON);
        int cyIcon = GetSystemMetrics(SM_CYICON);
        CRect rect;
        GetClientRect(&rect);
        int x = (rect.Width() – cxIcon + 1) / 2;
        int y = (rect.Height() – cyIcon + 1) / 2;

        // Draw the icon
        dc.DrawIcon(x, y, m_hIcon);
    }
    else
    {
        CDialog::OnPaint();
    }
}

// The system calls this function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CcsdnupdateDlg::OnQueryDragIcon()
{
    return static_cast<HCURSOR>(m_hIcon);
}


void CcsdnupdateDlg::OnBnClickedStart()
{
    // TODO: Add your control notification handler code here
    unsigned uid = 0;
    m_hUpdateThread = ( HANDLE )_beginthreadex(NULL, 0, UpdateProcProxy, (void*)this, 0, &uid );
    if ( m_hUpdateThread <= 0 )
    {
        AfxMessageBox( L"failed to create update thread" );
    }
}

void CcsdnupdateDlg::OnBnClickedStop()
{
    // TODO: Add your control notification handler code here
    TerminateThread( m_hUpdateThread, 0 );
    CloseHandle( m_hUpdateThread );
}

void CcsdnupdateDlg::PasteEvent()
{
    INPUT ip;
    ip.type = INPUT_KEYBOARD;
    ip.ki.wScan = 0;
    ip.ki.time = 0;
    ip.ki.dwExtraInfo = 0;

    // Press the "Ctrl" key
    ip.ki.wVk = VK_CONTROL;
    ip.ki.dwFlags = 0; // 0 for key press
    SendInput(1, &ip, sizeof(INPUT));

    // Press the "V" key
    ip.ki.wVk = 'V';
    ip.ki.dwFlags = 0; // 0 for key press
    SendInput(1, &ip, sizeof(INPUT));

    // Release the "V" key
    ip.ki.wVk = 'V';
    ip.ki.dwFlags = KEYEVENTF_KEYUP;
    SendInput(1, &ip, sizeof(INPUT));

    // Release the "Ctrl" key
    ip.ki.wVk = VK_CONTROL;
    ip.ki.dwFlags = KEYEVENTF_KEYUP;
    SendInput(1, &ip, sizeof(INPUT));
}

void CcsdnupdateDlg::CopyEvent()
{
    INPUT ip;
    ip.type = INPUT_KEYBOARD;
    ip.ki.wScan = 0;
    ip.ki.time = 0;
    ip.ki.dwExtraInfo = 0;

    // Press the "Ctrl" key
    ip.ki.wVk = VK_CONTROL;
    ip.ki.dwFlags = 0; // 0 for key press
    SendInput(1, &ip, sizeof(INPUT));

    // Press the "c" key
    ip.ki.wVk = 'C';
    ip.ki.dwFlags = 0; // 0 for key press
    SendInput(1, &ip, sizeof(INPUT));

    // Release the "c" key
    ip.ki.wVk = 'C';
    ip.ki.dwFlags = KEYEVENTF_KEYUP;
    SendInput(1, &ip, sizeof(INPUT));

    // Release the "Ctrl" key
    ip.ki.wVk = VK_CONTROL;
    ip.ki.dwFlags = KEYEVENTF_KEYUP;
    SendInput(1, &ip, sizeof(INPUT));

    Log( std::string("copy finished"));
}

void CcsdnupdateDlg::ClickEvent( int x, int y )
{
    SetCursorPos( x, y );
    mouse_event( MOUSEEVENTF_LEFTDOWN, x, y, 0, 0 );
    mouse_event( MOUSEEVENTF_LEFTUP, x, y, 0, 0 );
}

void CcsdnupdateDlg::SelectALL()
{
    INPUT ip;
    ip.type = INPUT_KEYBOARD;
    ip.ki.wScan = 0;
    ip.ki.time = 0;
    ip.ki.dwExtraInfo = 0;

    // Press the "Ctrl" key
    ip.ki.wVk = VK_CONTROL;
    ip.ki.dwFlags = 0; // 0 for key press
    SendInput(1, &ip, sizeof(INPUT));

    // Press the "V" key
    ip.ki.wVk = 'A';
    ip.ki.dwFlags = 0; // 0 for key press
    SendInput(1, &ip, sizeof(INPUT));

    // Release the "V" key
    ip.ki.wVk = 'A';
    ip.ki.dwFlags = KEYEVENTF_KEYUP;
    SendInput(1, &ip, sizeof(INPUT));

    // Release the "Ctrl" key
    ip.ki.wVk = VK_CONTROL;
    ip.ki.dwFlags = KEYEVENTF_KEYUP;
    SendInput(1, &ip, sizeof(INPUT));
}

void CcsdnupdateDlg::GotoURL( std::string url )
{
    // click address bar
    ClickEvent( 141, 41 );

    Sleep( 3000 );

    // paste URL
    PasteEvent();

    Sleep( 3000 );

    // click enter
    INPUT ip;
    ip.type = INPUT_KEYBOARD;
    ip.ki.wScan = 0;
    ip.ki.time = 0;
    ip.ki.dwExtraInfo = 0;

    // Press the "V" key
    ip.ki.wVk = VK_RETURN;
    ip.ki.dwFlags = 0; // 0 for key press
    SendInput(1, &ip, sizeof(INPUT));

    // Release the "V" key
    ip.ki.wVk = VK_RETURN;
    ip.ki.dwFlags = KEYEVENTF_KEYUP;
    SendInput(1, &ip, sizeof(INPUT));
}

void CcsdnupdateDlg::ScrollDown()
{
    INPUT in;
    in.type = INPUT_MOUSE;
    in.mi.dx = 0;
    in.mi.dy = 0;
    in.mi.dwFlags = MOUSEEVENTF_WHEEL;
    in.mi.time = 0;
    in.mi.dwExtraInfo = 0;
    in.mi.mouseData = -100 * 120;
    SendInput(1,&in,sizeof(in));
}

void CcsdnupdateDlg::SetClipBoardContent( std::string source )
{
    if(OpenClipboard())
    {
        HGLOBAL clipbuffer;
        WCHAR * buffer;
        EmptyClipboard();
        clipbuffer = GlobalAlloc(GMEM_DDESHARE, (source.length()+1)*2);
        WCHAR temp[512] = { 0 };
        MultiByteToWideChar(CP_ACP, 0, source.c_str(), source.length()+1, temp, 512);
        buffer = (WCHAR*)GlobalLock(clipbuffer);
        memcpy( buffer, temp, ( wcslen( temp ) + 1 ) * 2 );
        GlobalUnlock(clipbuffer);
        SetClipboardData(CF_UNICODETEXT,clipbuffer);
        CloseClipboard();
        GlobalFree(clipbuffer);
    }

}

void CcsdnupdateDlg::UpdateProc( void )
{
    const int nActionInterval = 1000;
    Sleep( 3000 );
    int x = 0;
    int y = 0;


    for( std::map<std::string, std::string>::iterator it = m_csdnlist.begin(); it != m_csdnlist.end(); ++it )
    {

        // get CSDN title and URL
        std::string strTitle = (it->first);
        std::string strUrl = (it->second);
        std::string strWPUrl = "";

        // get wordpress url based on CSDN blog title
        for( std::map<std::string, std::string>::iterator it1 = m_wplist.begin(); it1 != m_wplist.end(); ++it1 )
        {
            if ( 0 == strTitle.compare((it1->first)))
            {
                strWPUrl = (it1->second);
                break;
            }
        }
        if ( "" == strWPUrl )
        {
            Log( std::string( "cannot find title: " ) + strTitle );
            continue;
        }
        // set CSDN URL in clipboard
        SetClipBoardContent( strUrl );

        // first put CSDN blog URL in browser address bar
        Log(std::string("Goto url: ") + strUrl );
        GotoURL( strUrl );
        Sleep( 10000 );
        //VerifyReadPageLoaded();


        // set clipboard content
        std::string source = "博客已搬家至360converter博客平台,此文链接:";
        source += strWPUrl;
        Log(std::string("set clipboard: ") + source );
        SetClipBoardContent( source );

        // click edit
        Log(std::string("click edit button") );
        ClickEvent( 1803, 428 );
        Sleep( 10000 );
        //VerifyEditPageLoaded( strTitle );


        // click editable area
        Log(std::string("click editable area") );
        ClickEvent( 600, 480 );
        Sleep( nActionInterval );
        Log(std::string("select all") );
        SelectALL(); // select all
        Sleep( nActionInterval );
        Log(std::string("paste content") );
        PasteEvent(); // paste
        Sleep( nActionInterval );

        // scroll down to bottom
        Log(std::string("scroll down to bottom") );
        ScrollDown();
        Sleep( nActionInterval );

        // save
        Log(std::string("click save button") );
        ClickEvent( 830, 722 );
        
        // wait for save
        //Sleep( 3000 );
        //VerifySavePageLoaded();
        Log(std::string("wait for save") );
        Sleep( 10000 );
    }

    Log(std::string("all finished") );
    // write log to file
    FILE *pf = fopen("log.txt", "w" );
    if ( NULL != pf )
    {
        fwrite( m_hLogMessage.c_str(), 1, m_hLogMessage.length(), pf );
        fclose( pf );
    }

}

void CcsdnupdateDlg::Log( std::string& strMsg )
{
    m_hLogMessage += strMsg;
    m_hLogMessage += "\r\n";
    SetDlgItemTextA( this->m_hWnd, IDC_LOGMESSAGE2, m_hLogMessage.c_str());
    SendDlgItemMessage( IDC_LOGMESSAGE2, WM_VSCROLL , SB_BOTTOM, 0 );
}

void CcsdnupdateDlg::GetWPlist( void )
{
    std::locale lang("CHS");
    std::ifstream file( L"wplist.txt");
    file.imbue( lang );
    std::string title, url, blank;
    while ( true )
    {
        if (!std::getline(file, title)) // title
        {
            break;
        }
        if (!std::getline(file, url)) // URL
        {
            break;
        }
        m_wplist[title] = url;
        if (!std::getline(file, blank)) // blank
        {
            break;
        }
    }

    return;
}

void CcsdnupdateDlg::GetCSDNlist( void )
{
    std::locale lang("CHS");
    std::ifstream file( L"csdnlist.txt");
    file.imbue( lang );
    std::string title, url, blank;
    while( true )
    {
        if (!std::getline(file, title)) // title
        {
            break;
        }
        if (!std::getline(file, url)) // URL
        {
            break;
        }
        m_csdnlist[title] = url;
        if (!std::getline(file, blank)) // blank
        {
            break;
        }
    }
    return;
}

unsigned int __stdcall CcsdnupdateDlg::UpdateProcProxy( void* pVoid )
{
    CcsdnupdateDlg* pdlg = (CcsdnupdateDlg*)pVoid;
    if ( NULL == pdlg )
    {
        return -1;
    }
    pdlg->UpdateProc();

    return 0;
}


unsigned int __stdcall CcsdnupdateDlg::MonitorCursorProcProxy( void* pVoid )
{
    CcsdnupdateDlg* pdlg = (CcsdnupdateDlg*)pVoid;
    if ( NULL == pdlg )
    {
        return -1;
    }
    pdlg->MonitorCursorProc();

    return 0;
}

void CcsdnupdateDlg::MonitorCursorProc()
{
    while ( true )
    {
        POINT pt;
        GetCursorPos( &pt );
        char buffer1[20];
        _itoa( pt.x, buffer1, 10 );
        SetDlgItemTextA( this->m_hWnd, IDC_XCO, buffer1 );
        char buffer2[20];
        _itoa( pt.y, buffer2, 10 );
        SetDlgItemTextA( this->m_hWnd, IDC_YCO, buffer2 );
        Sleep( 100 );
    }
}

bool CcsdnupdateDlg::VerifySavePageLoaded( void )
{
    int x = 334;
    int y = 475;
    int x1 = 419;
    SetCursorPos( x, y );
    mouse_event( MOUSEEVENTF_LEFTDOWN, x, y, 0, 0 );
    SetCursorPos( x1, y );
    mouse_event( MOUSEEVENTF_LEFTUP, x, y, 0, 0 );
    SetCursorPos( x+10, y );
    mouse_event( MOUSEEVENTF_RIGHTDOWN, x, y, 0, 0 );
    mouse_event( MOUSEEVENTF_RIGHTUP, x, y, 0, 0 );
    Sleep( 500 );
    ClickEvent( 400, 490 );
    Sleep( 500 );

    // copy
    //CopyEvent();

    // get clipboard
    std::string saved = "360converter";
    char * buffer;
    if(OpenClipboard())
    {
        buffer = (char*)GetClipboardData(CF_TEXT);
    }
    bool bsaved = false;
    if ( 0 == saved.compare( buffer ))
    {
        Log( std::string( "saved successfully" ));
        bsaved = true;
    }
    CloseClipboard();

    return bsaved;
}

bool CcsdnupdateDlg::VerifyEditPageLoaded( std::string& title )
{
    // get title from browser
    ClickEvent( 555, 287 );
    Sleep( 5000 );
    SelectALL();
    Sleep( 5000 );
    //CopyEvent();

    SetCursorPos( 555 + 10, 287+10 );
    mouse_event( MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0 );
    mouse_event( MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0 );
    Sleep( 500 );
    ClickEvent( 627, 400 );
    Sleep( 500 );

    // get clipboard
    char * buffer;
    if(OpenClipboard())
    {

        buffer = (char*)GetClipboardData(CF_TEXT);
        Log( std::string( buffer ));
    }
    bool bloaded = false;
    if ( 0 == title.compare( buffer ))
    {
        Log( std::string( "loaded successfully" ));
        bloaded = true;
    }
    CloseClipboard();

    return bloaded;
}

bool CcsdnupdateDlg::VerifyReadPageLoaded()
{
    int x = 69;
    int y = 665;
    int x1 = 90;
    SetCursorPos( x, y );
    mouse_event( MOUSEEVENTF_LEFTDOWN, x, y, 0, 0 );
    SetCursorPos( x1, y );
    mouse_event( MOUSEEVENTF_LEFTUP, x, y, 0, 0 );
    SetCursorPos( x+10, y );
    mouse_event( MOUSEEVENTF_RIGHTDOWN, x, y, 0, 0 );
    mouse_event( MOUSEEVENTF_RIGHTUP, x, y, 0, 0 );
    Sleep( 500 );
    ClickEvent( 124, 683 );
    Sleep( 500 );

    // copy
    //CopyEvent();

    // get clipboard
    std::string blognum = "424"; // total blog number
    char * buffer;
    if(OpenClipboard())
    {
        buffer = (char*)GetClipboardData(CF_TEXT);
    }
    bool bloaded = false;
    if ( 0 == blognum.compare( buffer ))
    {
        Log( std::string( "loaded successfully" ));
        bloaded = true;
    }
    CloseClipboard();

    return bloaded;
}

void CcsdnupdateDlg::OnBnClickedButton3()
{
    // TODO: Add your control notification handler code here
    //int x = 334;
    //int y = 475;
    //int x1 = 419;
    //SetCursorPos( x, y );
    //mouse_event( MOUSEEVENTF_LEFTDOWN, x, y, 0, 0 );
    //SetCursorPos( x1, y );
    //mouse_event( MOUSEEVENTF_LEFTUP, x, y, 0, 0 );
    //SetCursorPos( x+10, y );
    //mouse_event( MOUSEEVENTF_RIGHTDOWN, x, y, 0, 0 );
    //mouse_event( MOUSEEVENTF_RIGHTUP, x, y, 0, 0 );
    //Sleep( 500 );
    //ClickEvent( 400, 490 );
    //Sleep( 500 );

    //// copy
    ////CopyEvent();

    //// get clipboard
    //char * buffer;
    //if(OpenClipboard())
    //{

    //    buffer = (char*)GetClipboardData(CF_TEXT);
    //    ::MessageBoxA( NULL, buffer, 0, 0 );
    //}
    //CloseClipboard();

    // get title from browser
    ClickEvent( 555, 287 );
    Sleep( 5000 );
    SelectALL();
    Sleep( 5000 );
    //CopyEvent();

    SetCursorPos( 555 + 10, 287+10 );
    mouse_event( MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0 );
    mouse_event( MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0 );
    Sleep( 500 );
    ClickEvent( 627, 400 );
    Sleep( 500 );

    // get clipboard
    char * buffer;
    if(OpenClipboard())
    {

        buffer = (char*)GetClipboardData(CF_TEXT);
        Log( std::string( buffer ));
    }
    CloseClipboard();
}

 

工程打包下载:

csdn-update

 

版权所有,禁止转载. 如需转载,请先征得博主的同意,并且表明文章出处,否则按侵权处理.

    分享到:

留言

你的邮箱是保密的 必填的信息用*表示