python抓取gitlab日志写入到库里面

抓取gitlab提交日志信息,代码可运行。

import gitlab

import gitlab
import MySQLdb
import time
from datetime import datetime
from datetime import timedelta

if __name__ == '__main__':
    print(1)
    url = 'http://gitlab.centaline.cn'
    token = 'pWZqajTAoyu'
    gl = gitlab.Gitlab(url, private_token=token)
    projects = gl.projects.list(all=True)
    '''
    sz = gl.projects.get(448)
    all_groups = gl.groups.list(all=True)

    for cm in sz.commits.list():
        print(2)
        print(cm.name)
'''

    #gl.projects.list(since='2021-01-01T00:00:00Z')
    conn = MySQLdb.connect(host='ip', user='dev', passwd='123456', db='gitlab', port=3306,
                           charset='utf8')
    sql_commit = "insert into commit values(%s, %s, %s, %s, %s, %s, %s)"
    try:
        with conn.cursor() as cursor:
            for Project in projects:
                conmmparams = []
                in_id = str(Project.id)
                name = Project.name
                in_commits = []
                commits = Project.commits.list(since='2021-11-09T00:00:00Z',)
                # Project.commits.list(since='2021-08-01T00:00:00Z')
                for c in commits:
                    a = [c.id, c.committer_name, c.committer_email,  c.created_at, c.message, c.project_id, name]
                    in_commits.append(a)
                    for i in range(len(in_commits)):
                        print(4)
                        param = tuple(in_commits[i])
                        conmmparams.append(param)
                cursor.executemany(sql_commit, conmmparams)
                conn.commit()
                print(6)

    except MySQLdb.MySQLError as err:
        print(err)
        conn.rollback()
    finally:
        conn.close()

 

posted @ 2021-11-16 17:53  红萝卜  阅读(321)  评论(0编辑  收藏  举报