腾讯文档获取excel数据

import datetime
import json
import os
import sys

import time
import traceback

import redis
import requests
from dingtalk_sdk_gmdzy2010.authority_request import AccessTokenRequest
from dingtalk_sdk_gmdzy2010.message_request import WorkNoticeRequest
from selenium import webdriver
from selenium.webdriver.common.by import By


def redisCon(host: str = '127.0.0.1', port: int = 6379, db: int = 1, password: str = 'pwd'):
    return redis.Redis(host=host, port=port, db=db, password=password)


DINGTALK_APPKEY = ''
DINGTALK_SECRET = ''
DINGTALK_AGENT_ID = ''


def get_dingtalk_token():
    params = {
        "appkey": DINGTALK_APPKEY,
        "appsecret": DINGTALK_SECRET,
    }
    request = AccessTokenRequest(params=params)
    request.get_json_response()
    token = request.get_access_token()
    redisCon().set('dingtalk_token', token, 7200)
    return token



def send_img_to_dingtalk(access_token, file_path, media_type='image'):
    url = "https://oapi.dingtalk.com/media/upload"

    media_file = open(file_path, 'rb')
    files = {
        'type': (None, media_type),
        'media': (media_file.name, media_file, 'application/octet-stream')
    }

    response = requests.post(url, params={'access_token': access_token}, files=files).json()
    return response


def send_work_notice(media_id):
    try:
        if redisCon().get('dingtalk_token'):
            dingtalk_token = redisCon().get('dingtalk_token').decode('utf-8')
        else:
            dingtalk_token = get_dingtalk_token()

        params = {"access_token": dingtalk_token}
        data = {
            "agent_id": DINGTALK_AGENT_ID,
            "userid_list": 'userid123',
            "msg": {"msgtype": "image", "image": {"media_id": media_id}}
        }
        request = WorkNoticeRequest(params=params, json=data)
        request.request_method = "post"
        request.get_json_response()
        print(request.call_status)
    except Exception as e:
        print(e)


def get_tencent_cookie():
    options = webdriver.FirefoxOptions()
    options.add_argument('--headless')
    if sys.platform == 'linux':
        options.binary_location = '/usr/local/bin/firefox'
    driver = webdriver.Firefox(options=options)
    try:
        driver.get('https://docs.qq.com/sheet/sheetId')

        time.sleep(5)

        driver.find_element(By.XPATH, "//*[@id='header-login-btn']").click()

        time.sleep(2)

        filename = '{}.png'.format(time.time_ns())
        path = os.path.join('path', 'qq')
        if not os.path.exists(path):
            os.makedirs(path)
        filepath = os.path.join(path, filename)
        driver.save_screenshot(filename)
        if redisCon().get('dingtalk_token'):
            dingtalk_token = redisCon().get('dingtalk_token').decode('utf-8')
        else:
            dingtalk_token = get_dingtalk_token()
        media_id = send_img_to_dingtalk(dingtalk_token, filepath).get('media_id')
        send_work_notice(media_id)

        for i in (range(300)):
            try:
                print('num---: ', i)
                print('监控弹窗', driver.find_element(By.XPATH, "//div[@class='dui-snackbar-container login-modal']"))
                if driver.find_element(By.XPATH, "//div[@class='dui-snackbar-container login-modal']"):
                    if i in [60, 120, 180, 240, 300]:
                        driver.find_element(By.XPATH, "//div[@class='qrcode-invalid-icon-failure']").click()
                        driver.save_screenshot(filepath)
                        media_id = send_img_to_dingtalk(dingtalk_token, filepath).get('media_id')
                        send_work_notice(media_id)
                else:
                    break
                time.sleep(1)
            except Exception as e:
                # print(traceback.print_exc())
                continue
        # 等待页面加载完成
        time.sleep(30)

        # 获取所有 cookies

        cookies = driver.get_cookies()
        print(cookies)
        if 'access_token' in json.dumps(cookies):
            cookie = ';'.join(['{key}={value}'.format(key=i.get('name'), value=i.get('value')) for i in cookies])
            redisCon().set('cookie', cookie, datetime.timedelta(days=29))
            driver.quit()
            return cookie
        return False
    except Exception as e:
        print(e)
        print(traceback.print_exc())
        return False
    finally:
        # driver.quit()
        os.remove(filepath)



def get_operation_id():
    print('开始获取 getOperationId')
    if redisCon().get('cookie'):
        Cookies = redisCon().get('cookie').decode('utf-8')
    else:
        Cookies = get_tencent_cookie()
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36',
        'Cookie': Cookies
    }
    print('getOperationId 请求头', headers)
    url = 'https://docs.qq.com/v1/export/export_office'
    data = {
        "exportType": 0,
        "switches": {
            "embedFonts": False
        },
        "exportSource": "client",
        "docId": "docId",
        "version": 2
    }
    res = requests.post(url, data=data, headers=headers).json()
    print('getOperationId 响应---{}'.format(res))
    if res.get('ret') == 0:
        print('getOperationId 获取成功---{}'.format(res.get('operationId')))
        return res.get('operationId')
    print('getOperationId 获取失败---{}'.format(res))
    if res.get('ret') == 403:
        get_tencent_cookie()
        return get_operation_id()
    return False


def get_download_uri(operationId):
    print('获取下载url接口参数---{}'.format(operationId))
    if redisCon().get('cookie'):
        Cookies = redisCon().get('cookie').decode('utf-8')
    else:
        Cookies = get_tencent_cookie()
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36',
        'Cookie': Cookies
    }
    url = 'https://docs.qq.com/v1/export/query_progress'
    p = {
        'operationId': operationId
    }
    res = requests.get(url, params=p, headers=headers).json()
    print('获取下载url接口响应---{}'.format(res))

    if res.get('file_url'):
        return res.get('file_url')

    time.sleep(1)
    return get_download_uri(operationId)


def save_file():
    url = get_download_uri(get_operation_id())
    res = requests.get(url)
    t = str(time.time_ns())

    filename = '{}.xlsx'.format(t)
    path = os.path.join('path', 'qq')
    if not os.path.exists(path):
        os.makedirs(path)
    filepath = os.path.join(path, filename)
    with open(filepath, 'wb') as f:
        f.write(res.content)
    return filepath
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Wufucks

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值