Advertisement
Guest User

FetchCodeforcesTags

a guest
Nov 19th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 2.26 KB | None | 0 0
  1. package com.bogdan.codeforceswatcher.util
  2.  
  3. import kotlinx.coroutines.GlobalScope
  4. import kotlinx.coroutines.launch
  5. import java.io.BufferedReader
  6. import java.io.InputStreamReader
  7. import java.net.HttpURLConnection
  8. import java.net.URL
  9. import java.sql.DriverManager.println
  10. import java.util.*
  11. import java.util.regex.Pattern
  12.  
  13. /*
  14.  * hello,karankapoor:)
  15.  * I am AnswerNotFound.
  16.  * this Java program helps you find all tags in codeforces
  17.  */
  18. object FindAllTags {
  19.  
  20.     private val set = HashSet<String>()
  21.  
  22.     fun getContent(urlString: String): String {
  23.         println("getContent")
  24.         val url = URL(urlString)
  25.         val urlConnection = url.openConnection() as HttpURLConnection
  26.         val reader = BufferedReader(InputStreamReader(urlConnection.inputStream))
  27.         var line: String?
  28.         var ans = ""
  29.         line = reader.readLine()
  30.         while (line != null) {
  31.             ans += line + "\n"
  32.             line = reader.readLine()
  33.         }
  34.         return ans
  35.     }
  36.  
  37.     fun getKeywords(content: String): ArrayList<String> {
  38.         println("getKeywords")
  39.         val list = ArrayList<String>()
  40.         val pattern = Pattern.compile("<a href=\"/problemset?tags=[^,]")
  41.         val matcher = pattern.matcher(content)
  42.         while (matcher.find()) {
  43.             var tag = matcher.group()
  44.             tag = tag.substring(26, tag.length - 1)
  45.             list.add(tag)
  46.         }
  47.         return list
  48.     }
  49.  
  50.     @Throws(Exception::class)
  51.     fun solve() {
  52.         //GlobalScope.launch {
  53.         val lastPage = "<a href=\"/problemset/problem/1/A\">"
  54.         for (i in 1..1000) {
  55.             val urlString = "http://codeforces.com/problemset/page/$i"
  56.             val content = getContent(urlString)
  57.             val list = getKeywords(content)
  58.             for (s in list) {
  59.                 set.add(s)
  60.             }
  61.             if (content.contains(lastPage)) {
  62.                 kotlin.io.println("page stop at $i");
  63.                 break
  64.             }
  65.             println("i $i")
  66.         }
  67.  
  68.         kotlin.io.println("here are " + set.size + " tags on codeforces now. they are:")
  69.         for (tag in set) {
  70.             print("$tag,")
  71.         }
  72.         //}
  73.     }
  74.  
  75.     @JvmStatic
  76.     fun main(args: Array<String>) {
  77.         solve()
  78.     }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement