1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2014 Alexis Laferrière <alexis.laf@xymus.net>
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
17 # Script to scan Github for repositories possibly using JNI.
18 module github_search_for_jni
22 # The proprieties introduced by this redef are to be used only on HashMap
23 # representing a Github repository.
25 # REQUIRE `assert self isa HashMap[String, nullable Object]`
26 # REQUIRE `for v in self.values do assert v isa Int
27 redef class HashMap[K
, V
]
28 # The repository has at least 50% Java code
29 fun has_lots_of_java
: Bool
32 if keys
.has
("Java") then java_count
= self["Java"].as(Int)
34 if java_count
== 0 then return false
43 return java_count
* 100 / total
> 50
46 # The repository has at least 100 lines of C code
50 if keys
.has
("C") then c_count
= self["C"].as(Int)
55 # Query sent to Github
56 var main_query
= "language:java"
58 # Curl instance use for all requests
59 var curl
= new GithubCurl("OAUTH TOKEN (replace with your own)", "JNI project finder (nitlanguage.org)")
61 if "NIT_TESTING".environ
== "true" then exit
0
63 # Current requested page
67 # Get a page of the main query
68 var uri
= "https://api.github.com/search/repositories?q={main_query}&page={page}&per_page={per_page}&sort=stars"
69 var obj
= curl
.get_and_check
(uri
).as(HashMap[String, nullable Object])
71 # Main object has "total_count" and "items"
72 var items
= obj
["items"].as(Array[nullable Object])
74 # "items" is an array of Json objects
76 assert item
isa HashMap[String, nullable Object]
78 # Each item has "name" and "languages_url"
79 assert item
.keys
.has
("name")
80 assert item
.keys
.has
("languages_url")
82 # Download the language list
83 var lang_url
= item
["languages_url"].as(String)
84 var langs
= curl
.get_and_check
(lang_url
).as(HashMap[String, nullable Object])
86 # The project is of interest if it has lots of Java and at least some C
87 var may_be_of_interest
= langs
.has_lots_of_java
and langs
.has_some_c
88 if may_be_of_interest
then print
"{item["name"].to_s}: {item["forks"].to_s}; {langs.keys.join(", ")}; {item["html_url"].to_s}"
91 # If we got less pages than asked for, we are done!
92 if items
.length
< per_page
then break