1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
| """ @author: pakro @filename: v2ray_better_geo.py @time: 2020/2/10 0010 11:33 """ import os import sys import time import getopt import requests
geoip = "https://github.com/Loyalsoldier/v2ray-rules-dat/releases/latest/download/geoip.dat" geosite = "https://github.com/Loyalsoldier/v2ray-rules-dat/releases/latest/download/geosite.dat"
def download_music(download_url, file_name, proxies=None, destination_folder="./"): """ 读取下载队列进行下载 """ if destination_folder[-1] != "/": destination_folder += "/" file_name = destination_folder + file_name if os.path.isfile(file_name): try: os.remove(file_name) except Exception as e: print("File delete failed! please check! ", e) time.sleep(3) sys.exit(0)
print("Start download: ", file_name) try: r = requests.get(download_url, proxies=proxies, timeout=5) except AttributeError: print("wrong format of proxies!, please check!") sys.exit(0) except requests.exceptions.ProxyError: print("timeout! please check your internet connection!") sys.exit(0) try: with open(file_name, "wb") as f: f.write(r.content) print(f"Download {file_name} success!") except Exception as e: print("SAVE FILE FAILED!: ", e)
if __name__ == '__main__': argv = sys.argv[1:] try: opts, args = getopt.getopt(argv, "hxp:", ["path="]) except getopt.GetoptError: print("不知道出了什么问题") sys.exit(2)
path = "./" proxy = None for opt, arg in opts: if opt in "-h": print("-p or --path to specify the path \nFor example: python v2ray_better_geo.py -p /path/to/dir/\n") print("-x to specify the proxies, support socks and http,\n" "format: \n{\n'http': 'http://192.168.199.x:1081/',\n'https': “http://192.168.199.x:1081/”,\n}\n" 'For example: python v2ray_better_geo.py -x {"http":"http://127.0.0.1:1081"}') sys.exit(0) if opt in "x": proxy = arg if opt in ("-p", "--path"): path = arg
download_music(geoip, "geoip.dat", proxies=proxy, destination_folder=path) download_music(geosite, "geosite.dat", proxies=proxy, destination_folder=path)
|