csdn图床批量转腾讯图床

以前写的blog用的csdn的图床,这两天突然不能用了,写个脚本批量把csdn的图片传到腾讯的图床上

from qcloud_cos import CosConfig
from qcloud_cos import CosS3Client
import sys
import os
import logging
import requests
import re

#logging.basicConfig(level=logging.INFO, stream=sys.stdout)

secret_id = ''     # 用户的 SecretId,子账号密钥获取可参见 https://cloud.tencent.com/document/product/598/37140
secret_key = ''   # 用户的 Secret_Key
region = 'ap-nanjing'      # 替换为用户的 region
token = None               # 如果使用永久密钥不需要填入 token,如果使用临时密钥需要填入,临时密钥生成和使用指引参见 https://cloud.tencent.com/document/product/436/14048
scheme = 'https'           # 指定使用 http/https 协议来访问 COS,默认为 https,可不填

config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token, Scheme=scheme)
client = CosS3Client(config)

def upload(has_img_convert,file,fenlei):
    if has_img_convert == 1:
        url = 'https://img-blog.csdnimg.cn/img_convert/' + str(file) + '.png'
    else:
        url = 'https://img-blog.csdnimg.cn/' + str(file) + '.png'
    stream = requests.get(url)
    response = client.put_object(
        Bucket='picgo-xxxxx',
        Body=stream,
        Key='/{0}/{1}.png'.format(fenlei,file)
    )

def run(has_img_convert,target,fenlei):
    if has_img_convert == 1:
        pattern = r'img_convert/(.*?)\.png'
    else:
        pattern = r'img-blog.csdnimg.cn/(.*?)\.png'
    for i in target:
        with open(i,'r',encoding="utf-8") as file:
            content = file.read()
            match = re.findall(pattern, content)
            for j in match:
                upload(has_img_convert,j,fenlei)
            print("[*]上传完成")
        if has_img_convert == 1:
            content = content.replace('https://img-blog.csdnimg.cn/img_convert/','https://picgo-xxx.cos.ap-nanjing.myqcloud.com/{0}/'.format(fenlei))
        else:
            content = content.replace('https://img-blog.csdnimg.cn/','https://picgo-xxx.cos.ap-nanjing.myqcloud.com/{0}/'.format(fenlei))
        with open(i, 'w', encoding="utf-8") as file:
            file.write(content)
    print("[*]完成")

if __name__ == '__main__':
    fenlei = 'pentest'  # 文章分类
    target = ['shiro.md']
    target2 = ['渗透.md']
    # has_img_convert = 1  # 比较老一点的文章图片url有:img_convert
    #run(1,target,fenlei)
    run(0, target2, fenlei)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值