GetFeiShuData.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. from numpy import empty
  2. import pandas as pd
  3. from pandas.core.frame import DataFrame
  4. import requests
  5. import json
  6. import datetime,time
  7. import numpy as np
  8. def getFeiShuDATA():
  9. Columns=["状态", "发生时间","代理商","电池编码","用户信息","业务分类A","业务分类B","操作","信息来源", "内容描述", "客服处理时间","客服处理结果","记录人","当前处理人","处理部门","跟进记录","问题类型A","问题类型B","事件处理完结时间","维修信息","返厂时间"]
  10. # 获取token
  11. app_id = 'cli_a1e503bb3d78100c'
  12. app_secret = 'oZBbGSRYsf9sXy8t8e8kbhVLciekyvMt'
  13. url = 'https://open.feishu.cn/open-apis/auth/v3/tenant_access_token/internal'
  14. headers = {'Content-Type':"application/json; charset=utf-8"}
  15. datas = json.dumps({'app_id':app_id, 'app_secret':app_secret})
  16. response = requests.post(url, data=datas, headers=headers)
  17. auth_token = json.loads(response.text)['tenant_access_token']
  18. # 获取数据
  19. app_token = 'bascnpaW50pJsCNd1AyIRFIc24b'
  20. table_id = 'tblo3wnR2HFWI6rA'
  21. sheet_id = 'vewZh94xDC'
  22. url = 'https://open.feishu.cn/open-apis/bitable/v1/apps/{}/tables/{}/records'.format(app_token, table_id)
  23. headers = {"Authorization":"Bearer {}".format(auth_token)}
  24. df_file = pd.DataFrame(columns=Columns)
  25. timesort=['发生时间','客服处理时间','事件处理完结时间','返厂时间']
  26. count=0
  27. GotPageToken=str()
  28. while True:
  29. # 筛选条件加载这里
  30. datas = {'view_id':sheet_id, 'page_size':100, 'text_field_as_array':"true", 'page_token':GotPageToken,'field_names':'["状态", "发生时间","代理商","电池编码","用户信息","业务分类A","业务分类B","操作","信息来源", "内容描述", "客服处理时间","客服处理结果","记录人","当前处理人","处理部门","跟进记录","问题类型A","问题类型B","事件处理完结时间","维修信息","返厂时间"]'}
  31. try:
  32. response = requests.get(url, params=datas, headers=headers)
  33. results = json.loads(response.text)['data']['items']
  34. GotPageToken=json.loads(response.text)['data']['page_token']
  35. for r in results:
  36. df_file = df_file.append([r['fields']], ignore_index=True)
  37. except:
  38. break
  39. for i in range(0,len(df_file)):
  40. for t in range(0,len(timesort)):
  41. if not df_file.loc[i,timesort[t]] is np.nan:
  42. # temp_para = df_file.loc[i,timesort[t]
  43. # timeTemp= float(temp_para/1000)
  44. # temp_para = df_file.loc[i,timesort[t]
  45. # print(i)
  46. # print(t)
  47. # print(df_file.loc[i,timesort[t]])
  48. timeTemp= float(df_file.loc[i,timesort[t]]/1000)
  49. timeTTmp=time.localtime(timeTemp)
  50. df_file.loc[i,timesort[t]] = time.strftime('%Y-%m-%d %H:%M:%S',timeTTmp)
  51. for col in Columns:
  52. df_file[col]=df_file[col].map(lambda x:str(x).lstrip('[{\'text\': ').rstrip('\', \'type\': \'text\'}]').replace('\\n\', \'type\': \'text\'}, {\'text\': \'',' '))
  53. df_file.to_excel('df_file.xlsx')
  54. return df_file