Exemplo n.º 1
0
        // STATUS [ June 24, 2019 ]: this works
        /// <summary>
        ///     Gets last X tweets from one Twitter list based on filters set in search and current sinceId
        /// </summary>
        /// <remarks>
        ///     Uses tweetinvi to get the list, then tweets in that list, and then filters the tweets
        ///     Note that this DOES filter any of the tweets returned (see previous method for no filters)
        ///     See: https://github.com/linvi/tweetinvi/wiki/Twitter-Lists
        /// </remarks>
        /// <param name="listName">
        ///     The name of the list as defined by the user (e.g., 'Baseball')
        /// </param>
        /// <param name="screenName">
        ///     The screen name / Twitter handle / user name that you want
        /// </param>
        /// <param name="includeRetweets">
        ///     True if you want to include retweets; False if you don't;
        /// </param>
        /// <example>
        ///     var tweets = GetTweetsFromTwitterListAfterSinceId("baseball", "mr_baseball", false);
        /// </example>
        /// <returns>
        ///     IEnumerable of Tweetinvi.Models.ITweets
        /// </returns>
        public IEnumerable <ITweet> GetTweetsFromTwitterListAfterSinceId(string listName, string screenName, bool includeRetweets)
        {
            ITwitterList list = TwitterList.GetExistingList(listName, screenName);

            var currentSinceId = ReadCurrentSinceIdFromTxtFile();

            var tweetsParameters = new GetTweetsFromListParameters
            {
                SinceId         = currentSinceId,
                IncludeRetweets = includeRetweets,
            };
            IEnumerable <ITweet> tweets = list.GetTweets(tweetsParameters);

            long newSinceId = currentSinceId;

            foreach (ITweet tweet in tweets)
            {
                if (tweet.Id > newSinceId)
                {
                    newSinceId = tweet.Id;
                }
            }
            WriteSinceIdToTxtFile(newSinceId);
            // PrintTweetInfoFromIEnumerableITweet(tweets);
            return(tweets);
        }
Exemplo n.º 2
0
        // STATUS [ June 24, 2019 ]: this works but probably not needed
        public ITwitterListDTO GetITwitterListDTO(string listName, string screenName)
        {
            ITwitterList list            = TwitterList.GetExistingList(listName, screenName);
            var          iTwitterListDTO = list.TwitterListDTO;

            return(iTwitterListDTO);
        }
Exemplo n.º 3
0
        // STATUS [ June 23, 2019 ]: this works
        // not sure if this is really needed - but good to include for testing purposes
        public IEnumerable <IUser> GetSelectedNumberOfMembersOfTwitterList(string listName, string screenName, int countOfMembersToGet)
        {
            ITwitterList        list    = TwitterList.GetExistingList(listName, screenName);
            IEnumerable <IUser> members = list.GetMembers(countOfMembersToGet);

            // PrintListMemberInfo(members);
            return(members);
        }
Exemplo n.º 4
0
        // STATUS [ June 21, 2019 ]: this works
        /// <summary>
        ///     Gets last X? tweets from one Twitter list
        /// </summary>
        /// <remarks>
        ///     Uses tweetinvi to get the list and then tweets in that list
        ///     Note that this does not filter any of the tweets returned (see next method for that)
        ///     See: https://github.com/linvi/tweetinvi/wiki/Twitter-Lists
        /// </remarks>
        /// <param name="listName">
        ///     The name of the list as defined by the user (e.g., 'Baseball')
        /// </param>
        /// <param name="screenName">
        ///     The screen name / Twitter handle / user name that you want
        /// </param>
        /// <example>
        ///     var tweets = GetAllTweetsFromTwitterList("baseball", "mr_baseball");
        /// </example>
        /// <returns>
        ///     IEnumerable of Tweetinvi.Models.ITweets
        /// </returns>
        public IEnumerable <ITweet> GetAllTweetsFromTwitterList(string listName, string screenName)
        {
            var list   = TwitterList.GetExistingList(listName, screenName);
            var tweets = list.GetTweets();

            PrintTweetInfoFromIEnumerableITweet(tweets);
            return(tweets);
        }
Exemplo n.º 5
0
 public void ShouldCreateFilesOfHandlesFromList()
 {
     const string creator      = "EarthPower_D";
     const string listName     = "art";
     const string category     = "Test";
     var          existingList = TwitterList.GetExistingList(listName, creator);
     var          x            = existingList.MemberCount;
 }
Exemplo n.º 6
0
        public static IEnumerable <IUser> GetUsersFromList(string listName, string creator)
        {
            WaitToScore();
            var tweetList   = TwitterList.GetExistingList(listName, creator);
            var usersInList = GetUsersInList(tweetList);

            return(usersInList);
        }
Exemplo n.º 7
0
        // STATUS [ June 24, 2019 ]: this works
        /// <summary>
        ///     Gets list of twitter IUsers that are members of a Twitter list
        /// </summary>
        /// <remarks>
        ///     Uses tweetinvi to get ITwitterList list
        ///     See: https://github.com/linvi/tweetinvi/wiki/Twitter-Lists
        /// </remarks>
        /// <param name="listName">
        ///     The name of the list as defined by the user (e.g., 'Baseball')
        /// </param>
        /// <param name="screenName">
        ///     The screen name / Twitter handle / user name that you want
        /// </param>
        /// <example>
        ///     var members = GetAllMembersOfTwitterList("baseball","mr_baseball");
        /// </example>
        /// <returns>
        ///     IEnumerable of IUsers
        /// </returns>
        public IEnumerable <IUser> GetAllMembersOfTwitterList(string listName, string screenName)
        {
            ITwitterList        list            = TwitterList.GetExistingList(listName, screenName);
            int                 listMemberCount = list.MemberCount;
            IEnumerable <IUser> members         = list.GetMembers(listMemberCount);

            PrintListMemberInfo(members);
            return(members);
        }
Exemplo n.º 8
0
        public void TwitterList_Lifecycle()
        {
            var loggedUser      = User.GetLoggedUser();
            var newList         = TwitterList.CreateList("myTemporaryList", PrivacyMode.Private, "tmp");
            var userLists       = TwitterList.GetUserSubscribedLists(loggedUser);
            var newListVerify   = TwitterList.GetExistingList(newList);
            var updateParameter = new TwitterListUpdateParameters();

            updateParameter.Name = "piloupe";
            newListVerify.Update(updateParameter);
            newListVerify.Destroy();
        }
Exemplo n.º 9
0
        // STATUS [ June 24, 2019 ]: THIS DOES NOT WORK
        public IEnumerable <ITweet> GetTweetsFromTwitterListAfterDate(string listName, string screenName, bool includeRetweets, DateTime date)
        {
            ITwitterList list = TwitterList.GetExistingList(listName, screenName);

            var tweetsParameters = new GetTweetsFromListParameters
            {
                // Until = new DateTime(2019,06,23),
                IncludeRetweets = includeRetweets,
            };
            IEnumerable <ITweet> tweets = list.GetTweets(tweetsParameters);

            // PrintTweetInfoFromIEnumerableITweet(tweets);
            return(tweets);
        }
Exemplo n.º 10
0
        private static void Main()
        {
            // Let's try literally try/catching the whole thing
            try
            {
                // Project requirements:
                // 1. TweetInvi library installed from NuGet - Install-Package TweetInvi
                // 2. Setup Twitter application to get a workable CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET (https://apps.twitter.com)
                // 3. Setup incoming Slack webhook to a channel in the group you want, to display the information you pass
                // 4. Collect credentials and pass them as environment variables; either using -e and docker run, or into the docker-compose file included.
                // That's it!

                Credentials creds = new Credentials();
                creds.ConsumerKey       = Environment.GetEnvironmentVariable("ConsumerKey");
                creds.ConsumerSecret    = Environment.GetEnvironmentVariable("ConsumerSecret");
                creds.AccessToken       = Environment.GetEnvironmentVariable("AccessToken");
                creds.AccessTokenSecret = Environment.GetEnvironmentVariable("AccessTokenSecret");
                creds.Channel           = Environment.GetEnvironmentVariable("Channel");
                creds.Username          = Environment.GetEnvironmentVariable("Username");
                creds.EmojiIcon         = Environment.GetEnvironmentVariable("EmojiIcon");
                creds.WebhookURL        = Environment.GetEnvironmentVariable("WebhookURL");

                // Load credentials into Tweetinvi auth.
                Auth.SetUserCredentials(creds.ConsumerKey, creds.ConsumerSecret, creds.AccessToken, creds.AccessTokenSecret);
                var authenticatedUser = Tweetinvi.User.GetAuthenticatedUser();

                // Enable Automatic RateLimit handling; not too sure what this does to be honest, but is recommended by the Tweetinvi dev.
                RateLimit.RateLimitTrackerMode = RateLimitTrackerMode.TrackAndAwait;

                // Get list membership info
                var listOwner        = Environment.GetEnvironmentVariable("ListOwner");
                var listToTrack      = Environment.GetEnvironmentVariable("ListToTrack");
                var chosenList       = TwitterList.GetExistingList(listToTrack, listOwner);
                var allUsersFromList = chosenList.GetMembers();

                // Attempting to make a stream of the users. Works, but gives tweets @ too.
                var tweetStream = Tweetinvi.Stream.CreateFilteredStream();
                foreach (var user in allUsersFromList)
                {
                    tweetStream.AddFollow(user);
                }

                // Initialize and say hello.
                SayHello(creds);

                // Write how many users you got to start with.
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.WriteLine(initString + "\n");
                Console.WriteLine("Using list: {0}", chosenList.FullName);
                Console.WriteLine("Have loaded {0} users into the stream...", tweetStream.FollowingUserIds.Count);

                // Start the stream; set a couple params.
                tweetStream.MatchOn       = Tweetinvi.Streaming.MatchOn.Follower;
                tweetStream.StallWarnings = true;

                tweetStream.MatchingTweetReceived += async(succsender, succargs) =>
                {
                    // Something broke in the actual setting to match followers, so doublecheck here.
                    if (succargs.MatchOn == tweetStream.MatchOn)
                    {
                        string tweetText = (succargs.Tweet.Url + " - " + DateTime.UtcNow).ToString();

                        // Created and then serialize JSON payload, which Slack expects. Pass as StringContent to HttpReader.
                        dynamic jsonPayload = new JObject();
                        jsonPayload.text       = tweetText;
                        jsonPayload.channel    = creds.Channel;
                        jsonPayload.username   = creds.Username;
                        jsonPayload.icon_emoji = creds.EmojiIcon;

                        var stringContent = new StringContent(JsonConvert.SerializeObject(jsonPayload), Encoding.UTF8, "application/json");

                        HttpResponseMessage response = await httpClient.PostAsync(creds.WebhookURL, stringContent);

                        HttpContent responseContent = response.Content;
                        using (var reader = new StreamReader(await responseContent.ReadAsStreamAsync()))
                        {
                            // Write the response output to console. Debug mode.
                            Console.ForegroundColor = ConsoleColor.Green;
                            Console.WriteLine(await reader.ReadToEndAsync() + " - " + DateTime.UtcNow + " - tweet from " + succargs.Tweet.CreatedBy.ScreenName + " matched and sent");
                        }
                    }
                    else
                    {
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.WriteLine("no - " + DateTime.UtcNow + " - tweet from " + succargs.Tweet.CreatedBy.ScreenName + " rejected for not matching");
                    }
                };

                // Start stream if it's not running; attempt to get it to auto-restart
                do
                {
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    Console.WriteLine(DateTime.UtcNow + " - Starting stream...\n");
                    tweetStream.StartStreamMatchingAllConditions();
                    Console.WriteLine(DateTime.UtcNow + " - Stream stopped...\n");
                } while (tweetStream.StreamState != Tweetinvi.Models.StreamState.Running);
            }

            catch (Exception e)
            {
                Console.WriteLine("{0}", e);
                File.WriteAllText(@"log.txt", DateTime.UtcNow + e.ToString());
            }

            Console.WriteLine(DateTime.UtcNow + "Main function ending, even though this shouldn't happen!");
        }