最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python獲取微信企業(yè)號打卡數(shù)據(jù)并生成windows計劃任務(wù)

 更新時間:2019年04月30日 09:32:39   作者:半杯清茶半杯酒  
由于公司的系統(tǒng)用的是Java版本,開通了企業(yè)號打卡之后又沒有預(yù)算讓供應(yīng)商做數(shù)據(jù)對接,所以只能自己搗鼓這個,以下是個人設(shè)置的一些內(nèi)容,僅供大家參考

由于公司的系統(tǒng)用的是Java版本,開通了企業(yè)號打卡之后又沒有預(yù)算讓供應(yīng)商做數(shù)據(jù)對接,所以只能自己搗鼓這個,以下是個人設(shè)置的一些內(nèi)容,僅供大家參考

安裝python

python的安裝,這里就不詳細(xì)寫了,大家可自行度娘或google。

安裝第三方庫

python安裝好之后別忘記配置環(huán)境變量!另外,所以的內(nèi)容都是安裝在服務(wù)器上的,且服務(wù)器需要能夠上外網(wǎng),否則,只能配置在本地,因為需要外網(wǎng)連接微信企業(yè)號的接口。這里需要用到幾個第三方庫:

python的pip命令,一般python安裝好之后都會默認(rèn)有,如果不確定,可輸入命令查詢,通過cmd進(jìn)入命令提示符,輸入

pip list

如果提示你需要更新,你可以更新,也可以不更新,更新命令其實給到你了python -m pip install --upgrade pip

安裝所需要的庫

Step.1

pip install pymssql

如果安裝pymssql出錯,提示什么visual C++ 14,則先安裝wheel,如不報錯則忽略step2、step3

Step.2

pip install wheel

Step.3

下載pymssql-2.1.4.dev5-cp37-cp37m-win_amd64.whl

可去這里下載最新版本的。pymssql下載

下載好之后,進(jìn)入該文件所在的目錄,通過pip install安裝即可cd D:\

pip install pymssql-2.1.4.dev5-cp37-cp37m-win_amd64.whl

step.4

pip install requests

至此,所有第三方庫都配置好了。

寫主程序

# !/usr/bin/python
# -*- coding:utf-8 -*-
# @Time: 2018/7/26 16:05
# @Author: hychen.cc
import json # 因微信企業(yè)號返回的格式為json,所以引入json
import requests
import pymssql
import math # 引入數(shù)學(xué)方法
import time
import datetime
server = 'XX.XX.XX.XX' # 數(shù)據(jù)庫服務(wù)器地址
user = 'sa' # 數(shù)據(jù)庫登錄名,可以用sa
password = '******' # 數(shù)據(jù)庫用戶對應(yīng)的密碼
dbName = 'DBNAME' # 數(shù)據(jù)庫名稱
CORP_ID = 'XXXXXX' # 微信企業(yè)號提供的CORP_ID
CORP_SECRET = 'XXXXX' # 微信企業(yè)號提供的CORP_SECRET
"""

因微信接口所需要unix時間戳,所以需要把時間轉(zhuǎn)為為Unix時間戳格式

定義時間轉(zhuǎn)換為Unix時間方法

"""def datetime_timestamp(dt):
 # dt為字符串
 # 中間過程,一般都需要將字符串轉(zhuǎn)化為時間數(shù)組
 time.strptime(dt, '%Y-%m-%d %H:%M:%S')
 ## time.struct_time(tm_year=2018, tm_mon=10, tm_mday=25, tm_hour=10, tm_min=0, tm_sec=0, tm_wday=0, tm_yday=88, tm_isdst=-1)
 # 將"2018-10-25 10:00:00"轉(zhuǎn)化為時間戳
 s = time.mktime(time.strptime(dt, '%Y-%m-%d %H:%M:%S'))
 return int(s)
# 定義連接數(shù)據(jù)庫方法
def get_link_server():
 connection = pymssql.connect(server, user, password, database=dbName)
 if connection:
  return connection
 else:
  raise ValueError('Connect DBServer failed.')
"""

定義獲取用戶列表,因為微信企業(yè)號一次最大只能獲取100個,所以需要轉(zhuǎn)換為列表格式,分批次獲取

我這里設(shè)置是從DB中獲取有權(quán)限微信打卡的人員(Select * From Table),換成自己的方式即可

"""
def get_userid_list():
 """
 獲取用戶列表
 :return:
 """
 conn = get_link_server()
 cursor = conn.cursor()
 sql = "Select * From Table"
 cursor.execute(sql)
 row = cursor.fetchone()
 userlist = []
 while row:
  userlist.append(row[0])
  row = cursor.fetchone()
 if userlist:
  return userlist
 else:
  raise ValueError('Get Userlist failed.')
 conn.close()
"""

獲取Access_Token,因為Token有時效(2小時),所以需要存在本地,這樣不需要頻繁調(diào)用,所以我定義了存儲過程(sP_GetWX_access_token)來判斷之前存儲的token是否有效,有效的話就不需要重復(fù)獲取了

"""
def get_access_token(refresh=False):
 """
 獲取Access Token
 :return:
 """
 if not refresh:
  API_ACCESS_TOKEN_URL = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=%s&corpsecret=%s" % (
   CORP_ID, CORP_SECRET)
  response = requests.get(API_ACCESS_TOKEN_URL, verify=False)
  if response.status_code == 200:
   rep_dict = json.loads(response.text)
   errcode = rep_dict.get('errcode')
   if errcode:
    raise ValueError('Get wechat Access Token failed, errcode=%s.' % errcode)
   else:
    access_token = rep_dict.get('access_token')
    if access_token:
     conn = get_link_server()
     cursor = conn.cursor()
     cursor.execute('exec sP_GetWX_access_token @Access_Token=%s', access_token)
     conn.commit()
     conn.close()
     return access_token
    else:
     raise ValueError('Get wechat Access Token failed.')
  else:
   raise ValueError('Get wechat Access Token failed.')
 else:
  conn = get_link_server()
  cursor = conn.cursor()
  cursor.execute("Select Access_Token From wx_AccessToken Where ID=1")
  access_token = cursor.fetchone()
  if access_token:
   return access_token[0]
   conn.close()
  else:
   API_ACCESS_TOKEN_URL = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=%s&corpsecret=%s" % (
    CORP_ID, CORP_SECRET)
   response = requests.get(API_ACCESS_TOKEN_URL, verify=False)
   if response.status_code == 200:
    rep_dict = json.loads(response.text)
    errcode = rep_dict.get('errcode')
    if errcode:
     raise ValueError('Get wechat Access Token failed, errcode=%s.' % errcode)
    else:
     access_token = rep_dict.get('access_token')
     if access_token:
      conn = get_link_server()
      cursor = conn.cursor()
      cursor.execute('exec sP_GetWX_access_token @Access_Token=%s', access_token)
      conn.commit()
      conn.close()
      return access_token
     else:
      raise ValueError('Get wechat Access Token failed.')
   else:
    raise ValueError('Get wechat Access Token failed.')
# 獲取微信打卡的json格式
def get_punchcard_info(access_token, opencheckindatatype, starttime, endtime, useridlist):
 API_PUNCH_CARD_URL = 'https://qyapi.weixin.qq.com/cgi-bin/checkin/getcheckindata?access_token=' + access_token
 json_str = json.dumps(
  {'opencheckindatatype': opencheckindatatype, 'starttime': starttime, 'endtime': endtime, 'useridlist': useridlist})
 response = requests.post(API_PUNCH_CARD_URL, data=json_str, verify=False)
 if response.status_code == 200:
  rep_dic = json.loads(response.text)
  errcode = rep_dic.get('errcode')
  if errcode == 42001:
   access_token = get_access_token(True)
   API_PUNCH_CARD_URL = 'https://qyapi.weixin.qq.com/cgi-bin/checkin/getcheckindata?access_token=' + access_token
   json_str = json.dumps(
    {'opencheckindatatype': opencheckindatatype, 'starttime': starttime, 'endtime': endtime,
    'useridlist': useridlist})
   response = requests.post(API_PUNCH_CARD_URL, data=json_str, verify=False)
   rep_dic = json.loads(response.text)
   errcode = rep_dic.get('errcode')
   if errcode:
    raise ValueError('Get punch data failed1, errcode=%s' % errcode)
   else:
    value_str = rep_dic.get('checkindata')
    if value_str:
     return value_str
    else:
     raise ValueError('Get punch data failed2.')
  elif errcode:
   raise ValueError ('Get punch data failed3, errcode=%s' % errcode)
  else:
   value_str = rep_dic.get('checkindata')
   if value_str:
    return value_str
   else:
    raise ValueError('I do not find employee punch data.')
 else:
  raise ValueError ('Get punch data failed5.')
# 調(diào)用接口,獲得數(shù)據(jù)
if __name__ == '__main__':
 today = datetime.date.today()
 oneday = datetime.timedelta(days=3) # days,即獲取幾天內(nèi)的
 yesterday = today - oneday
 starttime = datetime_timestamp(yesterday.strftime('%Y-%m-%d') + ' 00:00:00')
 endtime = datetime_timestamp(today.strftime('%Y-%m-%d') + ' 23:59:59')
 opencheckindatatype = 3
 access_token = get_access_token()
 if access_token:
  useridlist = get_userid_list()
  if useridlist:
   step = 100
   total = len(useridlist)
   n = math.ceil(total/step)
   for i in range(n):
    # print (useridlist[i*step:(i+1)*step])
    punch_card = get_punchcard_info(access_token, opencheckindatatype, starttime, endtime,useridlist[i*step:(i+1)*step])
    # print (punch_card)
    if punch_card:
     conn = get_link_server()
     cursor = conn.cursor()
     for dic_obj in punch_card:
      cursor.execute('exec sp_AnalysisPunchCard @Json=%s',
          (json.dumps(dic_obj, ensure_ascii=False)))
      # print((json.dumps(dic_obj, ensure_ascii=False))),sp_AnalysisPunchCard把獲取到的數(shù)據(jù)解析后存入數(shù)據(jù)庫中
      conn.commit()
     conn.close()
     print ('Get punch card successed.')
    else:
     raise ValueError('No userlist exists')

設(shè)置Windows計劃任務(wù)

通過控制面板-管理工具-任務(wù)計劃程序,右擊選擇創(chuàng)建基本任務(wù),這里注意的是路徑和程序。

在這里插入圖片描述
在這里插入圖片描述
在這里插入圖片描述
在這里插入圖片描述
在這里插入圖片描述

程序或腳本:python.exe

添加參數(shù)(可選)(A):你的py文件目錄

起始于:python目錄,如果不知道python安裝到哪去了,按照下列cmd命令,輸入python后進(jìn)入python命令查詢

import sys
sys.prefix,回車

到此,配置完成,可自行右擊任務(wù)-執(zhí)行查詢效果,或者通過python命令執(zhí)行py文件

進(jìn)入到py文件目錄

python xxx.py

總結(jié)

以上所述是小編給大家介紹的python獲取微信企業(yè)號打卡數(shù)據(jù)并生成windows計劃任務(wù),希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!

相關(guān)文章

  • Python 循環(huán)語句之 while,for語句詳解

    Python 循環(huán)語句之 while,for語句詳解

    Python中有兩種循環(huán),分別為:for循環(huán)和while循環(huán)。 for循環(huán)可以遍歷任何序列的項目,如一個列表或者一個字符串。while 語句用于循環(huán)執(zhí)行程序,即在某條件下,循環(huán)執(zhí)行某段程序,以處理需要重復(fù)處理的相同任務(wù)。
    2018-04-04
  • Python列表解析操作實例總結(jié)

    Python列表解析操作實例總結(jié)

    這篇文章主要介紹了Python列表解析操作,結(jié)合實例形式總結(jié)分析了Python列表解析常見的列表推導(dǎo)式、循環(huán)語句、條件列表、嵌套列表等相關(guān)操作技巧,需要的朋友可以參考下
    2020-02-02
  • python為Django項目上的每個應(yīng)用程序創(chuàng)建不同的自定義404頁面(最佳答案)

    python為Django項目上的每個應(yīng)用程序創(chuàng)建不同的自定義404頁面(最佳答案)

    這篇文章主要介紹了python為Django項目上的每個應(yīng)用程序創(chuàng)建不同的自定義404頁面,本文給出了最佳答案,大家可以跟隨小編一起學(xué)習(xí)下
    2020-03-03
  • BP神經(jīng)網(wǎng)絡(luò)原理及Python實現(xiàn)代碼

    BP神經(jīng)網(wǎng)絡(luò)原理及Python實現(xiàn)代碼

    這篇文章主要為大家詳細(xì)介紹了BP神經(jīng)網(wǎng)絡(luò)原理,以及Python實現(xiàn)BP神經(jīng)網(wǎng)絡(luò),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-12-12
  • python使用多線程不斷刷新網(wǎng)頁的方法

    python使用多線程不斷刷新網(wǎng)頁的方法

    這篇文章主要介紹了python使用多線程不斷刷新網(wǎng)頁的方法,涉及Python多線程thread及time模塊操作技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-03-03
  • python3-flask-3將信息寫入日志的實操方法

    python3-flask-3將信息寫入日志的實操方法

    在本篇文章里小編給大家整理的是關(guān)于python3-flask-3將信息寫入日志的實操方法,有興趣的朋友們學(xué)習(xí)下。
    2019-11-11
  • Python 剪繩子的多種思路實現(xiàn)(動態(tài)規(guī)劃和貪心)

    Python 剪繩子的多種思路實現(xiàn)(動態(tài)規(guī)劃和貪心)

    這篇文章主要介紹了Python 剪繩子的多種思路實現(xiàn)(動態(tài)規(guī)劃和貪心),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-02-02
  • python通過wxPython打開一個音頻文件并播放的方法

    python通過wxPython打開一個音頻文件并播放的方法

    這篇文章主要介紹了python通過wxPython打開一個音頻文件并播放的方法,實例分析了wxPython操作音頻文件的技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-03-03
  • Pandas中`ValueError: cannot reindex from a duplicate axis`錯誤分析及解決辦法

    Pandas中`ValueError: cannot reindex from 

    在Pandas中,ValueError: cannot reindex from a duplicate axis錯誤通常發(fā)生在嘗試對包含重復(fù)索引的DataFrame或Series進(jìn)行重新索引(reindex)時,所以本文介紹了Pandas中`ValueError: cannot reindex from a duplicate axis`錯誤分析及解決辦法,需要的朋友可以參考下
    2024-07-07
  • 用Python實現(xiàn)一個簡單的線程池

    用Python實現(xiàn)一個簡單的線程池

    這篇文章主要介紹了用Python實現(xiàn)一個簡單的線程池,通過這個小程序可以幫助更好地理解Python中線程的運行機(jī)制,需要的朋友可以參考下
    2015-04-04

最新評論

红桥区| 林口县| 富蕴县| 丹寨县| 玛曲县| 普兰县| 溧水县| 平武县| 行唐县| 怀集县| 新营市| 磐安县| 柞水县| 蚌埠市| 松滋市| 新竹市| 安泽县| 江西省| 忻城县| 区。| 穆棱市| 原阳县| 彭山县| 靖州| 杭锦旗| 昆山市| 岱山县| 全椒县| 古田县| 徐州市| 台安县| 土默特左旗| 洪洞县| 墨江| 顺义区| 红桥区| 宣武区| 阿图什市| 家居| 加查县| 兴文县|