Created
May 10, 2011 05:52
-
-
Save code-walkers/963968 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static wxString &GetNextJsonObject(wxString &input,wxString &obj) | |
{ | |
char tempDst[BUFFER_SIZE]; | |
memset(tempDst,0,BUFFER_SIZE); | |
char *s1 = strstr( input.c_str(),"\"retweet_count\":") ; | |
if( NULL == s1)return obj; | |
strncat(tempDst,input.c_str(),s1 - input.c_str()); | |
char *s2 = strstr(s1 ,"}"); | |
if(NULL == s2)return obj; | |
strncat(tempDst,s1 ,s2 - s1 + 1); | |
wxString x(tempDst); | |
obj = x; | |
input.Remove(0,s2 - input.c_str() + 1); | |
return obj; | |
} | |
void MyFrame::AnalyzeTweet(wxString &myTweet) | |
{ | |
if( myTweet.MakeLower().Contains(_T("good")) ){ | |
freq_table["goodluck"]++; | |
} | |
if( myTweet.MakeLower().Contains(_T("bad")) ){ | |
freq_table["badluck"]++; | |
} | |
} | |
void MyFrame::OnAnalyzeTweets(wxCommandEvent &event) | |
{ | |
wxJSONValue value; | |
wxJSONReader reader; | |
wxString jsonText,nextObj; | |
wxArrayString jsonObjs; | |
size_t len; | |
bool done; | |
char *tempBuffer; | |
tempBuffer = (char *)malloc(BUFFER_SIZE); if(NULL == tempBuffer)return; | |
wxURL url("http://stream.twitter.com/1/statuses/filter.json?track=black cat luck"); | |
url.GetProtocol().SetUser(_T("aporuri")); | |
url.GetProtocol().SetPassword(_T("ad12#$")); | |
url.GetProtocol().SetTimeout(3); | |
if(url.GetError() == wxURL_NOERR){ | |
wxInputStream *in_stream; | |
in_stream = url.GetInputStream(); | |
wxFileOutputStream outFile("C:\\osb.json"); | |
if(!outFile.IsOk()){ | |
wxMessageBox(_T("failed to create OP stream")); | |
return; | |
} | |
if(NULL != in_stream){ | |
outFile.Write(*in_stream); // <- Error compiling here. | |
int n = outFile.LastWrite(); | |
delete in_stream; | |
in_stream = NULL; | |
} | |
outFile.Close(); | |
} | |
wxFileInputStream fis(_T("C:\\osb.json"));//fileName); | |
if (!fis.Ok()) { | |
wxMessageBox("Cannot open the input."); | |
return ; | |
} | |
// read the file | |
do { | |
fis.Read(tempBuffer, BUFFER_SIZE); | |
len = fis.LastRead(); | |
done = len < BUFFER_SIZE; | |
wxString tempString(tempBuffer,len); | |
jsonText += tempString; | |
} while (!done); | |
//Accumulate JSON objects | |
// | |
int numTweets = 0; | |
int corrTweets = 0; | |
do{ | |
nextObj = _T(""); | |
jsonObjs.Add(GetNextJsonObject(jsonText,nextObj)); | |
int numErrors = reader.Parse( nextObj, &value ); | |
if ( numErrors > 0 ) { | |
corrTweets++; | |
//wxMessageBox( _T("Error reading the input")); | |
} | |
else{ | |
wxString tweet(value["text"].AsCString()); | |
AnalyzeTweet(tweet); | |
numTweets++; | |
} | |
//wxMessageBox(tweet); | |
}while(!jsonText.IsEmpty() && !nextObj.IsEmpty()); | |
wxMessageBox(wxString::Format("corr = %d,numTweets = %d, good - %d , bad - %d",corrTweets, numTweets,this->freq_table[_T("goodluck")], this->freq_table[_T("badluck")])); | |
freq_table.clear(); | |
free(tempBuffer); | |
return; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment