Пример #1
0
        // Async workers
        
        private void GroupsWork(Object sender, DoWorkEventArgs args)
        {
            // Do not access the form's BackgroundWorker reference directly. 
            // Instead, use the reference provided by the sender parameter.
            var bw = sender as BackgroundWorker;

            // Extract the argument.
            var param = args.Argument as GroupPostsParam;
            var groupId = param.gid;
            if (param.from <= param.to)
            {
                postsFromDate = param.from;
                postsToDate = param.to;
            }
            else
            {
                postsFromDate = param.to;
                postsToDate = param.from;
            }

            var context = new VkRestApi.VkRestContext(userId, authToken);
            var sb = new StringBuilder();
            isRunning = true;

            // gather group statistics
            if (param.justStats)
            {

                bw.ReportProgress(1, "Getting group stats");

                sb.Length = 0;
                sb.Append("group_id=").Append(Math.Abs(groupId).ToString()).Append("&");
                sb.Append("date_from=").Append(this.postsFromDate.ToString("yyyy-MM-dd")).Append("&");
                sb.Append("date_to=").Append(this.postsToDate.ToString("yyyy-MM-dd"));
                context.Parameters = sb.ToString();
                Debug.WriteLine("Download parameters: " + context.Parameters);

                // call VK REST API
                vkRestApi.CallVkFunction(VkFunction.StatsGet, context);

                return;
            }

            // create stream writers
            // 1) group posts
            // group posts
            IEntity e = new Post();
            String fileName = Utils.GenerateFileName(WorkingFolderTextBox.Text, groupId, e);
            groupPostsWriter = File.CreateText(fileName);
            Utils.PrintFileHeader(groupPostsWriter, e);

            postsWithComments.Clear(); // reset comments reference list
            likes.Clear(); // reset likes
            posters.Clear(); // reset posters
            posterIds.Clear(); // clear poster ids
            visitorIds.Clear(); // clear visitor ids

            totalCount = 0;
            currentOffset = 0;
            step = 1;

            long timeLastCall = 0;

            // get group posts 100 at a time and store them in the file
            // request group posts
            while (isRunning)
            {
                if (bw.CancellationPending)
                    break;

                if(currentOffset > totalCount)
                {
                    // done
                    break;
                }

                bw.ReportProgress(step, "Getting " + currentOffset + " posts out of " + totalCount);

                sb.Length = 0;
                sb.Append("owner_id=").Append(groupId.ToString()).Append("&");
                sb.Append("offset=").Append(currentOffset).Append("&");
                sb.Append("count=").Append(POSTS_PER_REQUEST).Append("&");
                context.Parameters = sb.ToString();
                Debug.WriteLine("Download parameters: " + context.Parameters);

                context.Cookie = currentOffset.ToString();

                // play nice, sleep for 1/3 sec to stay within 3 requests/second limits
                timeLastCall = Utils.sleepTime(timeLastCall); 
                // call VK REST API
                vkRestApi.CallVkFunction(VkFunction.WallGet, context);

                // wait for the user data
                ReadyEvent.WaitOne();

                currentOffset += POSTS_PER_REQUEST;
            }

            groupPostsWriter.Close();

            if (postsWithComments.Count > 0)
            {
                // group comments
                e = new Comment();
                fileName = Utils.GenerateFileName(WorkingFolderTextBox.Text, groupId, e);
                groupCommentsWriter = File.CreateText(fileName);
                Utils.PrintFileHeader(groupCommentsWriter, e);

                // request group comments
                bw.ReportProgress(-1, "Getting comments");
                step = 10000 / postsWithComments.Count;

                timeLastCall = 0;

                for (var i = 0; i < postsWithComments.Count; i++)
                {
                    isRunning = true;
                    totalCount = 0;
                    currentOffset = 0;

                    bw.ReportProgress(step, "Getting " + (i + 1) + " post comments out of " + postsWithComments.Count);

                    while (isRunning)
                    {
                        if (bw.CancellationPending)
                            break;

                        if (currentOffset > totalCount)
                        {
                            // done
                            break;
                        }

                        sb.Length = 0;
                        sb.Append("owner_id=").Append(groupId.ToString()).Append("&"); // group id
                        sb.Append("post_id=").Append(postsWithComments[i]).Append("&"); // post id
                        sb.Append("need_likes=").Append(1).Append("&"); // request likes info
                        sb.Append("offset=").Append(currentOffset).Append("&");
                        sb.Append("count=").Append(POSTS_PER_REQUEST).Append("&");
                        context.Parameters = sb.ToString();
                        context.Cookie = postsWithComments[i].ToString(); // pass post id as a cookie
                        Debug.WriteLine("Request parameters: " + context.Parameters);

                        // play nice, sleep for 1/3 sec to stay within 3 requests/second limits
                        timeLastCall = Utils.sleepTime(timeLastCall);
                        // call VK REST API
                        vkRestApi.CallVkFunction(VkFunction.WallGetComments, context);

                        // wait for the user data
                        ReadyEvent.WaitOne();

                        currentOffset += POSTS_PER_REQUEST;
                    }
                }

                groupCommentsWriter.Close();
            }

            // process the likers - they will be added to the posters
            if (likes.Count > 0)
            {
                // request likers ids
                bw.ReportProgress(-1, "Getting likers");
                step = 10000 / likes.Count;

                timeLastCall = 0;

                for (int i = 0; i < this.likes.Count; i++)
                {
                    isRunning = true;
                    //this.totalCount = 0;
                    //this.currentOffset = 0;

                    bw.ReportProgress(step, "Getting " + (i + 1) + " likes out of " + this.likes.Count);

                    if (bw.CancellationPending)
                        break;

                    sb.Length = 0;
                    sb.Append("type=").Append(likes[i].type).Append("&"); // group id
                    sb.Append("owner_id=").Append(likes[i].owner_id).Append("&"); // group id
                    sb.Append("item_id=").Append(likes[i].item_id).Append("&"); // post id
                    sb.Append("count=").Append(LIKES_PER_REQUEST).Append("&");
                    context.Parameters = sb.ToString();
                    Debug.WriteLine("Request parameters: " + context.Parameters);

                    // play nice, sleep for 1/3 sec to stay within 3 requests/second limits
                    timeLastCall = Utils.sleepTime(timeLastCall);
                    // call VK REST API
                    vkRestApi.CallVkFunction(VkFunction.LikesGetList, context);

                    // wait for the user data
                    ReadyEvent.WaitOne();
                }
            }

            // now collect visitors (not members, who left a post or a comment or a like) 
            foreach (var p in posterIds)
            {
                if (!memberIds.Contains(p))
                {
                    // this is a visitor poster
                    visitorIds.Add(p);
                }
            }

            if (visitorIds.Count > 0)
            {
                // group visitors profiles
                e = new Profile();
                fileName = Utils.GenerateFileName(this.WorkingFolderTextBox.Text, groupId, e, "visitor");
                groupVisitorsWriter = File.CreateText(fileName);
                Utils.PrintFileHeader(groupVisitorsWriter, e);

                // request visitors info
                bw.ReportProgress(-1, "Getting visitors");
                step = 10000 / visitorIds.Count;

                timeLastCall = 0;

                for (var i = 0; i < visitorIds.Count; i += 100)
                {
                    isRunning = true;
                    //this.totalCount = 0;
                    //this.currentOffset = 0;

                    bw.ReportProgress(step, "Getting " + (i + 1) + " visitors out of " + visitorIds.Count);

                    if (bw.CancellationPending)
                        break;

                    sb.Length = 0;

                    sb.Append("user_ids=");

                    for (var j = i; j < visitorIds.Count && j < i + 100; ++j)
                    {
                        sb.Append(visitorIds[j]).Append(","); // users
                    }

                    sb.Append("&").Append("fields=").Append(PROFILE_FIELDS);

                    context.Parameters = sb.ToString();
                    Debug.WriteLine("Request parameters: " + context.Parameters);

                    // play nice, sleep for 1/3 sec to stay within 3 requests/second limits
                    timeLastCall = Utils.sleepTime(timeLastCall);
                    // call VK REST API
                    vkRestApi.CallVkFunction(VkFunction.UsersGet, context);

                    // wait for the user data
                    ReadyEvent.WaitOne();

                }

                groupVisitorsWriter.Close();
            }

            //args.Result = TimeConsumingOperation(bw, arg);

            // If the operation was canceled by the user,  
            // set the DoWorkEventArgs.Cancel property to true. 
            if (bw.CancellationPending)
            {
                args.Cancel = true;
            }
        }
Пример #2
0
        // Members work async handlers
        private void MembersWork(Object sender, DoWorkEventArgs args)
        {
            // Do not access the form's BackgroundWorker reference directly. 
            // Instead, use the reference provided by the sender parameter.
            var bw = sender as BackgroundWorker;

            isMembersRunning = true;

            // Extract the argument. 
            var groupId = (decimal)args.Argument;

            // process group members
            IEntity e = new Profile();
            var fileName = Utils.GenerateFileName(this.WorkingFolderTextBox.Text, groupId, e, "members");
            groupMembersWriter = File.CreateText(fileName);
            Utils.PrintFileHeader(groupMembersWriter, e);

            var context = new VkRestApi.VkRestContext(this.userId, this.authToken);
            var sb = new StringBuilder();

            memberIds.Clear(); // clear member ids

            totalCount = 0;
            currentOffset = 0;
            step = 1;

            long timeLastCall = 0;

            // request group members
            while (isMembersRunning)
            {
                if (bw.CancellationPending)
                    break;

                if (currentOffset > totalCount)
                {
                    // done
                    break;
                }

                bw.ReportProgress(step, "Getting " + currentOffset + " members out of " + totalCount);

                sb.Length = 0;
                decimal gid = Math.Abs(groupId); // in this request, group id must be positive
                sb.Append("group_id=").Append(gid.ToString()).Append("&");
                sb.Append("sort=").Append("id_asc").Append("&");
                sb.Append("offset=").Append(currentOffset).Append("&");
                sb.Append("count=").Append(MEMBERS_PER_REQUEST).Append("&");
                sb.Append("fields=").Append(PROFILE_FIELDS).Append("&");
                context.Parameters = sb.ToString();
                Debug.WriteLine("Request parameters: " + context.Parameters);

                context.Cookie = currentOffset.ToString();

                // play nice, sleep for 1/3 sec to stay within 3 requests/second limits
                timeLastCall = Utils.sleepTime(timeLastCall);
                // call VK REST API
                vkRestApi.CallVkFunction(VkFunction.GroupsGetMembers, context);

                // wait for the members data
                ReadyEvent.WaitOne();

                currentOffset += MEMBERS_PER_REQUEST;
            }

            groupMembersWriter.Close();

            //args.Result = TimeConsumingOperation(bw, arg);

            // If the operation was canceled by the user,  
            // set the DoWorkEventArgs.Cancel property to true. 
            if (bw.CancellationPending)
            {
                args.Cancel = true;
            }
        }
Пример #3
0
        // Ego Net work async handlers
        private void EgoNetWork(Object sender, DoWorkEventArgs args)
        {
            // Do not access the form's BackgroundWorker reference directly. 
            // Instead, use the reference provided by the sender parameter.
            var bw = sender as BackgroundWorker;

            isEgoNetWorkRunning = true;

            // Extract the argument. 
            var groupId = (decimal)args.Argument;

            // gather the list of all users friends to build the group network
            totalCount = 0;
            currentOffset = 0;
            step = 1;

            // request group comments
            bw.ReportProgress(-1, "Getting members Ego network");
            step = 10000 / memberIds.Count;

            long timeLastCall = 0;
            long l = 0;
            foreach (long mId in memberIds)
            {
                if (bw.CancellationPending || !isEgoNetWorkRunning)
                    break;

                bw.ReportProgress(step, "Getting ego nets: " + (++l) + " out of " + memberIds.Count);

                // reset friends
                friendIds.Clear();
                
                // reset ego net analyzer
                egoNetAnalyzer.Clear();

                // for each member get his ego net
                var context = new VkRestApi.VkRestContext(mId.ToString(), authToken);
                var sb = new StringBuilder();

                sb.Length = 0;
                sb.Append("fields=").Append(PROFILE_FIELDS);
                context.Parameters = sb.ToString();
                vkRestApi.CallVkFunction(VkFunction.LoadFriends, context);

                // wait for the friends data
                ReadyEvent.WaitOne();

                foreach (long targetId in friendIds)
                {
                    sb.Length = 0;
                    sb.Append("target_uid=").Append(targetId); // Append target friend ids
                    context.Parameters = sb.ToString();
                    context.Cookie = targetId.ToString(); // pass target id in the cookie context field

                    // play nice, sleep for 1/3 sec to stay within 3 requests/second limits
                    timeLastCall = Utils.sleepTime(timeLastCall);
                    vkRestApi.CallVkFunction(VkFunction.FriendsGetMutual, context);

                    // wait for the friends data
                    ReadyEvent.WaitOne();
                }

                // save ego net document
                XmlDocument egoNet = egoNetAnalyzer.GenerateEgoNetwork();
                egoNet.Save(GenerateEgoNetworkFileName(groupId, mId));

            }

            // args.Result = this.groupNetworkAnalyzer.GeneratePostersNetwork();

            // If the operation was canceled by the user,  
            // set the DoWorkEventArgs.Cancel property to true. 
            if (bw.CancellationPending)
            {
                args.Cancel = true;
            }
        }
Пример #4
0
        private void FindGroupsButton_Click(object sender, EventArgs e)
        {
            var groupsDialog = new FindGroupsDialog();

            if (groupsDialog.ShowDialog() == DialogResult.OK)
            {
                //SearchParameters searchParameters = groupsDialog.searchParameters;
                //this.backgroundFinderWorker.RunWorkerAsync(searchParameters);
                var gid = groupsDialog.groupId;
                isGroup = groupsDialog.isGroup;

                if (isGroup)
                {
                    // lookup a group by id
                    var context = new VkRestApi.VkRestContext(this.userId, this.authToken);
                    var sb = new StringBuilder();
                    sb.Append("group_id=").Append(gid).Append("&");
                    sb.Append("fields=").Append(GROUP_FIELDS).Append("&");
                    context.Parameters = sb.ToString();
                    context.Cookie = groupsDialog.groupId.ToString();
                    Debug.WriteLine("Download parameters: " + context.Parameters);

                    // call VK REST API
                    vkRestApi.CallVkFunction(VkFunction.GroupsGetById, context);

                    // wait for the user data
                    ReadyEvent.WaitOne();

                } 
                else
                {
                    var context = new VkRestApi.VkRestContext(gid.ToString(), this.authToken);
                    vkRestApi.CallVkFunction(VkFunction.GetProfiles, context);
                    ReadyEvent.WaitOne();
                }
            }
            else
            {
                Debug.WriteLine("Search canceled");
            }
        }
Пример #5
0
        // Async workers
        private void GroupsWork(Object sender, DoWorkEventArgs args)
        {
            // Do not access the form's BackgroundWorker reference directly. 
            // Instead, use the reference provided by the sender parameter.
            var bw = sender as BackgroundWorker;

            // Extract the argument.
            var param = args.Argument as GroupPostsParam;

            if (bw == null ||
                param == null)
            {
                throw new ArgumentException("Illegal arguments for Group Work");
            }

            if (param.From <= param.To)
            {
                postsFromDate = param.From;
                postsToDate = param.To;
            }
            else
            {
                postsFromDate = param.To;
                postsToDate = param.From;
            }

            // working directory
            var workingDir = WorkingFolderTextBox.Text;

            // clear all
            postsWithComments.Clear(); // reset reference list
            likes.Clear(); // reset likes
            posters.Clear(); // reset posters
            posterIds.Clear(); // clear poster ids
            visitorIds.Clear(); // clear visitor ids
            postInfo.Clear(); // clear post infos
            boardPostInfo.Clear(); // reset boards post infos
            topics.Clear(); // clear topics infos
            u2uMatrix.Clear(); // clear u2u matrix

            var context = new VkRestApi.VkRestContext(this.userId, this.authToken);
            var sb = new StringBuilder();
            long timeLastCall = 0;

            networkEntities = param.GroupWall ? "wall" : "";
            networkEntities += param.GroupTopics ? "-topics" : "";

            // gather group statistics
            if (param.JustStats)
            {
                ResetCountersAndGetReady();

                bw.ReportProgress(1, "Getting group stats");

                sb.Length = 0;
                // TODO check if it takes negative number
                sb.Append("group_id=").Append(groupId).Append("&");
                sb.Append("date_from=").Append(this.postsFromDate.ToString("yyyy-MM-dd")).Append("&");
                sb.Append("date_to=").Append(this.postsToDate.ToString("yyyy-MM-dd"));
                context.Parameters = sb.ToString();
                Debug.WriteLine("Download parameters: " + context.Parameters);

                // call VK REST API
                vkRestApi.CallVkFunction(VkFunction.StatsGet, context);
                return;
            }

            IEntity e;
            string fileName;

            if (param.GroupWall)
            {
                // group posts
                e = new Post();
                fileName = Utils.GenerateFileName(workingDir, groupId, e);
                groupPostsWriter = File.CreateText(fileName);
                Utils.PrintFileHeader(groupPostsWriter, e);

                e = new PostCopyHistory(0, null);
                fileName = Utils.GenerateFileName(workingDir, groupId, e);
                groupPostsCopyHistoryWriter = File.CreateText(fileName);
                Utils.PrintFileHeader(groupPostsCopyHistoryWriter, e);

                ResetCountersAndGetReady();

                // request group posts
                bw.ReportProgress(-1, "Getting posts");
                while (ShellContinue(bw))
                {
                    sb.Length = 0;
                    sb.Append("owner_id=").Append(groupId.ToString()).Append("&");
                    sb.Append("offset=").Append(currentOffset).Append("&");
                    sb.Append("count=").Append(POSTS_PER_REQUEST).Append("&");
                    context.Parameters = sb.ToString();
                    Debug.WriteLine("Download parameters: " + context.Parameters);

                    context.Cookie = currentOffset.ToString();

                    // play nice, sleep for 1/3 sec to stay within 3 requests/second limits
                    timeLastCall = Utils.sleepTime(timeLastCall);
                    // call VK REST API
                    vkRestApi.CallVkFunction(VkFunction.WallGet, context);

                    // wait for the user data
                    ReadyEvent.WaitOne();
                    bw.ReportProgress(step, "Getting " + currentOffset + " posts out of " + totalCount);
                }

                groupPostsWriter.Close();
                groupPostsCopyHistoryWriter.Close();

                if (postsWithComments.Count > 0 &&
                    !bw.CancellationPending)
                {
                    // group comments
                    e = new Comment();
                    fileName = Utils.GenerateFileName(workingDir, groupId, e);
                    groupCommentsWriter = File.CreateText(fileName);
                    Utils.PrintFileHeader(groupCommentsWriter, e);

                    // request group comments
                    bw.ReportProgress(-1, "Getting comments");

                    timeLastCall = 0;

                    for (int i = 0; i < postsWithComments.Count && !bw.CancellationPending; i++)
                    {
                        ResetCountersAndGetReady();
                        step = CalcStep(postsWithComments.Count);

                        bw.ReportProgress(step,
                            "Getting " + (i + 1) + " comments out of " + postsWithComments.Count);

                        while (ShellContinue(bw))
                        {
                            sb.Length = 0;
                            sb.Append("owner_id=").Append(groupId).Append("&"); // group id
                            sb.Append("post_id=").Append(postsWithComments[i]).Append("&"); // post id
                            sb.Append("need_likes=").Append(1).Append("&"); // request likes info
                            sb.Append("offset=").Append(currentOffset).Append("&");
                            sb.Append("count=").Append(POSTS_PER_REQUEST).Append("&");
                            context.Parameters = sb.ToString();
                            context.Cookie = postsWithComments[i].ToString(); // pass post id as a cookie
                            Debug.WriteLine("Request parameters: " + context.Parameters);

                            // play nice, sleep for 1/3 sec to stay within 3 requests/second limits
                            timeLastCall = Utils.sleepTime(timeLastCall);
                            // call VK REST API
                            vkRestApi.CallVkFunction(VkFunction.WallGetComments, context);

                            // wait for the user data
                            ReadyEvent.WaitOne();
                        }
                    }

                    groupCommentsWriter.Close();
                }
            }

            if (param.GroupTopics)
            {
                // process group board topics and comments
                e = new BoardTopic();
                fileName = Utils.GenerateFileName(workingDir, groupId, e);
                groupBoardTopicsWriter = File.CreateText(fileName);
                Utils.PrintFileHeader(groupBoardTopicsWriter, e);

                e = new Comment();
                fileName = Utils.GenerateFileName(workingDir, groupId, e, "board");
                groupBoardCommentsWriter = File.CreateText(fileName);
                Utils.PrintFileHeader(groupBoardCommentsWriter, e);

                ResetCountersAndGetReady();

                bw.ReportProgress(-1, "Getting board topics");

                // get group board topics
                while (ShellContinue(bw))
                {
                    sb.Length = 0;
                    sb.Append("group_id=").Append(Math.Abs(groupId)).Append("&");
                    sb.Append("offset=").Append(currentOffset).Append("&");
                    sb.Append("count=").Append(POSTS_PER_REQUEST).Append("&");
                    context.Parameters = sb.ToString();
                    Debug.WriteLine("Download parameters: " + context.Parameters);

                    context.Cookie = currentOffset.ToString();

                    // play nice, sleep for 1/3 sec to stay within 3 requests/second limits
                    timeLastCall = Utils.sleepTime(timeLastCall);
                    // call VK REST API
                    vkRestApi.CallVkFunction(VkFunction.BoardGetTopics, context);

                    // wait for the user data
                    ReadyEvent.WaitOne();
                    bw.ReportProgress(step, "Getting board topics " + currentOffset + " out of " + totalCount);
                }

                bw.ReportProgress(-1, "Getting board comments");

                // collect comments from all board topics
                for (var i = 0; i < topics.Count; i++)
                {
                    if (bw.CancellationPending)
                        break; // canceled

                    if (topics[i].is_closed || topics[i].comments == 0)
                        continue; // empty or closed topic - ignore

                    ResetCountersAndGetReady();
                    step = CalcStep(topics.Count);
                    bw.ReportProgress(step, "Getting comments for board " + i + " / " + topics.Count);

                    while (ShellContinue(bw))
                    {
                        sb.Length = 0;
                        sb.Append("group_id=").Append(Math.Abs(groupId)).Append("&"); // group id
                        sb.Append("topic_id=").Append(topics[i].id).Append("&"); // post id
                        sb.Append("need_likes=").Append(1).Append("&"); // need likes count
                        sb.Append("offset=").Append(currentOffset).Append("&");
                        sb.Append("count=").Append(POSTS_PER_REQUEST).Append("&");
                        context.Parameters = sb.ToString();
                        context.Cookie = topics[i].id.ToString(); // pass topic id as a cookie
                        Debug.WriteLine("Request parameters: " + context.Parameters);

                        // play nice, sleep for 1/3 sec to stay within 3 requests/second limits
                        timeLastCall = Utils.sleepTime(timeLastCall);
                        // call VK REST API
                        vkRestApi.CallVkFunction(VkFunction.BoardGetComments, context);

                        // wait for the user data
                        ReadyEvent.WaitOne();
                        bw.ReportProgress(0,
                            "Getting comments " + currentOffset + " / " + totalCount + " for board " + i + " / " +
                            topics.Count);
                    }
                }

                groupBoardTopicsWriter.Close();
                groupBoardCommentsWriter.Close();
            }

            // process the likes from wall and board topics
            if (likes.Count > 0 &&
                !bw.CancellationPending)
            {
                // request likers ids
                ResetCountersAndGetReady();
                bw.ReportProgress(-1, "Getting likers");

                timeLastCall = 0;
                step = CalcStep(likes.Count);

                for (var i = 0; i < likes.Count; i++)
                {
                    isRunning = true;
                    bw.ReportProgress(step, "Getting likes " + (i + 1) + " / " + likes.Count);

                    if (bw.CancellationPending)
                        break;

                    sb.Length = 0;
                    sb.Append("type=").Append(likes[i].type).Append("&"); // type
                    sb.Append("owner_id=").Append(likes[i].owner_id).Append("&"); // group id
                    sb.Append("item_id=").Append(likes[i].item_id).Append("&"); // post id
                    sb.Append("count=").Append(LIKES_PER_REQUEST).Append("&");
                    context.Parameters = sb.ToString();
                    context.Cookie = likes[i].item_id.ToString(); // pass post/comment id as a cookie
                    Debug.WriteLine("Request parameters: " + context.Parameters);

                    // play nice, sleep for 1/3 sec to stay within 3 requests/second limits
                    timeLastCall = Utils.sleepTime(timeLastCall);
                    // call VK REST API
                    vkRestApi.CallVkFunction(VkFunction.LikesGetList, context);

                    // wait for the user data
                    ReadyEvent.WaitOne();
                }
            }

            // now collect info about posters (users or visitors who left post, comment or like) 
            visitorIds.AddRange(posterIds);

            if (visitorIds.Count > 0 &&
                !bw.CancellationPending)
            {
                // group visitors profiles
                e = new Profile();
                fileName = Utils.GenerateFileName(workingDir, groupId, e, networkEntities);
                groupVisitorsWriter = File.CreateText(fileName);
                Utils.PrintFileHeader(groupVisitorsWriter, e);

                // request visitors info
                bw.ReportProgress(-1, "Getting visitors");
                step = CalcStep(visitorIds.Count, 100);

                timeLastCall = 0;

                for (var i = 0; i < visitorIds.Count; i += 100)
                {
                    isRunning = true;

                    bw.ReportProgress(step, "Getting " + (i + 1) + " visitors out of " + visitorIds.Count);

                    if (bw.CancellationPending)
                        break;

                    sb.Length = 0;

                    sb.Append("user_ids=");

                    for (var j = i; j < visitorIds.Count && j < i + 100; ++j)
                    {
                        sb.Append(visitorIds[j]).Append(","); // users
                    }

                    sb.Append("&").Append("fields=").Append(PROFILE_FIELDS);

                    context.Parameters = sb.ToString();
                    Debug.WriteLine("Request parameters: " + context.Parameters);

                    // play nice, sleep for 1/3 sec to stay within 3 requests/second limits
                    timeLastCall = Utils.sleepTime(timeLastCall);
                    // call VK REST API
                    vkRestApi.CallVkFunction(VkFunction.UsersGet, context);

                    // wait for the user data
                    ReadyEvent.WaitOne();
                }

                groupVisitorsWriter.Close();
            }


            // update group posts/likes count (posted from group id)
            Poster groupPoster;
            if (posters.TryGetValue((long)this.groupId, out groupPoster))
            {
                var attr = dictionaryFromPoster(groupPoster);
                // update poster vertex attributes
                contentNetworkAnalyzer.UpdateVertexAttributes((long)groupId, attr);
            }


            // If the operation was canceled by the user,  
            // set the DoWorkEventArgs.Cancel property to true. 
            args.Cancel = bw.CancellationPending;

            // complete the job
            //args.Result = 
        }
Пример #6
0
        // Members Network work async handlers
        private void NetworkWork(Object sender, DoWorkEventArgs args)
        {
            // Do not access the form's BackgroundWorker reference directly. 
            // Instead, use the reference provided by the sender parameter.
            var bw = sender as BackgroundWorker;

            isNetworkRunning = true;

            // Extract the argument. 
            // var groupId = (decimal)args.Argument;

            // gather the list of all users friends to build the group network
            totalCount = 0;
            currentOffset = 0;
            step = 1;

            var context = new VkRestApi.VkRestContext(userId, authToken);
            var sb = new StringBuilder();

            // consolidate all group ids
            var members = this.memberIds;
            // add all posters visitors ids
            foreach (var mId in visitorIds)
            {
                members.Add(mId); // could be a member or a visitor
            }

            // request members friends
            bw.ReportProgress(-1, "Getting friends network");
            step = 10000 / members.Count;

            long timeLastCall = 0;
            long l = 0;
            foreach (var mId in members)
            {
                if (bw.CancellationPending || !isNetworkRunning)
                    break;

                bw.ReportProgress(step, "Getting friends: " + (++l) + " out of " + members.Count);

                sb.Length = 0;
                sb.Append("user_id=").Append(mId);
                context.Parameters = sb.ToString();
                context.Cookie = mId.ToString(); // pass member id as a cookie
                Debug.WriteLine("Request parameters: " + context.Parameters);

                // play nice, sleep for 1/3 sec to stay within 3 requests/second limits
                timeLastCall = Utils.sleepTime(timeLastCall);
                vkRestApi.CallVkFunction(VkFunction.FriendsGet, context);

                // wait for the friends data
                ReadyEvent.WaitOne();
            }

            args.Result = groupNetworkAnalyzer.GenerateGroupNetwork();

            // If the operation was canceled by the user,  
            // set the DoWorkEventArgs.Cancel property to true. 
            if (bw.CancellationPending)
            {
                args.Cancel = true;
            }
        }
Пример #7
0
        BackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            Debug.Assert(sender is BackgroundWorker);
            BackgroundWorker bw = sender as BackgroundWorker;

            Debug.Assert(e.Argument is NetworkAsyncArgs);
            NetworkAsyncArgs args = e.Argument as NetworkAsyncArgs;

            this.requestStatistics = new RequestStatistics();

            try
            {
                CheckCancellationPending();
                ReportProgress("Starting");

                // shell include ego node in the graph
                this.includeEgo = args.includeMe;

                // prepare rest contexts
                var context = new VkRestApi.VkRestContext(args.userId, args.accessToken);

                // get ego node
                vkRestApi.CallVkFunction(VkFunction.GetProfiles, context);

                // wait for the user data
                readyEvent.WaitOne();

                CheckCancellationPending();
                ReportProgress("Retrieving friends");

                // prepare fields context parameter
                StringBuilder sb = new StringBuilder("fields=");
                sb.Append(args.fields);
                context.Parameters = sb.ToString();

                // get friends node
                vkRestApi.CallVkFunction(VkFunction.LoadFriends, context);

                // wait for the friends data
                readyEvent.WaitOne();

                int total = this.friendIds.Count;
                int current = 0;

                foreach (string targetId in this.friendIds)
                {
                    CheckCancellationPending();
                    current++;
                    ReportProgress("Retrieving friends mutual " + current + " out of " + total);

                    // Append target friend ids
                    sb.Length = 0;
                    sb.Append("target_uid=");
                    sb.Append(targetId);

                    context.Parameters = sb.ToString();
                    context.Cookie = targetId; // pass target id in the context's cookie field
                    
                    // get mutual friends 
                    vkRestApi.CallVkFunction(VkFunction.FriendsGetMutual, context);

                    // wait for the mutual data
                    readyEvent.WaitOne();

                    // play it nice, sleep for 1/3 sec to stay within 3 requests/second limit
                    // TODO: account for time spent in processing
                    Thread.Sleep(333);
                }

                if (includeEgo)
                {
                    CreateIncludeMeEdges();
                }

                CheckCancellationPending();
                ReportProgress("Building network graph document");

                // create default attributes (values will be empty)
                AttributesDictionary<String> attributes = new AttributesDictionary<String>(VKAttributes);

                // build the file
                XmlDocument graph = GenerateNetworkDocument(vertices, edges, attributes);

                if (this.requestStatistics.UnexpectedExceptions > 0)
                {
                    // there was errors - pop up partial network dialog
                    throw new PartialNetworkException(graph, this.requestStatistics);
                }
                else
                {
                    // all good
                    e.Result = graph;
                }

            }
            catch (CancellationPendingException)
            {
                e.Cancel = true;
            }
        }
Пример #8
0
        public XmlDocument analyze(String userId, String authToken)
        {
            var context = new VkRestApi.VkRestContext(userId, authToken);

            this.requestStatistics = new RequestStatistics();

            vkRestApi.CallVkFunction(VkFunction.GetProfiles, context);

            // wait for the user data
            readyEvent.WaitOne();
            context.Parameters = "fields=uid,first_name,last_name,sex,bdate,photo_50,city,country,relation,nickname";
            vkRestApi.CallVkFunction(VkFunction.LoadFriends, context);

            // wait for the friends data
            readyEvent.WaitOne();
            foreach (string targetId in this.friendIds)
            {
                StringBuilder sb = new StringBuilder("target_uid=");
                // Append target friend ids
                sb.Append(targetId);

                context.Parameters = sb.ToString();
                context.Cookie = targetId; // pass target id in the cookie context field
                vkRestApi.CallVkFunction(VkFunction.FriendsGetMutual, context);

                // wait for the mutual data
                readyEvent.WaitOne();

                // play nice, sleep for 1/3 sec to stay within 3 requests/second limit
                // TODO: account for time spent in processing
                Thread.Sleep(333);
            }

            if (includeEgo)
            {
                CreateIncludeMeEdges();
            }

            // create default attributes (values will be empty)
            AttributesDictionary<String> attributes = new AttributesDictionary<String>(VKAttributes);

            return GenerateNetworkDocument(vertices, edges, attributes);
        }
Пример #9
0
        private void LoadUserInfoButton_Click(object sender, EventArgs e)
        {
            String userId = this.userIdTextBox.Text;
            if (userId == null ||
                userId.Length == 0)
            {
                userId = vkLoginDialog.userId;
                this.userIdTextBox.Text = userId;
            }

            var context = new VkRestApi.VkRestContext(userId, vkLoginDialog.authToken);
            vkRestApi.CallVkFunction(VkFunction.GetProfiles, context);
        }
Пример #10
0
        private void loadWork(Object sender, DoWorkEventArgs args)
        {
            // Do not access the form's BackgroundWorker reference directly. 
            // Instead, use the reference provided by the sender parameter.
            BackgroundWorker bw = sender as BackgroundWorker;

            // Extract the argument. 
            // SearchParameters searchParameters = args.Argument as SearchParameters;
            var context = new VkRestApi.VkRestContext(this.userId, this.authToken);


            // loop by birth year and month to maximize number of matches (1000 at a time)
            StringBuilder sb = new StringBuilder();
            this.run = true;
            this.totalCount = 0;
            this.currentOffset = 0;
            long timeLastCall = 0;

            // process regions
            while (this.run &&
                this.currentOffset <= this.totalCount)
            {
                sb.Length = 0;
                sb.Append("country_id=").Append(1).Append("&"); // Russia
                sb.Append("offset=").Append(currentOffset).Append("&");
                sb.Append("count=").Append(ITEMS_PER_REQUEST).Append("&");
                context.Parameters = sb.ToString();
                Debug.WriteLine("request params: " + context.Parameters);

                // play nice, sleep for 1/3 sec to stay within 3 requests/second limits
                timeLastCall = sleepTime(timeLastCall);
                vkRestApi.CallVkFunction(VkFunction.DatabaseGetRegions, context);

                // wait for the user data
                readyEvent.WaitOne();
                this.currentOffset += ITEMS_PER_REQUEST;
            }

            this.run = true;
            this.totalCount = 0;
            this.currentOffset = 0;
            timeLastCall = 0;
            // process cities
            while (this.run &&
                this.currentOffset <= this.totalCount)
            {
                sb.Length = 0;
                sb.Append("country_id=").Append(1).Append("&"); // Russia
                sb.Append("region_id=").Append(1045244).Append("&"); // Leningradskaya obl.
                sb.Append("need_all=").Append(1).Append("&"); // all
                sb.Append("offset=").Append(currentOffset).Append("&");
                sb.Append("count=").Append(ITEMS_PER_REQUEST).Append("&");
                context.Parameters = sb.ToString();
                Debug.WriteLine("request params: " + context.Parameters);

                // play nice, sleep for 1/3 sec to stay within 3 requests/second limits
                timeLastCall = sleepTime(timeLastCall);
                vkRestApi.CallVkFunction(VkFunction.DatabaseGetCities, context);

                // wait for the user data
                readyEvent.WaitOne();
                this.currentOffset += ITEMS_PER_REQUEST;
            }

            //args.Result = TimeConsumingOperation(bw, arg);

        }
Пример #11
0
        // Async Workers

        private void findWork(Object sender, DoWorkEventArgs args)
        {
            // Do not access the form's BackgroundWorker reference directly. 
            // Instead, use the reference provided by the sender parameter.
            BackgroundWorker bw = sender as BackgroundWorker;

            // Extract the argument. 
            SearchParameters searchParameters = args.Argument as SearchParameters;

            string parameters = parseSearchParameters(searchParameters);

            Int32 startYear = (Int32)searchParameters.yearStart;
            Int32 stopYear = (Int32)searchParameters.yearEnd;
            Int32 startMonth = (Int32)searchParameters.monthStart;
            Int32 stopMonth = (Int32)searchParameters.monthEnd;

            if (stopYear < startYear)
            {
                stopYear = startYear;
            }

            if (startYear > 1900)
            {
                if (startMonth == 0)
                {
                    startMonth = 1;
                }

                if (stopMonth == 0)
                {
                    stopMonth = 12;
                }
            } 
            else
            {
                startYear = stopYear = 1900;
                startMonth = stopMonth = 1;
                searchParameters.useSlowSearch = false;
            }

            DateTime saveStartDate = new DateTime(startYear, startMonth, 1);
            DateTime saveStopDate = new DateTime(stopYear, stopMonth, 1);

            if(searchParameters.useSlowSearch)
            {
                // set stop date to the last day of the stop month
                saveStopDate = saveStopDate.AddMonths(1).AddDays(-1); 
            }

            // figure out step
            int step = 10000;
                
            if( startYear > 1900)
            {
                if(searchParameters.useSlowSearch)
                {
                    // calculate number of days
                    step = (int)(10000 / ((saveStopDate - saveStartDate).TotalDays) + 0.5);
                }
                else
                {
                    // calculate number of months 
                    step = (int)(10000 / ((saveStopDate.Year - saveStartDate.Year) * 12  + saveStopDate.Month - saveStartDate.Month + 1));
                }
            }

            step /= searchParameters.cities.Count();

            // create stream writer
            String fileName = generateFileName(searchParameters);
            writer = File.CreateText(fileName);
            printHeader(writer);

            var context = new VkRestApi.VkRestContext(this.userId, this.authToken);

            // loop by birth year and month to maximize number of matches (1000 at a time)
            StringBuilder sb = new StringBuilder();
            foreach (var city in searchParameters.cities)
            {
                long timeLastCall = 0;
                DateTime startDate = saveStartDate;
                DateTime stopDate = saveStopDate;

                while (startDate <= stopDate)
                {
                    if (bw.CancellationPending)
                        break;

                    bw.ReportProgress(step, "Searching in " + city.Title);

                    this.run = true;
                    this.totalCount = 0;
                    this.currentOffset = 0;

                    while (this.run &&
                        this.currentOffset <= this.totalCount)
                    {
                        if (bw.CancellationPending)
                            break;

                        sb.Length = 0;
                        sb.Append("offset=").Append(currentOffset).Append("&");
                        sb.Append("count=").Append(ITEMS_PER_REQUEST).Append("&");
                        sb.Append(parameters);
                        if (city.Id > 0)
                        {
                            sb.Append("city=").Append(city.Id).Append("&");
                        }

                        // append bdate 
                        if (startYear > 1900)
                        {
                            sb.Append("birth_year=").Append(startDate.Year).Append("&");
                            sb.Append("birth_month=").Append(startDate.Month).Append("&");
                            if (searchParameters.useSlowSearch)
                            {
                                sb.Append("birth_day=").Append(startDate.Day).Append("&");
                            }
                        }
                        // append required fields
                        sb.Append(REQUIRED_FIELDS);

                        context.Parameters = sb.ToString();
                        Debug.WriteLine("Search parameters: " + context.Parameters);

                        context.Cookie = this.currentOffset.ToString();

                        // play nice, sleep for 1/3 sec to stay within 3 requests/second limits
                        timeLastCall = sleepTime(timeLastCall);
                        vkRestApi.CallVkFunction(VkFunction.UsersSearch, context);

                        // wait for the user data
                        readyEvent.WaitOne();

                        this.currentOffset += ITEMS_PER_REQUEST;
                    }

                    // increment date
                    startDate = searchParameters.useSlowSearch ? startDate.AddDays(1) : startDate.AddMonths(1);
                }
            }


            writer.Close();

            //args.Result = TimeConsumingOperation(bw, arg);

            // If the operation was canceled by the user,  
            // set the DoWorkEventArgs.Cancel property to true. 
            if (bw.CancellationPending)
            {
                args.Cancel = true;
            }

        }