Exemplo n.º 1
0
        public static async Task <TwitterPost[]> FetchNewPosts(SocialStreamPointSaved saved)
        {
            //Create a query. First, find all users that we'd like to use
            string usersQuery = "";

            foreach (var account in Program.config.social_pages)
            {
                if (account.platform == BotConfigFile_SocialAccountType.Twitter)
                {
                    if (usersQuery.Length != 0)
                    {
                        usersQuery += " OR ";
                    }
                    usersQuery += "from:" + account.username;
                }
            }

            //Combine this with the last post to produce a full query
            string finalQuery = $"?since_id={saved.twitter_latest_refresh_id}&q={System.Web.HttpUtility.UrlEncode(usersQuery)}";

            //Use the Twitter API
            TwitterPostContainer container = await SearchPosts(finalQuery);

            saved.twitter_latest_refresh_id = container.search_metadata.max_id;

            return(container.statuses);
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            Console.WriteLine("Loading the configuration file...");
            config = JsonConvert.DeserializeObject <BotConfigFile>(File.ReadAllText("config.json"));

            Console.WriteLine("Loading the saved stream data...");
            saved_stream = new SocialStreamPointSaved();
            if (File.Exists("saved_stream.json"))
            {
                saved_stream = JsonConvert.DeserializeObject <SocialStreamPointSaved>(File.ReadAllText("saved_stream.json"));
            }

            Console.WriteLine("Opening the database...");
            db       = new LiteDatabase("database.db");
            db_users = db.GetCollection <DiscordUserData>("members");

            Console.WriteLine("Starting the Discord bot...");
            RunBot().GetAwaiter().GetResult();
        }
Exemplo n.º 3
0
        public static async Task RefreshSocial()
        {
            //Log
            Program.LogMessage("social-worker", "Refreshing posts...");

            try
            {
                //Grab all and wait
                SocialStreamPointSaved stream      = Program.saved_stream;
                Task <TwitterPost[]>   twitterTask = TwitterApi.FetchNewPosts(stream);
                //Task<List<Tuple<InstaMedia, BotConfigFile_SocialAccount>>> instagramTask = InstagramApi.GetNewPosts(stream);
                Task <List <Tuple <YouTubePost, BotConfigFile_SocialAccount> > > youtubeTask = YouTubeApi.FetchNewVideos(stream);

                //Wait for all to complete
                Task.WaitAll(twitterTask /*, instagramTask*/, youtubeTask);

                //Now, process all of these and send them to Discord
                DiscordChannel channel = await Program.discord.GetChannelAsync(Program.config.notification_channel);

                foreach (var post in twitterTask.Result)
                {
                    await channel.SendMessageAsync(embed : SocialEmbedCreator.CreateTwitterEmbed(post));
                }

                /*foreach (var post in instagramTask.Result)
                 * {
                 *  await channel.SendMessageAsync(embed: SocialEmbedCreator.CreateInstagramEmbed(post.Item1, post.Item2));
                 * }*/
                foreach (var post in youtubeTask.Result)
                {
                    await channel.SendMessageAsync(embed : SocialEmbedCreator.CreateYouTubeEmbed(post.Item1, post.Item2));
                }

                //Save stream
                File.WriteAllText("saved_stream.json", JsonConvert.SerializeObject(stream));
            } catch (Exception ex)
            {
                //Log
                Program.LogMessage("social-worker-error", ex.Message + ex.StackTrace);
            }
        }
        public static async Task <List <Tuple <YouTubePost, BotConfigFile_SocialAccount> > > FetchNewVideos(SocialStreamPointSaved saved)
        {
            //Loop through all social accounts and find their latest videos
            List <Tuple <YouTubePost, BotConfigFile_SocialAccount> > newPosts = new List <Tuple <YouTubePost, BotConfigFile_SocialAccount> >();

            foreach (var account in Program.config.social_pages)
            {
                if (account.platform != BotConfigFile_SocialAccountType.YouTube)
                {
                    continue;
                }

                //Get all posts
                YouTubePost[] posts = await FetchChannel(account);

                //Get the saved data from the stream, if we have it
                DateTime lastTime = DateTime.MinValue;
                if (saved.youtube_latest_post_time.ContainsKey(account.username))
                {
                    lastTime = saved.youtube_latest_post_time[account.username];
                }
                DateTime checkTime = lastTime.AddSeconds(10); //Rounding protection. Not sure if it's actually needed.

                //Knowing that the latest posts start at the beginning, loop through and add new posts
                foreach (var p in posts)
                {
                    if (p.snippet.publishedAt > checkTime)
                    {
                        newPosts.Add(new Tuple <YouTubePost, BotConfigFile_SocialAccount>(p, account));
                    }
                }

                //Update the last time
                if (posts.Length >= 1)
                {
                    if (saved.youtube_latest_post_time.ContainsKey(account.username))
                    {
                        saved.youtube_latest_post_time[account.username] = posts[0].snippet.publishedAt;
                    }
                    else
                    {
                        saved.youtube_latest_post_time.Add(account.username, posts[0].snippet.publishedAt);
                    }
                }
            }

            return(newPosts);
        }
        public static async Task <List <Tuple <InstaMedia, BotConfigFile_SocialAccount> > > GetNewPosts(SocialStreamPointSaved saved)
        {
            //Login and create the API wrapper
            var api = InstaApiBuilder.CreateBuilder().SetUser(new InstagramApiSharp.Classes.UserSessionData
            {
                UserName = Program.config.instagram_auth.username,
                Password = Program.config.instagram_auth.password
            }).Build();
            var logInResult = await api.LoginAsync();

            //Find new posts
            List <Tuple <InstaMedia, BotConfigFile_SocialAccount> > newPosts = new List <Tuple <InstaMedia, BotConfigFile_SocialAccount> >();

            foreach (var account in Program.config.social_pages)
            {
                if (account.platform != BotConfigFile_SocialAccountType.Instagram)
                {
                    continue;
                }

                //Fetch posts
                var userMedias = await api.UserProcessor.GetUserMediaAsync(account.username, InstagramApiSharp.PaginationParameters.MaxPagesToLoad(1));

                //Get the saved data from the stream, if we have it
                DateTime lastTime = DateTime.MinValue;
                if (saved.instagram_latest_post_time.ContainsKey(account.username))
                {
                    lastTime = saved.instagram_latest_post_time[account.username];
                }
                DateTime checkTime = lastTime.AddSeconds(10); //Rounding protection. Not sure if it's actually needed.

                //We know that the latest posts are first, so go through until we get the last post that was sent.
                for (int i = 0; i < userMedias.Value.Count; i += 1)
                {
                    var media = userMedias.Value[i];
                    if (media.TakenAt > checkTime)
                    {
                        newPosts.Add(new Tuple <InstaMedia, BotConfigFile_SocialAccount>(media, account));
                    }
                }

                //Update the last time
                if (userMedias.Value.Count >= 1)
                {
                    if (saved.instagram_latest_post_time.ContainsKey(account.username))
                    {
                        saved.instagram_latest_post_time[account.username] = userMedias.Value[0].TakenAt;
                    }
                    else
                    {
                        saved.instagram_latest_post_time.Add(account.username, userMedias.Value[0].TakenAt);
                    }
                }
            }

            return(newPosts);
        }