{"action":"created","comment":{"url":"https://api.github.com/repos/cscenter/interestscsc/pulls/comments/41699352","id":41699352,"diff_hunk":"@@ -0,0 +1,236 @@\n+package crawler;\n+\n+\n+import com.mashape.unirest.http.HttpResponse;\n+import com.mashape.unirest.http.Unirest;\n+import com.mashape.unirest.http.exceptions.UnirestException;\n+import org.jsoup.Jsoup;\n+import org.jsoup.nodes.Document;\n+import org.jsoup.nodes.Element;\n+import org.jsoup.select.Elements;\n+\n+import java.io.BufferedWriter;\n+import java.io.File;\n+import java.io.FileWriter;\n+import java.io.IOException;\n+import java.nio.file.Path;\n+import java.util.*;\n+import java.util.regex.Matcher;\n+import java.util.regex.Pattern;\n+\n+public class Crawler {\n+\n+    private static final Integer MAX_TAG_USES_POW = 6; // max uses count 1e6\n+\n+    // set of all users\n+    private static Set<String> allUsers;\n+\n+    // queue of users who should be considered\n+    private static LinkedList<String> usersQueue;\n+\n+    // dictionary where key=tags' name; value=count uses of this tags\n+    private static Map<String, Integer> allTags;\n+\n+    public Crawler() {\n+        allUsers = new HashSet<String>();\n+        usersQueue = new LinkedList<String>();\n+        allTags = new HashMap<String, Integer>();\n+    }\n+\n+    // get all user's friends\n+    private static void getUserFriends(String user) throws UnirestException, InterruptedException {\n+\n+        HttpResponse<String> userFriendsResponse = Unirest.get(\"http://www.livejournal.com/misc/fdata.bml?user={user_name}\")\n+                .routeParam(\"user_name\", user)\n+                .header(\"Accept-Language\", \"ru-RU,ru;q=0.8,en-US;q=0.5,en;q=0.3\")\n+                .asString();\n+        Thread.sleep(200);  // delay\n+\n+\n+        String friends = userFriendsResponse.getBody()\n+                .replaceAll(\"^[^\\n]*\\n\", \"\")            // delete first line\n+                .replaceAll(\"[<>] \", \"\")                // delete separators < >\n+                .replaceAll(\"_\", \"-\")                   // change _ to - for Unirest response\n+                .replaceAll(\"\\n.*-\\n\", \"\\n\")            // delete users with name like \"user_\", \"user-\"\n+                .replaceAll(\"\\n-.*\\n\", \"\\n\");           // delete users with name like \"_user\", \"-user\"\n+\n+        String[] friendsArray = friends.split(\"\\n\");\n+\n+        for (String friend: friendsArray) {\n+            if (allUsers.add(friend)) {\n+                usersQueue.add(friend);\n+            }\n+        }\n+    }\n+\n+    // get all user's tags\n+    private static List<Tag> getUserTags(String user) throws UnirestException, InterruptedException {\n+\n+        HttpResponse<String> userTagsResponse = Unirest.get(\"http://{user_name}.livejournal.com/tag\")\n+                .routeParam(\"user_name\", user)\n+                .header(\"Accept-Language\", \"ru-RU,ru;q=0.8,en-US;q=0.5,en;q=0.3\")\n+                .asString();\n+        Thread.sleep(1000);  // delay\n+\n+        Document doc = Jsoup.parse(userTagsResponse.getBody());\n+\n+        // find all tags on page\n+        Elements httpTags = doc.select(\"a[href^=http://\" + user + \".livejournal.com/tag\");\n+\n+        String regex = \"\\\\d+\";    // the number\n+        Pattern pattern = Pattern.compile(regex);\n+        List<Tag> tagList = new ArrayList<Tag>();\n+\n+        for(Element httpTag: httpTags) {\n+            String tagText = httpTag.text();\n+\n+            // delete unused attributes\n+            httpTag = httpTag.attr(\"href\", \"\")\n+                    .attr(\"style\", \"\")\n+                    .attr(\"target\", \"\")\n+                    .attr(\"rel\", \"\")\n+                    .attr(\"class\", \"\")\n+                    .text(\"\");\n+\n+            String strUses = \"\";\n+\n+            // find count of uses into reference\n+            Matcher matcher = pattern.matcher(httpTag.outerHtml());\n+            if (matcher.find()) {\n+                strUses = matcher.group();\n+            }\n+            else {\n+                // find count of uses into parent\n+                matcher = pattern.matcher(httpTag.parent().outerHtml());\n+                if (matcher.find()) {\n+                    strUses = matcher.group();\n+                }\n+            }\n+\n+            // if don't find uses or find irregular result\n+            if (!strUses.isEmpty() && strUses.length() < MAX_TAG_USES_POW) {\n+                Integer tagUses = Integer.parseInt(strUses);\n+                tagList.add(new Tag(tagText, tagUses));\n+            }\n+        }\n+\n+        return tagList;\n+    }\n+\n+    // @TODO add logger to handle exceptions\n+    public void crawl(String startUser) {\n+\n+        allUsers.add(startUser);\n+        usersQueue.add(startUser);\n+\n+        try {\n+\n+            getUserFriends(startUser);\n+\n+        } catch (UnirestException e) {\n+            e.printStackTrace();\n+        } catch (InterruptedException e) {\n+            e.printStackTrace();\n+        }\n+\n+        // get users without tags\n+        Integer countUsersNoTags = 0;\n+        List<String> usersNoTags = new ArrayList<String>();\n+\n+        // for all users\n+        while(!usersQueue.isEmpty()) {\n+\n+            String user = usersQueue.poll();\n+            List<Tag> userTags = null;\n+            try {\n+\n+                getUserFriends(user);\n+                userTags = getUserTags(user);\n+\n+            } catch (UnirestException e) {\n+                System.out.println(\"I don't no why do you see this exception.\");","path":"Crawler/src/main/java/crawler/Crawler.java","position":151,"original_position":151,"commit_id":"311ffb3a1599e57437d707ccba678a3afd5904ff","original_commit_id":"311ffb3a1599e57437d707ccba678a3afd5904ff","user":{"login":"KorixD","id":1475343,"avatar_url":"https://avatars.githubusercontent.com/u/1475343?v=3","gravatar_id":"","url":"https://api.github.com/users/KorixD","html_url":"https://github.com/KorixD","followers_url":"https://api.github.com/users/KorixD/followers","following_url":"https://api.github.com/users/KorixD/following{/other_user}","gists_url":"https://api.github.com/users/KorixD/gists{/gist_id}","starred_url":"https://api.github.com/users/KorixD/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/KorixD/subscriptions","organizations_url":"https://api.github.com/users/KorixD/orgs","repos_url":"https://api.github.com/users/KorixD/repos","events_url":"https://api.github.com/users/KorixD/events{/privacy}","received_events_url":"https://api.github.com/users/KorixD/received_events","type":"User","site_admin":false},"body":"know? и можно без do.","created_at":"2015-10-10T17:28:24Z","updated_at":"2015-10-10T17:28:24Z","html_url":"https://github.com/cscenter/interestscsc/pull/1#discussion_r41699352","pull_request_url":"https://api.github.com/repos/cscenter/interestscsc/pulls/1","_links":{"self":{"href":"https://api.github.com/repos/cscenter/interestscsc/pulls/comments/41699352"},"html":{"href":"https://github.com/cscenter/interestscsc/pull/1#discussion_r41699352"},"pull_request":{"href":"https://api.github.com/repos/cscenter/interestscsc/pulls/1"}}},"pull_request":{"url":"https://api.github.com/repos/cscenter/interestscsc/pulls/1","id":47328945,"html_url":"https://github.com/cscenter/interestscsc/pull/1","diff_url":"https://github.com/cscenter/interestscsc/pull/1.diff","patch_url":"https://github.com/cscenter/interestscsc/pull/1.patch","issue_url":"https://api.github.com/repos/cscenter/interestscsc/issues/1","number":1,"state":"open","locked":false,"title":"Crawler","user":{"login":"sssmaximusss","id":11423910,"avatar_url":"https://avatars.githubusercontent.com/u/11423910?v=3","gravatar_id":"","url":"https://api.github.com/users/sssmaximusss","html_url":"https://github.com/sssmaximusss","followers_url":"https://api.github.com/users/sssmaximusss/followers","following_url":"https://api.github.com/users/sssmaximusss/following{/other_user}","gists_url":"https://api.github.com/users/sssmaximusss/gists{/gist_id}","starred_url":"https://api.github.com/users/sssmaximusss/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sssmaximusss/subscriptions","organizations_url":"https://api.github.com/users/sssmaximusss/orgs","repos_url":"https://api.github.com/users/sssmaximusss/repos","events_url":"https://api.github.com/users/sssmaximusss/events{/privacy}","received_events_url":"https://api.github.com/users/sssmaximusss/received_events","type":"User","site_admin":false},"body":"I create the first version of Crawler.\r\nIt can collect tags and count of its uses.\r\nBut I can't check on bigdata(all users), because unirest handles the exception: UnknownHostException after 50-200 response.\r\nP.S. а тут можно писать по-русски? или так не принято?) ","created_at":"2015-10-09T23:03:38Z","updated_at":"2015-10-10T17:28:24Z","closed_at":null,"merged_at":null,"merge_commit_sha":"b548a2ef0d8fd891c904e110ad4a81336dc0e76b","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/cscenter/interestscsc/pulls/1/commits","review_comments_url":"https://api.github.com/repos/cscenter/interestscsc/pulls/1/comments","review_comment_url":"https://api.github.com/repos/cscenter/interestscsc/pulls/comments{/number}","comments_url":"https://api.github.com/repos/cscenter/interestscsc/issues/1/comments","statuses_url":"https://api.github.com/repos/cscenter/interestscsc/statuses/311ffb3a1599e57437d707ccba678a3afd5904ff","head":{"label":"cscenter:crawler","ref":"crawler","sha":"311ffb3a1599e57437d707ccba678a3afd5904ff","user":{"login":"cscenter","id":5622041,"avatar_url":"https://avatars.githubusercontent.com/u/5622041?v=3","gravatar_id":"","url":"https://api.github.com/users/cscenter","html_url":"https://github.com/cscenter","followers_url":"https://api.github.com/users/cscenter/followers","following_url":"https://api.github.com/users/cscenter/following{/other_user}","gists_url":"https://api.github.com/users/cscenter/gists{/gist_id}","starred_url":"https://api.github.com/users/cscenter/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cscenter/subscriptions","organizations_url":"https://api.github.com/users/cscenter/orgs","repos_url":"https://api.github.com/users/cscenter/repos","events_url":"https://api.github.com/users/cscenter/events{/privacy}","received_events_url":"https://api.github.com/users/cscenter/received_events","type":"Organization","site_admin":false},"repo":{"id":42926815,"name":"interestscsc","full_name":"cscenter/interestscsc","owner":{"login":"cscenter","id":5622041,"avatar_url":"https://avatars.githubusercontent.com/u/5622041?v=3","gravatar_id":"","url":"https://api.github.com/users/cscenter","html_url":"https://github.com/cscenter","followers_url":"https://api.github.com/users/cscenter/followers","following_url":"https://api.github.com/users/cscenter/following{/other_user}","gists_url":"https://api.github.com/users/cscenter/gists{/gist_id}","starred_url":"https://api.github.com/users/cscenter/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cscenter/subscriptions","organizations_url":"https://api.github.com/users/cscenter/orgs","repos_url":"https://api.github.com/users/cscenter/repos","events_url":"https://api.github.com/users/cscenter/events{/privacy}","received_events_url":"https://api.github.com/users/cscenter/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/cscenter/interestscsc","description":"","fork":false,"url":"https://api.github.com/repos/cscenter/interestscsc","forks_url":"https://api.github.com/repos/cscenter/interestscsc/forks","keys_url":"https://api.github.com/repos/cscenter/interestscsc/keys{/key_id}","collaborators_url":"https://api.github.com/repos/cscenter/interestscsc/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/cscenter/interestscsc/teams","hooks_url":"https://api.github.com/repos/cscenter/interestscsc/hooks","issue_events_url":"https://api.github.com/repos/cscenter/interestscsc/issues/events{/number}","events_url":"https://api.github.com/repos/cscenter/interestscsc/events","assignees_url":"https://api.github.com/repos/cscenter/interestscsc/assignees{/user}","branches_url":"https://api.github.com/repos/cscenter/interestscsc/branches{/branch}","tags_url":"https://api.github.com/repos/cscenter/interestscsc/tags","blobs_url":"https://api.github.com/repos/cscenter/interestscsc/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/cscenter/interestscsc/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/cscenter/interestscsc/git/refs{/sha}","trees_url":"https://api.github.com/repos/cscenter/interestscsc/git/trees{/sha}","statuses_url":"https://api.github.com/repos/cscenter/interestscsc/statuses/{sha}","languages_url":"https://api.github.com/repos/cscenter/interestscsc/languages","stargazers_url":"https://api.github.com/repos/cscenter/interestscsc/stargazers","contributors_url":"https://api.github.com/repos/cscenter/interestscsc/contributors","subscribers_url":"https://api.github.com/repos/cscenter/interestscsc/subscribers","subscription_url":"https://api.github.com/repos/cscenter/interestscsc/subscription","commits_url":"https://api.github.com/repos/cscenter/interestscsc/commits{/sha}","git_commits_url":"https://api.github.com/repos/cscenter/interestscsc/git/commits{/sha}","comments_url":"https://api.github.com/repos/cscenter/interestscsc/comments{/number}","issue_comment_url":"https://api.github.com/repos/cscenter/interestscsc/issues/comments{/number}","contents_url":"https://api.github.com/repos/cscenter/interestscsc/contents/{+path}","compare_url":"https://api.github.com/repos/cscenter/interestscsc/compare/{base}...{head}","merges_url":"https://api.github.com/repos/cscenter/interestscsc/merges","archive_url":"https://api.github.com/repos/cscenter/interestscsc/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/cscenter/interestscsc/downloads","issues_url":"https://api.github.com/repos/cscenter/interestscsc/issues{/number}","pulls_url":"https://api.github.com/repos/cscenter/interestscsc/pulls{/number}","milestones_url":"https://api.github.com/repos/cscenter/interestscsc/milestones{/number}","notifications_url":"https://api.github.com/repos/cscenter/interestscsc/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/cscenter/interestscsc/labels{/name}","releases_url":"https://api.github.com/repos/cscenter/interestscsc/releases{/id}","created_at":"2015-09-22T10:18:16Z","updated_at":"2015-09-30T23:27:59Z","pushed_at":"2015-10-09T23:03:38Z","git_url":"git://github.com/cscenter/interestscsc.git","ssh_url":"git@github.com:cscenter/interestscsc.git","clone_url":"https://github.com/cscenter/interestscsc.git","svn_url":"https://github.com/cscenter/interestscsc","homepage":null,"size":176,"stargazers_count":1,"watchers_count":1,"language":"HTML","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":1,"forks":0,"open_issues":1,"watchers":1,"default_branch":"tomita_config_file"}},"base":{"label":"cscenter:master","ref":"master","sha":"4565e94538a41e1230348444479ce79a5cf9fd6f","user":{"login":"cscenter","id":5622041,"avatar_url":"https://avatars.githubusercontent.com/u/5622041?v=3","gravatar_id":"","url":"https://api.github.com/users/cscenter","html_url":"https://github.com/cscenter","followers_url":"https://api.github.com/users/cscenter/followers","following_url":"https://api.github.com/users/cscenter/following{/other_user}","gists_url":"https://api.github.com/users/cscenter/gists{/gist_id}","starred_url":"https://api.github.com/users/cscenter/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cscenter/subscriptions","organizations_url":"https://api.github.com/users/cscenter/orgs","repos_url":"https://api.github.com/users/cscenter/repos","events_url":"https://api.github.com/users/cscenter/events{/privacy}","received_events_url":"https://api.github.com/users/cscenter/received_events","type":"Organization","site_admin":false},"repo":{"id":42926815,"name":"interestscsc","full_name":"cscenter/interestscsc","owner":{"login":"cscenter","id":5622041,"avatar_url":"https://avatars.githubusercontent.com/u/5622041?v=3","gravatar_id":"","url":"https://api.github.com/users/cscenter","html_url":"https://github.com/cscenter","followers_url":"https://api.github.com/users/cscenter/followers","following_url":"https://api.github.com/users/cscenter/following{/other_user}","gists_url":"https://api.github.com/users/cscenter/gists{/gist_id}","starred_url":"https://api.github.com/users/cscenter/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cscenter/subscriptions","organizations_url":"https://api.github.com/users/cscenter/orgs","repos_url":"https://api.github.com/users/cscenter/repos","events_url":"https://api.github.com/users/cscenter/events{/privacy}","received_events_url":"https://api.github.com/users/cscenter/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/cscenter/interestscsc","description":"","fork":false,"url":"https://api.github.com/repos/cscenter/interestscsc","forks_url":"https://api.github.com/repos/cscenter/interestscsc/forks","keys_url":"https://api.github.com/repos/cscenter/interestscsc/keys{/key_id}","collaborators_url":"https://api.github.com/repos/cscenter/interestscsc/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/cscenter/interestscsc/teams","hooks_url":"https://api.github.com/repos/cscenter/interestscsc/hooks","issue_events_url":"https://api.github.com/repos/cscenter/interestscsc/issues/events{/number}","events_url":"https://api.github.com/repos/cscenter/interestscsc/events","assignees_url":"https://api.github.com/repos/cscenter/interestscsc/assignees{/user}","branches_url":"https://api.github.com/repos/cscenter/interestscsc/branches{/branch}","tags_url":"https://api.github.com/repos/cscenter/interestscsc/tags","blobs_url":"https://api.github.com/repos/cscenter/interestscsc/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/cscenter/interestscsc/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/cscenter/interestscsc/git/refs{/sha}","trees_url":"https://api.github.com/repos/cscenter/interestscsc/git/trees{/sha}","statuses_url":"https://api.github.com/repos/cscenter/interestscsc/statuses/{sha}","languages_url":"https://api.github.com/repos/cscenter/interestscsc/languages","stargazers_url":"https://api.github.com/repos/cscenter/interestscsc/stargazers","contributors_url":"https://api.github.com/repos/cscenter/interestscsc/contributors","subscribers_url":"https://api.github.com/repos/cscenter/interestscsc/subscribers","subscription_url":"https://api.github.com/repos/cscenter/interestscsc/subscription","commits_url":"https://api.github.com/repos/cscenter/interestscsc/commits{/sha}","git_commits_url":"https://api.github.com/repos/cscenter/interestscsc/git/commits{/sha}","comments_url":"https://api.github.com/repos/cscenter/interestscsc/comments{/number}","issue_comment_url":"https://api.github.com/repos/cscenter/interestscsc/issues/comments{/number}","contents_url":"https://api.github.com/repos/cscenter/interestscsc/contents/{+path}","compare_url":"https://api.github.com/repos/cscenter/interestscsc/compare/{base}...{head}","merges_url":"https://api.github.com/repos/cscenter/interestscsc/merges","archive_url":"https://api.github.com/repos/cscenter/interestscsc/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/cscenter/interestscsc/downloads","issues_url":"https://api.github.com/repos/cscenter/interestscsc/issues{/number}","pulls_url":"https://api.github.com/repos/cscenter/interestscsc/pulls{/number}","milestones_url":"https://api.github.com/repos/cscenter/interestscsc/milestones{/number}","notifications_url":"https://api.github.com/repos/cscenter/interestscsc/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/cscenter/interestscsc/labels{/name}","releases_url":"https://api.github.com/repos/cscenter/interestscsc/releases{/id}","created_at":"2015-09-22T10:18:16Z","updated_at":"2015-09-30T23:27:59Z","pushed_at":"2015-10-09T23:03:38Z","git_url":"git://github.com/cscenter/interestscsc.git","ssh_url":"git@github.com:cscenter/interestscsc.git","clone_url":"https://github.com/cscenter/interestscsc.git","svn_url":"https://github.com/cscenter/interestscsc","homepage":null,"size":176,"stargazers_count":1,"watchers_count":1,"language":"HTML","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":1,"forks":0,"open_issues":1,"watchers":1,"default_branch":"tomita_config_file"}},"_links":{"self":{"href":"https://api.github.com/repos/cscenter/interestscsc/pulls/1"},"html":{"href":"https://github.com/cscenter/interestscsc/pull/1"},"issue":{"href":"https://api.github.com/repos/cscenter/interestscsc/issues/1"},"comments":{"href":"https://api.github.com/repos/cscenter/interestscsc/issues/1/comments"},"review_comments":{"href":"https://api.github.com/repos/cscenter/interestscsc/pulls/1/comments"},"review_comment":{"href":"https://api.github.com/repos/cscenter/interestscsc/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/cscenter/interestscsc/pulls/1/commits"},"statuses":{"href":"https://api.github.com/repos/cscenter/interestscsc/statuses/311ffb3a1599e57437d707ccba678a3afd5904ff"}}}}