SHOW:
|
|
- or go back to the newest paste.
1 | - | #!/usr/bin/env python3.2 |
1 | + | package main |
2 | ||
3 | - | import logging, urllib.request, urllib.parse, re, concurrent.futures, itertools |
3 | + | import ( |
4 | "http" | |
5 | - | def language_popularity(lang): |
5 | + | "io/ioutil" |
6 | - | qlang = urllib.parse.quote(lang) |
6 | + | "regexp" |
7 | - | try: |
7 | + | "url" |
8 | - | sub = urllib.request.urlopen("http://github.com/languages/" + qlang).read() |
8 | + | "strconv" |
9 | - | except: |
9 | + | ) |
10 | - | logging.exception('Error fetching %r', lang) |
10 | + | |
11 | - | return |
11 | + | var langre = regexp.MustCompile("/languages/([^/\"]+)") |
12 | - | match = re.search(b'is the #(\\d+)', sub) |
12 | + | |
13 | - | if match: |
13 | + | func languages() (langs []string) { |
14 | - | rank = int(match.group(1)) |
14 | + | resp, err := http.Get("http://github.com/languages") |
15 | - | else: |
15 | + | if err != nil { |
16 | - | rank = 1 |
16 | + | panic(err) |
17 | - | return rank, lang |
17 | + | } |
18 | body, err := ioutil.ReadAll(resp.Body) | |
19 | - | def languages(): |
19 | + | if err != nil { |
20 | - | for match in re.finditer(b'/languages/([^/]+?)"', urllib.request.urlopen('http://github.com/languages').read()): |
20 | + | panic(err) |
21 | - | yield urllib.parse.unquote(match.group(1).decode()) |
21 | + | } |
22 | seen := make(map[string]bool) | |
23 | - | def main(): |
23 | + | for _, subm := range langre.FindAllStringSubmatch(string(body), -1) { |
24 | - | futures = [] |
24 | + | unlang, err := url.QueryUnescape(subm[1]) |
25 | - | with concurrent.futures.ThreadPoolExecutor(45) as pool: |
25 | + | if err != nil { panic(err) } |
26 | - | for lang in languages(): |
26 | + | if !seen[unlang] { |
27 | - | futures.append(pool.submit(language_popularity, lang)) |
27 | + | seen[unlang] = true |
28 | - | ranks = {} |
28 | + | langs = append(langs, subm[1]) |
29 | - | completed = concurrent.futures.as_completed(futures) |
29 | + | } |
30 | - | for rank in itertools.count(1): |
30 | + | } |
31 | - | while rank not in ranks: |
31 | + | return |
32 | - | try: |
32 | + | } |
33 | - | result = next(completed).result() |
33 | + | |
34 | - | except StopIteration: |
34 | + | var rankre = regexp.MustCompile("is the #([0-9]+)") |
35 | - | break |
35 | + | var mostpop = regexp.MustCompile("is <strong>the most") |
36 | - | if result is None: |
36 | + | |
37 | - | continue |
37 | + | func popularity(lang string) int { |
38 | - | new_rank, lang = result |
38 | + | url := "http://github.com/languages/" + lang |
39 | - | ranks[new_rank] = lang |
39 | + | //~ println(url) |
40 | - | if rank not in ranks: |
40 | + | resp, err := http.Get(url) |
41 | if err != nil {panic(err)} | |
42 | - | print(rank, ranks[rank]) |
42 | + | body, err := ioutil.ReadAll(resp.Body) |
43 | - | for rank1 in sorted(r for r in ranks if r >= rank): |
43 | + | if err != nil { |
44 | - | print(rank1, ranks[rank1]) |
44 | + | panic(err) |
45 | } | |
46 | - | if __name__ == '__main__': |
46 | + | subm := rankre.FindSubmatch(body) |
47 | - | main() |
47 | + | if subm == nil { |
48 | if mostpop.Find(body) != nil { | |
49 | return 1 | |
50 | } | |
51 | return 0 | |
52 | } | |
53 | rank, err := strconv.Atoi(string(subm[1])) | |
54 | if err != nil {panic(err)} | |
55 | return rank | |
56 | } | |
57 | ||
58 | type langpop struct { Lang string; Rank int } | |
59 | ||
60 | func main() { | |
61 | rankch := make(chan langpop) | |
62 | nlangs := 0 | |
63 | for _, lang := range languages() { | |
64 | go func(lang string) { | |
65 | rankch<-langpop{lang, popularity(lang)} | |
66 | }(lang) | |
67 | nlangs++ | |
68 | } | |
69 | ranks := make(map[int]string) | |
70 | for r := 1; ; r++ { | |
71 | for { | |
72 | if _, ok := ranks[r]; ok { | |
73 | break | |
74 | } else if nlangs == 0 { | |
75 | return | |
76 | } | |
77 | lp := <-rankch | |
78 | //~ println(lp.Rank, lp.Lang) | |
79 | ranks[lp.Rank] = lp.Lang | |
80 | nlangs-- | |
81 | } | |
82 | plang, err := url.QueryUnescape(ranks[r]) | |
83 | if err != nil { | |
84 | panic(err) | |
85 | } | |
86 | println(r, plang) | |
87 | } | |
88 | } |