1 Replies - 2212 Views - Last Post: 13 February 2012 - 04:19 AM

#1 Core  Icon User is offline

  • using System.Linq;
  • member icon

Reputation: 782
  • View blog
  • Posts: 5,101
  • Joined: 08-December 08

Twitter API: Get Mentions

Posted 21 August 2009 - 11:07 PM

Description: The System.Collections, System.Collections.Generic and System.ServiceModel.Syndication namespaces should be referenced in the project.Returns the 20 most recent mentions (status containing @username) for the authenticating user.

public enum Optional
        {
            None,
            SinceID,
            MaxID,
            Count,
            Page
        }

        ///<summary>
        ///Returns the 20 most recent mentions (status containing @username) for the authenticating user.
        ///http://apiwiki.twitter.com/Twitter-REST-API-Method%3A-statuses-mentions
        ///</summary>
        ///<param name="username">Twitter account username.</param>
        ///<param name="password">Twitter account password.</param>
        ///<param name="optional">The type of optional parameter to apply to the search.</param>
        ///<param name="optValue">The value of the optional parameter applied to the search. For Optional.None the field can hold any value. Required.</param>
        public static ArrayList Statuses_Mentions(string username, string password, Optional optional, string optValue)
        {
            try
            {
                ArrayList searchResults = new ArrayList();
                KeyValuePair<KeyValuePair<string, string>, string> foundElement = new KeyValuePair<KeyValuePair<string, string>, string>();
                KeyValuePair<string, string> additionalElement = new KeyValuePair<string, string>();

                XmlUrlResolver xmlResolver = new XmlUrlResolver();
                xmlResolver.Credentials = new NetworkCredential(username, password);

                XmlReaderSettings xmlSettings = new XmlReaderSettings();
                xmlSettings.XmlResolver = xmlResolver;

                switch (optional)
                {
                    case (Optional.None):
                        {
                            XmlReader feedReader = XmlReader.Create("http://twitter.com/statuses/mentions.atom", xmlSettings);
                            SyndicationFeed feed = SyndicationFeed.Load(feedReader);


                            foreach (var feedItem in feed.Items)
                            {
                                additionalElement = new KeyValuePair<string, string>(feedItem.Id.ToString(), feedItem.Authors[0].ToString());
                                foundElement = new KeyValuePair<KeyValuePair<string, string>, string>(additionalElement, feedItem.Title.Text);
                                searchResults.Add(foundElement);
                            }

                            feedReader.Close();
                            break;
                        }
                    case (Optional.SinceID):
                        {
                            XmlReader feedReader = XmlReader.Create("http://twitter.com/statuses/mentions.atom?since_id=" + optValue, xmlSettings);
                            SyndicationFeed feed = SyndicationFeed.Load(feedReader);


                            foreach (var feedItem in feed.Items)
                            {
                                additionalElement = new KeyValuePair<string, string>(feedItem.Id.ToString(), feedItem.Authors[0].ToString());
                                foundElement = new KeyValuePair<KeyValuePair<string, string>, string>(additionalElement, feedItem.Title.Text);
                                searchResults.Add(foundElement);
                            }

                            feedReader.Close();
                            break;
                        }
                    case (Optional.MaxID):
                        {
                            XmlReader feedReader = XmlReader.Create("http://twitter.com/statuses/mentions.atom?max_id=" + optValue, xmlSettings);
                            SyndicationFeed feed = SyndicationFeed.Load(feedReader);


                            foreach (var feedItem in feed.Items)
                            {
                                additionalElement = new KeyValuePair<string, string>(feedItem.Id.ToString(), feedItem.Authors[0].ToString());
                                foundElement = new KeyValuePair<KeyValuePair<string, string>, string>(additionalElement, feedItem.Title.Text);
                                searchResults.Add(foundElement);
                            }

                            feedReader.Close();
                            break;
                        }
                    case (Optional.Count):
                        {
                            XmlReader feedReader = XmlReader.Create("http://twitter.com/statuses/mentions.atom?count=" + optValue, xmlSettings);
                            SyndicationFeed feed = SyndicationFeed.Load(feedReader);


                            foreach (var feedItem in feed.Items)
                            {
                                additionalElement = new KeyValuePair<string, string>(feedItem.Id.ToString(), feedItem.Authors[0].ToString());
                                foundElement = new KeyValuePair<KeyValuePair<string, string>, string>(additionalElement, feedItem.Title.Text);
                                searchResults.Add(foundElement);
                            }

                            feedReader.Close();
                            break;
                        }
                    case (Optional.Page):
                        {
                            XmlReader feedReader = XmlReader.Create("http://twitter.com/statuses/mentions.atom?page=" + optValue, xmlSettings);
                            SyndicationFeed feed = SyndicationFeed.Load(feedReader);


                            foreach (var feedItem in feed.Items)
                            {
                                additionalElement = new KeyValuePair<string, string>(feedItem.Id.ToString(), feedItem.Authors[0].ToString());
                                foundElement = new KeyValuePair<KeyValuePair<string, string>, string>(additionalElement, feedItem.Title.Text);
                                searchResults.Add(foundElement);
                            }

                            feedReader.Close();
                            break;
                        }
                }
                return searchResults;
            }
            catch
            {
                return null;
            }
        }


Is This A Good Question/Topic? 0
  • +

Replies To: Twitter API: Get Mentions

#2 DjShow  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 13-February 12

Re: Twitter API: Get Mentions

Posted 13 February 2012 - 04:19 AM

where can download the working project cause i imported the collections and ServiceModel and it still gives me a collections error
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1