get timeline of 2 (or more) users from Twitter
I am trying to get the timeline of 2 or more users in a list, and then order by the date and display in a listview
at the moment I have the following :-
Contacts searchTerm1 = cmbContact.SelectedItem as Contacts;
if (searchTerm1 != null)
{
contactList = GetCurrentContacts(searchTerm1.CatID);
foreach (var contact in contactList)
{
GetTweetResults(contact.Username);
foreach (var contactTweet in _contactTweets)
{
_allContactTweets.Add(contactTweet);
}
}
TweetList1.ItemsSource = _allContactTweets;
}
And I am getting the tweets Async
private void GetTweetResults(string user)
{
WebClient twitterAccess = new WebClient();
twitterAccess.DownloadStringCompleted += new DownloadStringCompletedEventHandler(twitterAccess_DownloadStringCompleted);
twitterAccess.DownloadStringAsync(new Uri("http://api.twitter.com/1/statuses/user_timeline.xml?screen_name=" + user));
}
private void twitterAccess_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs args)
{
try
{
XElement xmlTweets = XElement.Parse(args.Result);
var contactTweets = (from tweet in xmlTweets.Descendants("status")
select new ContactTweet
{
ProfileImage =
tweet.Element("user").Element("profile_image_url").Value,
TweetText = tweet.Element("text").Value,
UserName = tweet.Element("user").Element("screen_name").Value,
Created = (tweet.Element("created_at").Value.ParseDateTime())
}).ToList();
foreach (var contactTweet in contactTweets)
{
_contactTweets.Add(contactTweet);
}
}
catch (Exception exception)
{
MessageBox.Show("Error downloading tweets - " + exception.Message);
}
}
}
Now my idea is, in GetTweetResults, to wait for the twitterAccess_DownloadStringCompleted to finish and then continue the process, however I do not know how to do it.
Can anyone tell me how to achieve this?
Thanks for your help and time
UPDATE
Managed to get it down to
WebClient twitterAccess = new WebClient();
twitterAccess.DownloadStringCompleted += new DownloadStringCompletedEventHandler(twitterAccess_DownloadStringCompleted);
twitterAccess.DownloadStringAsync(new Uri("http://api.twitter.com/1/statuses/user_timeline.xml?screen_name=" + contactList1[0].Username));
WebClient twitterAccess2 = new WebClient();
twitterAccess2.DownloadStringCompleted += new DownloadStringCompletedEventHandler(twitterAccess_DownloadStringCompleted);
twitterAccess2.DownloadStringAsync(new Uri("http://api.twitter.com/1/statuses/user_timeline.xml?screen_name=" + contactList1[1].Username));
Now i need to find a way to loop in the contact开发者_Python百科List1 and generate the WebClient dynamically.....
UPDATE 2
Problem solved
foreach (var contact in contactList)
{
WebClient twitterAccess = new WebClient();
twitterAccess.DownloadStringCompleted += new DownloadStringCompletedEventHandler(twitterAccess_DownloadStringCompleted);
twitterAccess.DownloadStringAsync(new Uri("http://api.twitter.com/1/statuses/user_timeline.xml?screen_name=" + contact.Username));
}
foreach (var contact in contactList) { WebClient twitterAccess = new WebClient(); twitterAccess.DownloadStringCompleted += new DownloadStringCompletedEventHandler(twitterAccess_DownloadStringCompleted);
twitterAccess.DownloadStringAsync(new Uri("http://api.twitter.com/1/statuses/user_timeline.xml?screen_name=" + contact.Username));
}
精彩评论