123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897 |
- # -*- coding: utf-8 -*-
- # @Author: wangkun
- # @Time: 2022/5/27
- import json
- import time
- import requests
- import urllib3
- from common import Common
- proxies = {"http": None, "https": None}
- class Feishu:
- """
- 编辑飞书云文档
- """
- # 看一看爬虫数据表
- kanyikan_url = "https://w42nne6hzg.feishu.cn/sheets/shtcngRPoDYAi24x52j2nDuHMih?"
- # 快手爬虫数据表
- kuaishou_url = "https://w42nne6hzg.feishu.cn/sheets/shtcnp4SaJt37q6OOOrYzPMjQkg?"
- # 微视爬虫数据表
- weishi_url = "https://w42nne6hzg.feishu.cn/sheets/shtcn5YSWg91JfVGzj0SFZIRRPh?"
- # 小年糕爬虫数据表
- xiaoniangao_url = "https://w42nne6hzg.feishu.cn/sheets/shtcnYxiyQ1wLklo1W5Kdqc9cGh?"
- # twitter 爬虫吧
- twitter_url = "https://whtlrai9ej.feishu.cn/sheets/shtcn6BYfYuqegIP13ORB6rI2dh?"
- # 飞书路径token
- @classmethod
- def spreadsheettoken(cls, crawler):
- """
- :param crawler: 哪个爬虫
- """
- if crawler == "kanyikan":
- return "shtcngRPoDYAi24x52j2nDuHMih"
- elif crawler == "kuaishou":
- return "shtcnp4SaJt37q6OOOrYzPMjQkg"
- elif crawler == "weishi":
- return "shtcn5YSWg91JfVGzj0SFZIRRPh"
- elif crawler == "xiaoniangao":
- return "shtcnYxiyQ1wLklo1W5Kdqc9cGh"
- elif crawler == "twitter":
- return "shtcn6BYfYuqegIP13ORB6rI2dh"
- # 获取飞书api token
- @classmethod
- def get_token(cls):
- """
- 获取飞书api token
- :return:
- """
- time.sleep(1)
- url = "https://open.feishu.cn/open-apis/auth/v3/tenant_access_token/internal/"
- post_data = {"app_id": "cli_a13ad2afa438d00b", # 这里账号密码是发布应用的后台账号及密码
- "app_secret": "4tK9LY9VbiQlY5umhE42dclBFo6t4p5O"}
- try:
- urllib3.disable_warnings()
- response = requests.post(url=url, data=post_data, proxies=proxies, verify=False)
- tenant_access_token = response.json()["tenant_access_token"]
- return tenant_access_token
- except Exception as e:
- Common.logger().error("获取飞书 api token 异常:{}", e)
- # 获取表格元数据
- @classmethod
- def get_metainfo(cls, crawler):
- """
- 获取表格元数据
- :return:
- """
- get_metainfo_url = "https://open.feishu.cn/open-apis/sheets/v2/spreadsheets/" \
- + cls.spreadsheettoken(crawler) + "/metainfo"
- headers = {
- "Authorization": "Bearer " + cls.get_token(),
- "Content-Type": "application/json; charset=utf-8"
- }
- params = {
- "extFields": "protectedRange", # 额外返回的字段,extFields=protectedRange时返回保护行列信息
- "user_id_type": "open_id" # 返回的用户id类型,可选open_id,union_id
- }
- try:
- urllib3.disable_warnings()
- r = requests.get(url=get_metainfo_url, headers=headers, params=params, proxies=proxies, verify=False)
- response = json.loads(r.content.decode("utf8"))
- return response
- except Exception as e:
- Common.logger().error("获取表格元数据异常:{}", e)
- # 读取工作表中所有数据
- @classmethod
- def get_values_batch(cls, crawler, sheetid):
- """
- 读取工作表中所有数据
- :param crawler: 哪个爬虫
- :param sheetid: 哪张表
- :return: 所有数据
- """
- get_values_batch_url = "https://open.feishu.cn/open-apis/sheets/v2/spreadsheets/" \
- + cls.spreadsheettoken(crawler) + "/values_batch_get"
- headers = {
- "Authorization": "Bearer " + cls.get_token(),
- "Content-Type": "application/json; charset=utf-8"
- }
- params = {
- # 多个查询范围 如 url?ranges=range1,range2 ,其中 range 包含 sheetId 与单元格范围两部分
- "ranges": sheetid,
- # valueRenderOption=ToString 可返回纯文本的值(数值类型除外);
- # valueRenderOption=FormattedValue 计算并格式化单元格;
- # valueRenderOption=Formula单元格中含有公式时返回公式本身;
- # valueRenderOption=UnformattedValue计算但不对单元格进行格式化
- "valueRenderOption": "ToString",
- # dateTimeRenderOption=FormattedString 计算并将时间日期按照其格式进行格式化,但不会对数字进行格式化,返回格式化后的字符串。
- "dateTimeRenderOption": "",
- # 返回的用户id类型,可选open_id,union_id
- "user_id_type": "open_id"
- }
- try:
- urllib3.disable_warnings()
- r = requests.get(url=get_values_batch_url, headers=headers, params=params, proxies=proxies, verify=False)
- response = json.loads(r.content.decode("utf8"))
- values = response["data"]["valueRanges"][0]["values"]
- return values
- except Exception as e:
- Common.logger().error("读取工作表所有数据异常:{}", e)
- # 工作表,插入行或列
- @classmethod
- def insert_columns(cls, crawler, sheetid, majordimension, startindex, endindex):
- """
- 工作表插入行或列
- :param crawler: 哪个爬虫
- :param sheetid:哪张工作表
- :param majordimension:行或者列
- :param startindex:开始位置
- :param endindex:结束位置
- """
- insert_columns_url = "https://open.feishu.cn/open-apis/sheets/v2/spreadsheets/" \
- + cls.spreadsheettoken(crawler) + "/insert_dimension_range"
- headers = {
- "Authorization": "Bearer " + cls.get_token(),
- "Content-Type": "application/json; charset=utf-8"
- }
- body = {
- "dimension": {
- "sheetId": sheetid,
- "majorDimension": majordimension, # 默认 ROWS ,可选 ROWS、COLUMNS
- "startIndex": startindex, # 开始的位置
- "endIndex": endindex # 结束的位置
- },
- "inheritStyle": "AFTER" # BEFORE 或 AFTER,不填为不继承 style
- }
- try:
- urllib3.disable_warnings()
- r = requests.post(url=insert_columns_url, headers=headers, json=body, proxies=proxies, verify=False)
- Common.logger().info("插入行或列:{}", r.json()["msg"])
- except Exception as e:
- Common.logger().error("插入行或列异常:{}", e)
- # 写入数据
- @classmethod
- def update_values(cls, crawler, sheetid, ranges, values):
- """
- 写入数据
- :param crawler: 哪个爬虫
- :param sheetid:哪张工作表
- :param ranges:单元格范围
- :param values:写入的具体数据,list
- """
- update_values_url = "https://open.feishu.cn/open-apis/sheets/v2/spreadsheets/" \
- + cls.spreadsheettoken(crawler) + "/values_batch_update"
- headers = {
- "Authorization": "Bearer " + cls.get_token(),
- "Content-Type": "application/json; charset=utf-8"
- }
- body = {
- "valueRanges": [
- {
- "range": sheetid + "!" + ranges,
- "values": values
- },
- ],
- }
- try:
- urllib3.disable_warnings()
- r = requests.post(url=update_values_url, headers=headers, json=body, proxies=proxies, verify=False)
- Common.logger().info("写入数据:{}", r.json()["msg"])
- except Exception as e:
- Common.logger().error("写入数据异常:{}", e)
- # 合并单元格
- @classmethod
- def merge_cells(cls, crawler, sheetid, ranges):
- """
- 合并单元格
- :param crawler: 哪个爬虫
- :param sheetid:哪张工作表
- :param ranges:需要合并的单元格范围
- """
- merge_cells_url = "https://open.feishu.cn/open-apis/sheets/v2/spreadsheets/" \
- + cls.spreadsheettoken(crawler) + "/merge_cells"
- headers = {
- "Authorization": "Bearer " + cls.get_token(),
- "Content-Type": "application/json; charset=utf-8"
- }
- body = {
- "range": sheetid + "!" + ranges,
- "mergeType": "MERGE_ROWS"
- }
- try:
- urllib3.disable_warnings()
- r = requests.post(url=merge_cells_url, headers=headers, json=body, proxies=proxies, verify=False)
- Common.logger().info("合并单元格:{}", r.json()["msg"])
- except Exception as e:
- Common.logger().error("合并单元格异常:{}", e)
- # 读取单元格数据
- @classmethod
- def get_range_value(cls, crawler, sheetid, cell):
- """
- 读取单元格内容
- :param crawler: 哪个爬虫
- :param sheetid: 哪张工作表
- :param cell: 哪个单元格
- :return: 单元格内容
- """
- get_range_value_url = "https://open.feishu.cn/open-apis/sheets/v2/spreadsheets/" \
- + cls.spreadsheettoken(crawler) + "/values/" + sheetid + "!" + cell
- headers = {
- "Authorization": "Bearer " + cls.get_token(),
- "Content-Type": "application/json; charset=utf-8"
- }
- params = {
- # valueRenderOption=ToString 可返回纯文本的值(数值类型除外);
- # valueRenderOption=FormattedValue 计算并格式化单元格;
- # valueRenderOption=Formula 单元格中含有公式时返回公式本身;
- # valueRenderOption=UnformattedValue 计算但不对单元格进行格式化。
- "valueRenderOption": "FormattedValue",
- # dateTimeRenderOption=FormattedString 计算并对时间日期按照其格式进行格式化,但不会对数字进行格式化,返回格式化后的字符串。
- "dateTimeRenderOption": "",
- # 返回的用户id类型,可选open_id,union_id
- "user_id_type": "open_id"
- }
- try:
- urllib3.disable_warnings()
- r = requests.get(url=get_range_value_url, headers=headers, params=params, proxies=proxies, verify=False)
- return r.json()["data"]["valueRange"]["values"][0]
- except Exception as e:
- Common.logger().error("读取单元格数据异常:{}", e)
- # 删除行或列,可选 ROWS、COLUMNS
- @classmethod
- def dimension_range(cls, crawler, sheetid, major_dimension, startindex, endindex):
- """
- 删除行或列
- :param crawler: 哪个爬虫
- :param sheetid:工作表
- :param major_dimension:默认 ROWS ,可选 ROWS、COLUMNS
- :param startindex:开始的位置
- :param endindex:结束的位置
- :return:
- """
- dimension_range_url = "https://open.feishu.cn/open-apis/sheets/v2/spreadsheets/" \
- + cls.spreadsheettoken(crawler) + "/dimension_range"
- headers = {
- "Authorization": "Bearer " + cls.get_token(),
- "Content-Type": "application/json; charset=utf-8"
- }
- body = {
- "dimension": {
- "sheetId": sheetid,
- "majorDimension": major_dimension,
- "startIndex": startindex,
- "endIndex": endindex
- }
- }
- try:
- urllib3.disable_warnings()
- r = requests.delete(url=dimension_range_url, headers=headers, json=body, proxies=proxies, verify=False)
- Common.logger().info("删除视频数据:{}", r.json()["msg"])
- except Exception as e:
- Common.logger().error("删除视频数据异常:{}", e)
- # 查找单元格
- @classmethod
- def find_cell(cls, crawler, sheetid, find_text):
- """
- 查找单元格
- :param crawler: 哪个爬虫
- :param sheetid: 哪张表
- # :param ranges: 单元格范围
- :param find_text: 查找的字符
- :return: 返回单元格索引
- """
- find_cell_url = "https://open.feishu.cn/open-apis/sheets/v3/spreadsheets/" \
- + cls.spreadsheettoken(crawler) + "/sheets/" \
- + sheetid + "/find"
- headers = {
- "Authorization": "Bearer " + cls.get_token(),
- "Content-Type": "application/json; charset=utf-8"
- }
- rows_count = len(cls.get_values_batch("twitter", "db114c"))
- body = {
- "find_condition": {
- "range": sheetid + "!A1:A" + str(rows_count),
- "match_case": True, # 是否忽略大小写
- "match_entire_cell": False, # 是否匹配整个单元格
- "search_by_regex": False, # 是否为正则匹配
- "include_formulas": False # 是否搜索公式内容
- },
- "find": find_text # 搜索内容
- }
- try:
- urllib3.disable_warnings()
- r = requests.post(url=find_cell_url, headers=headers, json=body, proxies=proxies, verify=False)
- Common.logger().info("查找单元格:{}", r.json()["msg"])
- matched_cell = r.json()["data"]["find_result"]["matched_cells"][0].split("A")[-1]
- return matched_cell
- except Exception as e:
- Common.logger().error("查找单元格异常:{}", e)
- # 筛选:filter
- @classmethod
- def filter_created_at(cls):
- filter_created_at_url = "https://open.feishu.cn/open-apis/sheets/v3/spreadsheets/" \
- "shtcn8fFzDhCFHpB6vzf51s2xbf/sheets/48cfb0/filter"
- headers = {
- "Authorization": "Bearer " + cls.get_token(),
- "Content-Type": "application/json; charset=utf-8"
- }
- body = {
- "col": "A",
- "condition": {
- "filter_type": "number",
- "compare_type": "less",
- "expected": [
- "6"
- ]
- }
- }
- try:
- urllib3.disable_warnings()
- r = requests.put(url=filter_created_at_url, headers=headers, json=body, proxies=proxies, verify=False)
- print(r.json())
- except Exception as e:
- Common.logger().error("查找单元格异常:{}", e)
- class Bitable:
- """
- 多维表格 API
- 文档地址:https://w42nne6hzg.feishu.cn/base/bascnpAYvIA0B1hBtNJlriZceUV?table=tblqMbXrpqFbDLNE&view=vewsMtek0O
- app_token:bascnpAYvIA0B1hBtNJlriZceUV
- """
- app_token = "bascnpAYvIA0B1hBtNJlriZceUV"
- table_id = "tblqMbXrpqFbDLNE"
- page_token = "" # 列出记录时,翻页参数
- # 获取飞书api token
- @classmethod
- def tenant_access_token(cls):
- """
- 获取飞书api token
- :return:
- """
- time.sleep(1)
- url = "https://open.feishu.cn/open-apis/auth/v3/tenant_access_token/internal/"
- post_data = {"app_id": "cli_a13ad2afa438d00b", # 这里账号密码是发布应用的后台账号及密码
- "app_secret": "4tK9LY9VbiQlY5umhE42dclBFo6t4p5O"}
- try:
- urllib3.disable_warnings()
- response = requests.post(url=url, data=post_data, proxies=proxies, verify=False)
- tenant_access_token = response.json()["tenant_access_token"]
- return tenant_access_token
- except Exception as e:
- Common.logger().error("获取tenant_access_token异常:{}", e)
- # 获取多维表格元数据
- @classmethod
- def get_apps(cls):
- """
- 获取多维表格元数据
- 该接口支持调用频率上限为 20 QPS
- https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/reference/bitable-v1/app/get
- """
- url = "https://open.feishu.cn/open-apis/bitable/v1/apps/" + cls.app_token
- headers = {
- "Authorization": "Bearer " + cls.tenant_access_token(),
- "Content-Type": "application/json; charset=utf-8"
- }
- try:
- urllib3.disable_warnings()
- r = requests.get(url=url, headers=headers, proxies=proxies, verify=False)
- Common.logger().info("获取多维表格元数据,code:{},msg:{}", r.json()["code"], r.json()["msg"])
- except Exception as e:
- Common.logger().error("获取多维表格元数据异常:{}", e)
- # 列出数据表
- @classmethod
- def get_tables(cls):
- """
- 列出数据表
- 该接口支持调用频率上限为 20 QPS
- https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/reference/bitable-v1/app-table/list
- """
- url = "https://open.feishu.cn/open-apis/bitable/v1/apps/" + cls.app_token + "/tables"
- headers = {
- "Authorization": "Bearer " + cls.tenant_access_token(),
- "Content-Type": "application/json; charset=utf-8"
- }
- params = {
- "page_token": "",
- "page_size": ""
- }
- try:
- urllib3.disable_warnings()
- r = requests.get(url=url, headers=headers, params=params, proxies=proxies, verify=False)
- Common.logger().info("列出数据表,code:{},msg:{}", r.json()["code"], r.json()["msg"])
- except Exception as e:
- Common.logger().error("列出数据表异常:{}", e)
- # 列出记录
- @classmethod
- def list_records(cls, count):
- """
- 该接口用于列出数据表中的现有记录,单次最多列出 100 行记录,支持分页获取。
- 该接口支持调用频率上限为 1000 次/分钟
- https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/reference/bitable-v1/app-table-record/list
- """
- url = "https://open.feishu.cn/open-apis/bitable/v1/apps/" \
- + cls.app_token + "/tables/" + cls.table_id + "/records"
- headers = {
- "Authorization": "Bearer " + cls.tenant_access_token(),
- "Content-Type": "application/json; charset=utf-8"
- }
- params = {
- "view_id": "", # 视图 id; 注意:
- # 如 filter 或 sort 有值,view_id 会被忽略。
- # 示例值: "vewqhz51lk"
- "filter": "", # 筛选参数; 注意:
- # 1.筛选记录的表达式不超过2000个字符。
- # 2.不支持对“人员”以及“关联字段”的属性进行过滤筛选,如人员的 OpenID。
- # 3.仅支持字段在页面展示字符值进行筛选。
- # 详细参考:https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/reference/bitable-v1/filter
- # 示例值:"示例表达式:AND(CurrentValue.[身高]>180, CurrentValue.[体重]>150)"
- "sort": "", # 排序参数。注意:
- # 1.表达式需要不超过1000字符。
- # 2.不支持对带“公式”和“关联字段”的表的使用。
- # 示例值:"["字段1 DESC","字段2 ASC"]
- # 注意:使用引号将字段名称和顺序逆序连接起来。"
- "field_names": "[]", # 字段名称。示例值:"["字段1"]"
- "text_field_as_array": True, # 控制多行文本字段数据的返回格式,true 表示以数组形式返回。注意:
- # 1.多行文本中如果有超链接部分,则会返回链接的 URL。
- # 2.目前可以返回多行文本中 URL 类型为多维表格链接、飞书 doc、飞书 sheet的URL类型以及@人员的数据结构。
- # 示例值:true
- # "user_id_type": "", # 用户 ID 类型
- # 示例值:"open_id"
- # 可选值有:
- # open_id:用户的 open id
- # union_id:用户的 union id
- # user_id:用户的 user id
- # 默认值:open_id
- "display_formula_ref": "", # 控制公式、查找引用是否显示完整的原样返回结果。示例值:true
- "automatic_fields": "", # 控制是否返回自动计算的字段
- # 例如 created_by/created_time/last_modified_by/last_modified_time,true 表示返回
- # 示例值:true
- "page_token": "", # 分页标记
- # 第一次请求不填,表示从头开始遍历;
- # 分页查询结果还有更多项时会同时返回新的 page_token
- # 下次遍历可采用该 page_token 获取查询结果
- # 示例值:"recn0hoyXL"
- "page_size": count # 分页大小。示例值:10。数据校验规则:最大值 100
- }
- try:
- urllib3.disable_warnings()
- r = requests.get(url=url, headers=headers, params=params, proxies=proxies, verify=False)
- cls.page_token = r.json()["data"]["page_token"]
- items = r.json()["data"]["items"]
- for item in items:
- print(item)
- Common.logger().info("列出记录,code:{},msg:{}", r.json()["code"], r.json()["msg"])
- except Exception as e:
- Common.logger().error("列出记录异常:{}", e)
- # 检索记录
- @classmethod
- def search_records(cls, record_id):
- """
- 该接口用于根据 record_id 的值检索现有记录
- 该接口支持调用频率上限为 20 QPS
- https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/reference/bitable-v1/app-table-record/get
- """
- url = "https://open.feishu.cn/open-apis/bitable/v1/apps/" \
- + cls.app_token + "/tables/" + cls.table_id + "/records/" + record_id
- headers = {
- "Authorization": "Bearer " + cls.tenant_access_token(),
- "Content-Type": "application/json; charset=utf-8"
- }
- params = {
- "text_field_as_array": True, # 控制多行文本字段数据的返回格式, true 表示以数组形式返回。示例值:true
- # "user_id_type": "", # 用户 ID 类型
- # 示例值:"open_id"
- # 可选值有:
- # open_id:用户的 open id
- # union_id:用户的 union id
- # user_id:用户的 user id
- # 默认值:open_id
- "display_formula_ref": True, # 控制公式、查找引用是否显示完整的原样返回结果。示例值:true
- "automatic_fields": True, # 控制是否返回自动计算的字段
- # 例如 created_by/created_time/last_modified_by/last_modified_time,true 表示返回。示例值:true
- }
- try:
- urllib3.disable_warnings()
- r = requests.get(url=url, headers=headers, params=params, proxies=proxies, verify=False)
- Common.logger().info("检索记录,code:{},msg:{}", r.json()["code"], r.json()["msg"])
- except Exception as e:
- Common.logger().error("检索记录异常:{}", e)
- # 新增记录
- @classmethod
- def create_record(cls, fields):
- """
- 该接口用于在数据表中新增一条记录
- 该接口支持调用频率上限为 10 QPS
- https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/reference/bitable-v1/app-table-record/create
- """
- url = "https://open.feishu.cn/open-apis/bitable/v1/apps/" \
- + cls.app_token + "/tables/" + cls.table_id + "/records"
- headers = {
- "Authorization": "Bearer " + cls.tenant_access_token(),
- "Content-Type": "application/json; charset=utf-8"
- }
- body = fields
- try:
- urllib3.disable_warnings()
- r = requests.post(url=url, headers=headers, json=body, proxies=proxies, verify=False)
- Common.logger().info("新增记录,code:{},msg:{}", r.json()["code"], r.json()["msg"])
- except Exception as e:
- Common.logger().error("新增记录异常:{}", e)
- # 新增多条记录
- @classmethod
- def create_records(cls, records):
- """
- 该接口用于在数据表中新增多条记录
- 该接口支持调用频率上限为 10 QPS
- https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/reference/bitable-v1/app-table-record/batch_create
- """
- url = "https://open.feishu.cn/open-apis/bitable/v1/apps/" \
- + cls.app_token + "/tables/" + cls.table_id + "/records/batch_create"
- headers = {
- "Authorization": "Bearer " + cls.tenant_access_token(),
- "Content-Type": "application/json; charset=utf-8"
- }
- body = {
- "records": records
- }
- try:
- urllib3.disable_warnings()
- r = requests.post(url=url, headers=headers, json=body, proxies=proxies, verify=False)
- Common.logger().info("新增多条记录,code:{},msg:{}", r.json()["code"], r.json()["msg"])
- except Exception as e:
- Common.logger().error("新增多条记录异常:{}", e)
- # 更新记录
- @classmethod
- def update_record(cls, record_id, fields):
- """
- 该接口用于更新数据表中的一条记录
- 该接口支持调用频率上限为 10 QPS
- https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/reference/bitable-v1/app-table-record/update
- """
- url = "https://open.feishu.cn/open-apis/bitable/v1/apps/" \
- + cls.app_token + "/tables/" + cls.table_id + "/records/" + record_id
- headers = {
- "Authorization": "Bearer " + cls.tenant_access_token(),
- "Content-Type": "application/json; charset=utf-8"
- }
- body = fields
- try:
- urllib3.disable_warnings()
- r = requests.put(url=url, headers=headers, json=body, proxies=proxies, verify=False)
- Common.logger().info("更新记录,code:{},msg:{}", r.json()["code"], r.json()["msg"])
- except Exception as e:
- Common.logger().error("更新记录异常:{}", e)
- # 更新多条记录
- @classmethod
- def update_records(cls, records):
- """
- 该接口用于更新数据表中的多条记录
- 该接口支持调用频率上限为 10 QPS
- https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/reference/bitable-v1/app-table-record/batch_update
- """
- url = "https://open.feishu.cn/open-apis/bitable/v1/apps/" \
- + cls.app_token + "/tables/" + cls.table_id + "/records/batch_update"
- headers = {
- "Authorization": "Bearer " + cls.tenant_access_token(),
- "Content-Type": "application/json; charset=utf-8"
- }
- body = records
- try:
- urllib3.disable_warnings()
- r = requests.post(url=url, headers=headers, json=body, proxies=proxies, verify=False)
- Common.logger().info("更新多条记录,code:{},msg:{}", r.json()["code"], r.json()["msg"])
- except Exception as e:
- Common.logger().error("更新多条记录异常:{}", e)
- # 删除记录
- @classmethod
- def del_record(cls, record_id):
- """
- 该接口用于删除数据表中的一条记录
- 该接口支持调用频率上限为 10 QPS
- https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/reference/bitable-v1/app-table-record/delete
- """
- url = "https://open.feishu.cn/open-apis/bitable/v1/apps/" \
- + cls.app_token + "/tables/" + cls.table_id + "/records/" + record_id
- headers = {
- "Authorization": "Bearer " + cls.tenant_access_token(),
- "Content-Type": "application/json; charset=utf-8"
- }
- try:
- urllib3.disable_warnings()
- r = requests.delete(url=url, headers=headers, proxies=proxies, verify=False)
- Common.logger().info("删除记录,code:{},msg:{}", r.json()["code"], r.json()["msg"])
- except Exception as e:
- Common.logger().error("删除记录异常:{}", e)
- # 删除多条记录
- @classmethod
- def del_records(cls, record_ids):
- """
- 该接口用于删除数据表中现有的多条记录
- 该接口支持调用频率上限为 10 QPS
- https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/reference/bitable-v1/app-table-record/batch_delete
- """
- url = "https://open.feishu.cn/open-apis/bitable/v1/apps/" \
- + cls.app_token + "/tables/" + cls.table_id + "/records/batch_delete"
- headers = {
- "Authorization": "Bearer " + cls.tenant_access_token(),
- "Content-Type": "application/json; charset=utf-8"
- }
- body = {
- "records": record_ids # 删除的多条记录id列表。示例值:["recIcJBbvC","recvmiCORa"]
- }
- try:
- urllib3.disable_warnings()
- r = requests.post(url=url, headers=headers, json=body, proxies=proxies, verify=False)
- Common.logger().info("删除多条记录,code:{},msg:{}", r.json()["code"], r.json()["msg"])
- except Exception as e:
- Common.logger().error("删除多条记录异常:{}", e)
- if __name__ == "__main__":
- # feishu = Feishu()
- # print(feishu.get_bitable_token())
- 'reck6nLiZV'
- 'recHcfJZnG'
- 'recxdSMhzE'
- # 实例化多维表格
- bitable = Bitable()
- # # 获取多维表格元数据
- # bitable.get_apps()
- #
- # # 列出数据表
- # bitable.get_tables()
- #
- # # 列出记录
- # bitable.list_records(3)
- #
- # # 检索记录
- # bitable.search_records("recHcfJZnG")
- # # 新增一条记录
- # create_value = {
- # "fields": {
- # "uid": "0000000000",
- # "key_words": "0000000000",
- # "name": "功能开发🥕",
- # "screen_name": "功能开发🥕",
- # "person_url": {
- # "link": "https://bytedance.feishu.cn/drive/home/", "text": "https://bytedance.feishu.cn/drive/home/"
- # },
- # "description": "功能开发🥕",
- # "location": "null",
- # "friends_count": 9999999999,
- # "followers_count": 9999999999,
- # "favourites_count": 9999999999,
- # "listed_count": 9999999999,
- # "statuses_count": 9999999999,
- # "media_count": 9999999999,
- # "display_url": {
- # "link": "https://bytedance.feishu.cn/drive/home/",
- # "text": "https://bytedance.feishu.cn/drive/home/"
- # },
- # "created_at": 1656053209000,
- # "profile_image_url": {
- # "link": "https://bytedance.feishu.cn/drive/home/",
- # "text": "https://bytedance.feishu.cn/drive/home/"
- # },
- # "profile_banner_url": {
- # "link": "null",
- # "text": "null"
- # },
- # "ext_has_nft_avatar": "False",
- # "verified": "False",
- # "记录创建时间": 1656053209000,
- # # "记录修改时间": ""
- # }
- # }
- # bitable.create_record(create_value)
- # 新增多条记录
- # create_values = {
- # "fields": {
- # "uid": "0000000000",
- # "key_words": "0000000000",
- # "name": "功能开发🥕",
- # "screen_name": "功能开发🥕",
- # "person_url": {
- # "link": "https://bytedance.feishu.cn/drive/home/", "text": "https://bytedance.feishu.cn/drive/home/"
- # },
- # "description": "功能开发🥕",
- # "location": "null",
- # "friends_count": 9999999999,
- # "followers_count": 9999999999,
- # "favourites_count": 9999999999,
- # "listed_count": 9999999999,
- # "statuses_count": 9999999999,
- # "media_count": 9999999999,
- # "display_url": {
- # "link": "https://bytedance.feishu.cn/drive/home/",
- # "text": "https://bytedance.feishu.cn/drive/home/"
- # },
- # "created_at": 1656053209000,
- # "profile_image_url": {
- # "link": "https://bytedance.feishu.cn/drive/home/",
- # "text": "https://bytedance.feishu.cn/drive/home/"
- # },
- # "profile_banner_url": {
- # "link": "null",
- # "text": "null"
- # },
- # "ext_has_nft_avatar": "False",
- # "verified": "False",
- # "记录创建时间": 1656053209000,
- # # "记录修改时间": ""
- # }
- # }
- # values_list = [create_values, create_values]
- # bitable.create_records(values_list)
- # # 更新一条记录
- # use_record_id = "recxdSMhzE"
- # use_fields = {
- # "fields": {
- # "uid": "1111111111",
- # "key_words": "1111111111",
- # "name": "功能开发🥕",
- # "screen_name": "功能开发🥕",
- # "person_url": {
- # "link": "https://bytedance.feishu.cn/drive/home/", "text": "https://bytedance.feishu.cn/drive/home/"
- # },
- # "description": "功能开发🥕",
- # "location": "null",
- # "friends_count": 9999999999,
- # "followers_count": 9999999999,
- # "favourites_count": 9999999999,
- # "listed_count": 9999999999,
- # "statuses_count": 9999999999,
- # "media_count": 9999999999,
- # "display_url": {
- # "link": "https://bytedance.feishu.cn/drive/home/",
- # "text": "https://bytedance.feishu.cn/drive/home/"
- # },
- # "created_at": 1656053209000,
- # "profile_image_url": {
- # "link": "https://bytedance.feishu.cn/drive/home/",
- # "text": "https://bytedance.feishu.cn/drive/home/"
- # },
- # "profile_banner_url": {
- # "link": "null",
- # "text": "null"
- # },
- # "ext_has_nft_avatar": "False",
- # "verified": "False",
- # "记录创建时间": 1656053209000,
- # # "记录修改时间": ""
- # }
- # }
- # bitable.update_record(use_record_id, use_fields)
- # # 更新多条记录
- # "recxdSMhzE"
- # "recHcfJZnG"
- # use_records = {
- # "records": [
- # {
- # "record_id": "recxdSMhzE",
- # "fields": {
- # "uid": "3333333333",
- # "key_words": "3333333333",
- # "name": "功能开发🥕",
- # "screen_name": "功能开发🥕",
- # "person_url": {
- # "link": "https://bytedance.feishu.cn/drive/home/",
- # "text": "https://bytedance.feishu.cn/drive/home/"
- # },
- # "description": "功能开发🥕",
- # "location": "null",
- # "friends_count": 9999999999,
- # "followers_count": 9999999999,
- # "favourites_count": 9999999999,
- # "listed_count": 9999999999,
- # "statuses_count": 9999999999,
- # "media_count": 9999999999,
- # "display_url": {
- # "link": "https://bytedance.feishu.cn/drive/home/",
- # "text": "https://bytedance.feishu.cn/drive/home/"
- # },
- # "created_at": 1656053209000,
- # "profile_image_url": {
- # "link": "https://bytedance.feishu.cn/drive/home/",
- # "text": "https://bytedance.feishu.cn/drive/home/"
- # },
- # "profile_banner_url": {
- # "link": "null",
- # "text": "null"
- # },
- # "ext_has_nft_avatar": "False",
- # "verified": "False",
- # "记录创建时间": 1656053209000,
- # # "记录修改时间": ""
- # }
- # },
- # {
- # "record_id": "recHcfJZnG",
- # "fields": {
- # "uid": "3333333333",
- # "key_words": "3333333333",
- # "name": "功能开发🥕",
- # "screen_name": "功能开发🥕",
- # "person_url": {
- # "link": "https://bytedance.feishu.cn/drive/home/",
- # "text": "https://bytedance.feishu.cn/drive/home/"
- # },
- # "description": "功能开发🥕",
- # "location": "null",
- # "friends_count": 9999999999,
- # "followers_count": 9999999999,
- # "favourites_count": 9999999999,
- # "listed_count": 9999999999,
- # "statuses_count": 9999999999,
- # "media_count": 9999999999,
- # "display_url": {
- # "link": "https://bytedance.feishu.cn/drive/home/",
- # "text": "https://bytedance.feishu.cn/drive/home/"
- # },
- # "created_at": 1656053209000,
- # "profile_image_url": {
- # "link": "https://bytedance.feishu.cn/drive/home/",
- # "text": "https://bytedance.feishu.cn/drive/home/"
- # },
- # "profile_banner_url": {
- # "link": "null",
- # "text": "null"
- # },
- # "ext_has_nft_avatar": "False",
- # "verified": "False",
- # "记录创建时间": 1656053209000,
- # # "记录修改时间": ""
- # }
- # }
- # ]
- # }
- # bitable.update_records(use_records)
- # # 删除一条记录
- # bitable.del_record("reck6nLiZV")
- # # 删除多条记录
- # bitable.del_records(['recHcfJZnG', 'recxdSMhzE'])
|