Browse Source

输入0,下载所有

fish 7 months ago
parent
commit
e1fbbe1753
4 changed files with 57 additions and 20 deletions
  1. 2 0
      .gitignore
  2. 2 0
      README.md
  3. 0 1
      crawl_hero/__init__.py
  4. 53 19
      crawl_hero/crawl_hero.py

+ 2 - 0
.gitignore

@@ -1,3 +1,5 @@
 /build/getHero
 /__pycache__
 *.pyc
+data/
+.jpg

+ 2 - 0
README.md

@@ -21,6 +21,8 @@
 virtualenv .venv 
 
 pip install -r requirements.txt
+python main.py
+zip -r data.zip data
 
 pip install pyinstaller
 pyinstaller -F -c  getHero.py

+ 0 - 1
crawl_hero/__init__.py

@@ -6,7 +6,6 @@
 @License :   Copyright © 2017-2022 liuyuqi. All Rights Reserved.
 @Desc    :   main function
 '''
-import time
 from crawl_hero.crawl_hero import CrawlHero
 
 

+ 53 - 19
crawl_hero/crawl_hero.py

@@ -1,3 +1,5 @@
+import random
+from time import sleep
 import requests
 import os
 from crawl_hero import api
@@ -38,30 +40,62 @@ class CrawlHero():
 
     def getHeroPic(self):
         while(1):
-            mHeroId = input("用户输入所需要的英雄id:")
-        # 获取所有英雄图片的地址
-            for hero_id in self.hero_id_list:
-                if mHeroId == hero_id:
-                    hero_img_url = api.hero_img_url % (hero_id)
-                    skin_list = self.s.get(hero_img_url).json()["skins"]
-                    for skin in skin_list:
-                        mainImg = skin["mainImg"]
-                        heroName = skin["heroName"]
-                        name = skin["name"]
-                        if not os.path.exists(heroName):
-                            os.mkdir(heroName)
-                        if mainImg == "":
-                            with open(heroName + "/" + name + ".jpg", "wb") as file:
-                                file.write(self.s.get(
-                                    skin["chromaImg"]).content)
-                        else:
-                            with open(heroName + "/" + name + ".jpg", "wb") as file:
-                                file.write(self.s.get(skin["mainImg"]).content)
+            mHeroId = input("用户输入所需要的英雄id(0代表所有):")
+            if mHeroId=="0":
+                for hero_id in self.hero_id_list:
+                    self.downPic(hero_id)
+            else:
+                # 获取所有英雄图片的地址
+                for hero_id in self.hero_id_list:
+                    if mHeroId == hero_id:
+                        self.downPic(hero_id)
             print("下载结束,头像保存至当前目录\r\n\r\n")
             yn = input("y 继续    n 输出程序结束:")
             if yn == "n":
                 exit()
 
+    def downPic(self, hero_id):
+        print("开始下载英雄id为%s的皮肤" % hero_id)
+        hero_img_url = api.hero_img_url % (hero_id)
+        skin_list = self.s.get(hero_img_url).json()["skins"]
+        for skin in skin_list:
+            sleep(random.uniform(0.1, 2))
+            mainImg = skin["mainImg"]
+            heroName = skin["heroName"]
+            name = skin["name"]
+            name = name.replace("/", "").replace("\\", "")
+            dataDir= "./data"
+            if not os.path.exists(os.path.join(dataDir,heroName)):
+                os.makedirs(os.path.join(dataDir,heroName))
+            try:
+                fileName = os.path.join(dataDir, heroName, name + ".jpg")
+                if skin["chromaImg"] != "" or skin["mainImg"] != "":
+                    if mainImg == "":
+                        with open(fileName, "wb") as file:
+                            file.write(self.s.get(
+                                                skin["chromaImg"]).content)
+                        print("已下载英雄id为 %s 名称为 %s 的皮肤 %s, url: %s" % (hero_id, heroName, name, skin["chromaImg"]))
+                    else:
+                        with open(fileName, "wb") as file:
+                            file.write(self.s.get(skin["mainImg"]).content)
+                        print("已下载英雄id为 %s 名称为 %s 的皮肤 %s, url: %s" % (hero_id, heroName, name, skin["mainImg"]))
+                else:
+                    print("未找到英雄id为 %s 名称为 %s 的皮肤 %s 的图片" % (hero_id, heroName, name))
+            except Exception as e:
+                print(e)
+
     def getHero(self):
         self.getHeroId()
         self.getHeroPic()
+        self.clean()
+
+    def clean(self):
+        """ 清理文件大小小于50kb的 图片 """
+        dataDir = "./data"
+        for root, dirs, files in os.walk(dataDir):
+            for dir in dirs:
+                path = os.path.join(root, dir)
+                for name in os.listdir(path):
+                    if os.path.getsize(os.path.join(path, name)) < 50*1024:
+                        os.remove(os.path.join(path, name))
+                        print("删除文件%s" % name)