python写工作日志

代码如下:

# -*- coding: utf-8 -*-

import Tkinter as tk
import ttk
import tkMessageBox
import datetime
import urllib2
import requests
import sys
from bs4 import BeautifulSoup as bs
reload(sys)
sys.setdefaultencoding('utf-8')

proxy = {'https': '122.72.18.34:80'}
week_session = requests.session()
week_session.keep_alive = False

def is_weekday(date):
    url = 'https://api.goseek.cn/Tools/holiday?date=' + date
    res_get = week_session.get(url, verify=False)
    res = res_get.json()['data']
    return not res

def getExePath():
    sap = '/'
    if sys.argv[0].find(sap) == -1:
        sap = '\\'
    indx = sys.argv[0].rfind(sap)
    path = sys.argv[0][:indx] + sap
    return path

# 设置窗口居中
def window_info():
    ws = window.winfo_screenwidth()
    hs = window.winfo_screenheight()
    x = (ws / 2) - 200
    y = (hs / 2) - 200
    return x, y

# 设置登陆窗口属性
window = tk.Tk()
window.title('Log Tool')
a, b = window_info()
window.geometry("480x300+%d+%d" % (a, b))

infos = []
#读取文本信息
file_object = open(getExePath() + 'info.txt')
try:
    for line in file_object:
         infos.append(line.rstrip('\n').decode('gbk').encode('utf-8'))
finally:
    file_object.close()

# 登陆界面的信息
tk.Label(window, text="git令牌:",font=("宋体", 14)).place(x=80, y=20)
tk.Label(window, text="git用户名:",font=("宋体", 14)).place(x=80, y=50)
tk.Label(window, text="日志人姓名:",font=("宋体", 14)).place(x=80, y=80)
tk.Label(window, text="日志用户名:",font=("宋体", 14)).place(x=80, y=110)
tk.Label(window, text="日志密码:",font=("宋体", 14)).place(x=80, y=140)
# 显示输入框
private_tokeninput = tk.StringVar()
private_tokeninput.set(infos[0])
entry_private_token = tk.Entry(window, textvariable=private_tokeninput)
entry_private_token.place(x=230, y=20)

git_committer_nameinput = tk.StringVar()
git_committer_nameinput.set(infos[1])
entry_git_committer_name = tk.Entry(window, textvariable=git_committer_nameinput)
entry_git_committer_name.place(x=230, y=50)

log_nameinput = tk.StringVar()
log_nameinput.set(infos[2])
entry_log_name = tk.Entry(window, textvariable=log_nameinput)
entry_log_name.place(x=230, y=80)

log_usernameinput = tk.StringVar()
log_usernameinput.set(infos[3])
entry_log_username = tk.Entry(window, textvariable=log_usernameinput)
entry_log_username.place(x=230, y=110)

log_pwdinput = tk.StringVar()
log_pwdinput.set(infos[4])
entry_log_pwd = tk.Entry(window, textvariable=log_pwdinput, show='*')
entry_log_pwd.place(x=230, y=140)

now = datetime.datetime.now()

# today = time.strftime('%Y%m%d',time.localtime(time.time()))
# month_bg = time.strftime('%Y%m01',time.localtime(time.time()))
s = 1
day_list = []
while s <= 31:
    day_list.append(str(now.year) + '-' + str(now.month).zfill(2) + '-' + str(s).zfill(2))
    s += 1

start_day_box = ttk.Combobox(window,values=day_list)
start_day_box.pack()
start_day_box.place(x=55,y=185)
tk.Label(window, text="自:",font=("宋体", 10)).place(x=15, y=185)

end_day_box = ttk.Combobox(window,values=day_list)
end_day_box.pack()
end_day_box.place(x=280,y=185)
tk.Label(window, text="至:",font=("宋体", 10)).place(x=235, y=185)

def send_single_day_message(date,private_token,git_committer_name,log_name,log_username,log_pwd):
    error_message = ""
    timeStruct = datetime.datetime.strptime(date, "%Y-%m-%d")
    since_date = (timeStruct + datetime.timedelta(days=-1)).strftime("%Y-%m-%d")
    log_day = timeStruct.strftime("%Y年%m月%d日")
    since = since_date + "T16:00:00.000+00:00"
    until = date + "T16:00:00.000+00:00"

    origin_url = 'http://192.168.1.9/api/v4/projects/1/repository/commits?'

    url = origin_url + "private_token=" + private_token + '&' + 'since=' + since + '&' + 'until=' + until
    #
    req = urllib2.Request(url)

    response = urllib2.urlopen(req)
    html = response.read()
    content = "工作内容:\n"
    i = 1
    log_str = ""
    for h in eval(html):
        if h['committer_name'] == git_committer_name:
            log_str += str(i) + '.' + h['title'] + '\n'
            i += 1
    if log_str == "":
        error_message += log_day + "没有在gitlab上提交任务,请手动补充\n"
    content += log_str
    requests.packages.urllib3.disable_warnings()

    req = requests.session()

    headers = dict()
    headers[
        'User-Agent'] = 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.62 Safari/537.36'

    first_get = req.get('http://work.karrytech.com:8681/redmine/login', verify=False)
    response = bs(first_get.content, 'html.parser')
    authenticity_token = response.find('input', attrs={'name': 'authenticity_token'})['value']

    login_data = {
        'authenticity_token': authenticity_token,
        'username': log_username,
        'password': log_pwd,
        'login': "登录 »"
    }
    login_url = "http://work.karrytech.com:8681/redmine/login"

    login_ret = req.post(url=login_url,
                         data=login_data,
                         headers=headers,
                         verify=False
                         )

    q = 'q=' + log_day + ' ' + log_name
    old_news_url = 'http://work.karrytech.com:8681/redmine/search/index/job-5?' + q + '&scope=&all_words=1&news=1&submit=提交'
    old_news = req.get(old_news_url,verify=False)
    old_news_content = bs(old_news.content,'html.parser')
    search_result = old_news_content.find('h3').text
    if search_result == '结果 (0)':
        news_get = req.get('http://work.karrytech.com:8681/redmine/projects/job-5/news', verify=False)
        news_response = bs(news_get.content, 'html.parser')

        authenticity_token = news_response.find('input', attrs={'name': 'authenticity_token'})['value']

        data = {'authenticity_token': authenticity_token,
                'news[title]': log_day + " 工作日志 " + log_name,
                'news[summary]': "",
                'news[description]': content,
                'commit': '创建'
                }
        url = 'http://work.karrytech.com:8681/redmine/projects/job-5/news/new'

        ret = req.post(url=url,
                       data=data,
                       headers=headers,
                       verify=False
                       )
        return error_message
    return log_day + "日志已填写\n"

def send_message(start_date,end_date,private_token,git_committer_name,log_name,log_username,log_pwd):
    start = start_date
    error_message = ""
    while start <= end_date:
        if is_weekday(start.replace('-','')):
            error_message += send_single_day_message(start, private_token, git_committer_name, log_name, log_username, log_pwd)
        start = (datetime.datetime.strptime(start, '%Y-%m-%d') + datetime.timedelta(days=1)).strftime("%Y-%m-%d")
    return error_message


def writ_log():
    private_token = private_tokeninput.get()
    git_committer_name = git_committer_nameinput.get()
    log_name = log_nameinput.get()
    log_username = log_usernameinput.get()
    log_pwd = log_pwdinput.get()

    if not private_token:
        tkMessageBox.showerror(message='请输入git令牌!')
    elif not git_committer_name:
        tkMessageBox.showerror(message='请输入git用户名!')
    elif not log_name:
        tkMessageBox.showerror(message='请输入日志人姓名!')
    elif not log_username:
        tkMessageBox.showerror(message='请输入日志用户名!')
    elif not log_pwd:
        tkMessageBox.showerror(message='请输入日志密码!')
    else:
        error_message = send_message(start_day_box.get(),end_day_box.get(),private_token,git_committer_name,log_name,log_username,log_pwd)
        if error_message == "":
            message = "提交成功!"
        else:
            message = error_message

        tkMessageBox.showinfo('提示', message)




btn_login = tk.Button(window, text="一键填写", command=writ_log,width=20)
btn_login.place(x=165, y=250)


window.mainloop()

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值