Python下载GitHub数据

2023-05-16

配置文件

[Source]
Source_path = /Users/xxx/PycharmProjects/Source/list
Source_path_original = /Users/xxx/PycharmProjects/Source/original
Source_path_sg = /Users/xxx/PycharmProjects/Source/sgmodule
Source_backup_dir = /Users/xxx/PycharmProjects/Source/backup

下载模块

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2022/11/6 09:22
# @Author  : lumia98
# @File    : DowanloadUrls
# @Software: PyCharm
# 下载url数据
import os,sys
import ssl
import time
import urllib.request

# 下载模块
def downloanUrl(urlPath, filePath, mPath):
    """
    urlPath: 下载Github原始数据的url
    filePath: 存放Github数据的目录
    mPath: 其他格式后缀的目录
    """
    # 全局取消证书验证
    #ssl._create_default_https_context = ssl._create_unverified_context

    # 后缀的变量
    list_suffix = ".list"
    yaml_suffix = ".yaml"

    # 下载的url数据是字典集合
    # 判断传递url是否为字典
    if isinstance(urlPath, dict):
        # 循环字典
        for i in urlPath.items():
            # 切割字典key,方便辨识传递的数据是否带后缀
            a = str(i[0]).split(".")  # 以 . 切割
            if len(a) == 1:  # 如果切割完成,长度没有两个(不是aa.js这种)
                # print(a)
                # 默认用list后缀
                urllib.request.urlretrieve(i[1], os.path.join(filePath, i[0] + list_suffix))
                time.sleep(2)
            else:
                # key带了后缀
                urllib.request.urlretrieve(i[1], os.path.join(mPath, i[0] ))
                time.sleep(2)

            # 判断数据下载是否完整
            if len(a) == 1:
                # 计算list后缀下载的文件大小
                list_size = os.path.getsize(os.path.join(filePath, i[0] + list_suffix))

                # 转换成整数
                int_list_size = int(list_size)

                if int_list_size <= 90:  # 如果小于90字节,则退出
                    print("{} 数据下载过小,请查看文件是否完整.....".format(i[0]))
                    os.remove(os.path.join(filePath, i[0] + list_suffix))
                    break
                else:
                    print("{} 正在下载.............".format(i[0]))
            else:
                # 计算不是list后缀的文件大小(如模块这些)
                exists_path = os.path.exists(os.path.join(mPath, i[0])) # 判断文件是否存在
                #print(i[0])
                if exists_path:
                    other_size = os.path.getsize(os.path.join(mPath, i[0]))

                    int_other_size = int(other_size)

                    if int_other_size < 10:
                        print("{} 数据下载过小,请查看文件是否完整.....".format(i[0]))
                        os.remove(os.path.join(mPath, i[0]))
                        break
                    print("{} 正在下载.............".format(i[0]))
    else:
        # 如果不是字典的下载数据,则告诉对方
        print("""
                  url的格式请用字典方式传递,如:
                  test = {"xxx": "https://url/xxx.py"}
                  """)
        exit(0)

需要下载的数据模块

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2022/11/7 09:11
# @Author  : lumia98
# @File    : Source
# @Software: PyCharm
import time, os
from Functions import DowanloadUrls
import configparser

class githubObj(object):
    """
    Source 相关代码
    """

    def __init__(self):
        # 获取FilePath.conf配置路径
        RootPath = os.path.abspath(".")
        ConfPath = os.path.join(RootPath, "Functions", "FilePath.conf")
        # 读取FilePath.conf内容
        cf = configparser.ConfigParser()
        cf.read(ConfPath)
        # 拿到conf内容
        self._SourcePath = cf.get("Source", "Source_path") # finsh后的存储数据目录
        self._Source_original = cf.get("Source", "Source_path_original") # Github下载存放目录
        self._Source_sgmodule = cf.get("Source", "Source_path_sg") # github模块下载地址目录
        self._Source_backup = cf.get("Source", "Source_backup_dir") # 备份上次的数据目录

        # url地址集合
        self._urls = {
            # github地址
            "xxx": "https://raw.githubusercontent.com/xxx.py",
            "aa.js": "https://raw.githubusercontent.com/aa.js"
        }

    # 下载github数据
    def StartDownload(self):
        DowanloadUrls.downloanUrl(urlPath=self._urls,filePath=self._Source_original, mPath=self._Source_module)

启动下载

githubObj().StartDownload()

此代码只需要在githubObj类(url地址集合)里的数据增删改

如果目录则修改配置文件

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Python下载GitHub数据 的相关文章

随机推荐

  • 使用ICMP协议检测网络状态

    ICMP xff08 Internet ControlMessages Protocol xff0c 网间控制报文协议 xff09 是TCP IP协议族的子协议 xff0c 是一种面向无连接的协议 xff0c 在IP和路由器之前传递控制消息
  • c++打印enum class

    span class token keyword enum span span class token keyword class span span class token class name A span span class tok
  • 使用strace查找Emacs启动阻塞的原因(exec-path-from-shell)

    原文地址 https www lujun9972 win blog 2019 09 26 使用strace查找emacs启动阻塞的原因 exec path from shell index html 之前就觉得我的Emacs启动好慢 xff
  • 为Linux安装虚拟PDF打印机

    原文地址 https lujun9972 github io blog 2020 04 11 为linux安装虚拟pdf打印机 index html 今天发现一个 CUPS PDF 项目 可以为 CUPS Common Unix Print
  • ubuntu系统启用shell远程登陆

    Ubuntu desktop系统安装后 xff0c 想使用shell远程登陆 xff0c 会提示 Connecting to 192 168 220 133 22 Could not connect to 39 192 168 220 13
  • 枚举类(ENUM)用法总结

    对于ENUM一直是比较陌生的 xff0c 在和某酷爱ENUM的大神合作时 xff0c 才慢慢接触到ENUM的用法 1 ENUM是什么 xff1f 首先ENUM是一个类 xff0c 不像String int之类的数据结构 xff0c 更类似于
  • Python循环结构练习2

    Problem A xff1a 循环结构 输出数列2 xff0c 5 xff0c 8 xff0c 11 xff0c 14 题目描述 输入正整数n xff08 n 100 xff09 xff0c 输出数列2 xff0c 5 xff0c 8 x
  • KVM网络模型之:PCI Passthrough

    目录 PCI Passthrough技术介绍和KVM中配置 案例 内核启用 重新启动虚拟机实例 PCI Passthrough技术介绍和KVM中配置 PCI Passthrough技术是虚拟化网卡的终极解决方案 xff0c 能够让虚拟机独占
  • 微信开放公众平台,扩展自定义类,定时提醒,定时发消息

    微信开放公众平台 xff0c 扩展自定义类 xff0c 定时提醒 xff0c 定时发消息 lt php class MyapiAction extends BaseAction public function index 微医疗 预约提醒
  • Ubuntu配置iptables规则

    Ubuntu配置防火墙 xff0c 并且开机iptables自启动规则 适用于CentOS 1 登录root账号 span class token comment 切换到root账号 span super 64 super span cla
  • Linux记录用户执行命令

    span class token shebang important bin bash span span class token comment By lumia98 64 vip qq com span span class token
  • Nginx规则配置实例

    配置某个ip或者页面禁止访问及跳转方法 server listen 80 server name www test com cn location proxy redirect off proxy set header host host
  • Debian 11.2安装ssh服务

    切换到root用户 更新软件源 span class token function apt get span update 安装ssh span class token function apt get span span class to
  • Python计算文件大小

    span class token comment usr bin env python span span class token comment coding utf 8 span span class token triple quot
  • Python获取文件内的下一行数据

    span class token comment usr bin env python span span class token comment Version 61 3 8 1 span span class token comment
  • iptables配置实例

    查看当前所有规则 iptables L n 查看所有规则 iptables nL line number 显示行 iptables nvL line number 显示行 清空所有配置 iptables F iptables X iptab
  • 利用Shell脚本校验数据一致性

    span class token shebang important bin bash span span class token comment span span class token comment 检测两台服务器指定目录下的文件一
  • Debian11系统Redis源码安装

    span class token shebang important bin bash span span class token comment Debian11 Redis6 2 6安装 span span class token co
  • iOS 根据文字内容设置cell 的高度

    今天学习一个简单的 根据内容的大小设置cell 的高度 第一步 建两个类 分别继承于UIViewController 和UITableViewCell 第二步 mainViewController h import lt UIKit UIK
  • Python下载GitHub数据

    配置文件 span class token punctuation span Source span class token punctuation span Source path span class token operator 61