import os
import json
import urllib.request

def httpGet(url, params={}):
    real_url = url + urllib.parse.urlencode(params)
    req = urllib.request.Request(real_url)
    req.add_header('Private-Token', 'cRjSP2EUx1SaQYcis9W7')
    data = urllib.request.urlopen(req).read()
    return json.loads(data.decode('utf-8'))

projects = httpGet('http://git.kingsome.cn/api/v4/projects?',
                   {
                       'page': 1,
                       'per_page': 1000
                   })
for proj in projects:
    http_url_to_repo = proj['http_url_to_repo']
    name = proj['name']
    path = proj['namespace']['path']
    if path != 'publish':
        continue
    if name == 'game1008_wsproxy':
        continue
    if not os.path.exists('repository/%s' % path):
        os.mkdir('repository/%s' % (path))
    if not os.path.exists('repository/%s/%s' % (path,  name)):
        os.system('cd repository/%s && git clone %s' % (path, http_url_to_repo))
    os.system('cd repository/%s/%s && git pull' % (path, name))
    os.system('cd repository/%s/%s && git submodule init' % (path, name))
    os.system('cd repository/%s/%s && git submodule update' % (path, name))
    submodules = os.listdir('repository/%s/%s/third_party' % (path, name))
    for module_name in submodules:
        os.system('cd repository/%s/%s/third_party/%s && git checkout master && git pull' %
                  (path, name, module_name))
    os.system('cd repository/%s/%s && git commit -am "同步代码" && git push' % (path, name))
    print('%s/%s' % (path, name))
