roomservice: Improve new device retrieval

Without credentials, GitHub's search API limits requests to 60/hr.
The existing method to add a new device is to fetch JSON-formatted info
for ALL CM repositories and then search for the device. In doing so,
more than 10 pages of results are returned (i.e. more than 10 requests
per device). This is clumsy, slow, and limits use of roomservice to
only ~5 devices per hour.

Instead, only return search results for repositories that have the
device name in the repository name. Then, one device = one request.
It's faster and allows closer to 60 device setups / hr.

Additional bailouts are included to stop the script earlier than later
if a device is not found.

Change-Id: I7f914d7ede82da0f100d9fd6cf8b603177962e48
diff --git a/tools/roomservice.py b/tools/roomservice.py
index f5aac06..86f75c3 100755
--- a/tools/roomservice.py
+++ b/tools/roomservice.py
@@ -52,16 +52,20 @@
     if githubauth:
         githubreq.add_header("Authorization","Basic %s" % githubauth)
 
-page = 1
-while not depsonly:
-    githubreq = urllib2.Request("https://api.github.com/users/CyanogenMod/repos?per_page=200&page=%d" % page)
+if not depsonly:
+    githubreq = urllib.request.Request("https://api.github.com/search/repositories?q=%s+user:CyanogenMod+in:name" % device)
     add_auth(githubreq)
-    result = json.loads(urllib2.urlopen(githubreq).read())
-    if len(result) == 0:
-        break
-    for res in result:
+    result = json.loads(urllib.request.urlopen(githubreq).read().decode())
+    try:
+        numresults = int(result['total_count'])
+    except:
+        print("Failed to search GitHub (offline?)")
+        sys.exit()
+    if (numresults == 0):
+        print("Could not find device %s on github.com/CyanogenMod" % device)
+        sys.exit()
+    for res in result['items']:
         repositories.append(res)
-    page = page + 1
 
 local_manifests = r'.repo/local_manifests'
 if not os.path.exists(local_manifests): os.makedirs(local_manifests)