chendeben 7 rokov pred
commit
b6725c76ef

+ 9 - 0
.idea/Download.iml

@@ -0,0 +1,9 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<module type="WEB_MODULE" version="4">
+  <component name="Go" enabled="true" />
+  <component name="NewModuleRootManager">
+    <content url="file://$MODULE_DIR$" />
+    <orderEntry type="inheritedJdk" />
+    <orderEntry type="sourceFolder" forTests="false" />
+  </component>
+</module>

+ 3 - 0
.idea/dictionaries/Heanup.xml

@@ -0,0 +1,3 @@
+<component name="ProjectDictionaryState">
+  <dictionary name="Heanup" />
+</component>

+ 6 - 0
.idea/encodings.xml

@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="Encoding">
+    <file url="PROJECT" charset="UTF-8" />
+  </component>
+</project>

+ 6 - 0
.idea/misc.xml

@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="JavaScriptSettings">
+    <option name="languageLevel" value="ES6" />
+  </component>
+</project>

+ 8 - 0
.idea/modules.xml

@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="ProjectModuleManager">
+    <modules>
+      <module fileurl="file://$PROJECT_DIR$/.idea/Download.iml" filepath="$PROJECT_DIR$/.idea/Download.iml" />
+    </modules>
+  </component>
+</project>

+ 6 - 0
.idea/vcs.xml

@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="VcsDirectoryMappings">
+    <mapping directory="$PROJECT_DIR$" vcs="Git" />
+  </component>
+</project>

+ 260 - 0
.idea/workspace.xml

@@ -0,0 +1,260 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="ChangeListManager">
+    <list default="true" id="ceb00765-dd07-4348-a4b0-6d376c2e3641" name="Default Changelist" comment="">
+      <change afterPath="$PROJECT_DIR$/.idea/Download.iml" afterDir="false" />
+      <change afterPath="$PROJECT_DIR$/.idea/dictionaries/Heanup.xml" afterDir="false" />
+      <change afterPath="$PROJECT_DIR$/.idea/encodings.xml" afterDir="false" />
+      <change afterPath="$PROJECT_DIR$/.idea/misc.xml" afterDir="false" />
+      <change afterPath="$PROJECT_DIR$/.idea/modules.xml" afterDir="false" />
+      <change afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
+      <change afterPath="$PROJECT_DIR$/down_bang.go" afterDir="false" />
+      <change afterPath="$PROJECT_DIR$/main.go" afterDir="false" />
+    </list>
+    <option name="EXCLUDED_CONVERTED_TO_IGNORED" value="true" />
+    <option name="SHOW_DIALOG" value="false" />
+    <option name="HIGHLIGHT_CONFLICTS" value="true" />
+    <option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
+    <option name="LAST_RESOLUTION" value="IGNORE" />
+  </component>
+  <component name="FileEditorManager">
+    <leaf SIDE_TABS_SIZE_LIMIT_KEY="300">
+      <file pinned="false" current-in-tab="true">
+        <entry file="file://$PROJECT_DIR$/main.go">
+          <provider selected="true" editor-type-id="text-editor">
+            <state relative-caret-position="162">
+              <caret line="20" selection-start-line="20" selection-end-line="20" />
+            </state>
+          </provider>
+        </entry>
+      </file>
+      <file pinned="false" current-in-tab="false">
+        <entry file="file://$PROJECT_DIR$/down_bang.go">
+          <provider selected="true" editor-type-id="text-editor">
+            <state relative-caret-position="204">
+              <caret line="49" column="47" selection-start-line="49" selection-start-column="39" selection-end-line="49" selection-end-column="47" />
+              <folding>
+                <element signature="e#14#62#0" expanded="true" />
+              </folding>
+            </state>
+          </provider>
+        </entry>
+      </file>
+    </leaf>
+  </component>
+  <component name="FileTemplateManagerImpl">
+    <option name="RECENT_TEMPLATES">
+      <list>
+        <option value="Go File" />
+        <option value="Go Application" />
+      </list>
+    </option>
+  </component>
+  <component name="GOROOT" path="C:\Go" />
+  <component name="Git.Settings">
+    <option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" />
+  </component>
+  <component name="IdeDocumentHistory">
+    <option name="CHANGED_PATHS">
+      <list>
+        <option value="$PROJECT_DIR$/down_bang.go" />
+        <option value="$PROJECT_DIR$/main.go" />
+      </list>
+    </option>
+  </component>
+  <component name="ProjectFrameBounds" extendedState="6">
+    <option name="x" value="10" />
+    <option name="y" value="20" />
+    <option name="width" value="1346" />
+    <option name="height" value="688" />
+  </component>
+  <component name="ProjectLevelVcsManager" settingsEditedManually="true" />
+  <component name="ProjectView">
+    <navigator proportions="" version="1">
+      <foldersAlwaysOnTop value="true" />
+    </navigator>
+    <panes>
+      <pane id="Scope" />
+      <pane id="ProjectPane">
+        <subPane>
+          <expand>
+            <path>
+              <item name="Download" type="b2602c69:ProjectViewProjectNode" />
+              <item name="Download" type="462c0819:PsiDirectoryNode" />
+            </path>
+          </expand>
+          <select />
+        </subPane>
+      </pane>
+    </panes>
+  </component>
+  <component name="PropertiesComponent">
+    <property name="DefaultGoTemplateProperty" value="Go Application" />
+    <property name="SHARE_PROJECT_CONFIGURATION_FILES" value="true" />
+    <property name="WebServerToolWindowFactoryState" value="false" />
+    <property name="configurable.Global.GOPATH.is.expanded" value="true" />
+    <property name="configurable.Module.GOPATH.is.expanded" value="false" />
+    <property name="configurable.Project.GOPATH.is.expanded" value="false" />
+    <property name="go.gopath.indexing.explicitly.defined" value="true" />
+    <property name="go.import.settings.migrated" value="true" />
+    <property name="go.sdk.automatically.set" value="true" />
+    <property name="last_opened_file_path" value="$PROJECT_DIR$" />
+    <property name="settings.editor.selected.configurable" value="http.proxy" />
+  </component>
+  <component name="RunDashboard">
+    <option name="ruleStates">
+      <list>
+        <RuleState>
+          <option name="name" value="ConfigurationTypeDashboardGroupingRule" />
+        </RuleState>
+        <RuleState>
+          <option name="name" value="StatusDashboardGroupingRule" />
+        </RuleState>
+      </list>
+    </option>
+  </component>
+  <component name="RunManager" selected="Go Build.go build down_bang.go">
+    <configuration name="go build Download" type="GoApplicationRunConfiguration" factoryName="Go Application" temporary="true" nameIsGenerated="true">
+      <module name="Download" />
+      <working_directory value="$PROJECT_DIR$/" />
+      <go_parameters value="-i" />
+      <kind value="FILE" />
+      <filePath value="$PROJECT_DIR$/main.go" />
+      <directory value="$PROJECT_DIR$/" />
+      <method v="2" />
+    </configuration>
+    <configuration name="go build down_bang.go" type="GoApplicationRunConfiguration" factoryName="Go Application" temporary="true" nameIsGenerated="true">
+      <module name="Download" />
+      <working_directory value="$PROJECT_DIR$/" />
+      <kind value="FILE" />
+      <filePath value="$PROJECT_DIR$/down_bang.go" />
+      <package value="Download" />
+      <directory value="$PROJECT_DIR$/" />
+      <method v="2" />
+    </configuration>
+    <recent_temporary>
+      <list>
+        <item itemvalue="Go Build.go build down_bang.go" />
+        <item itemvalue="Go Build.go build Download" />
+      </list>
+    </recent_temporary>
+  </component>
+  <component name="ToolWindowManager">
+    <frame x="-8" y="-8" width="1382" height="744" extended-state="6" />
+    <layout>
+      <window_info active="true" content_ui="combo" id="Project" order="0" visible="true" weight="0.25416037" />
+      <window_info id="Structure" order="1" side_tool="true" weight="0.25" />
+      <window_info id="Favorites" order="2" side_tool="true" />
+      <window_info anchor="bottom" id="Message" order="0" />
+      <window_info anchor="bottom" id="Find" order="1" />
+      <window_info anchor="bottom" id="Run" order="2" weight="0.31535947" />
+      <window_info anchor="bottom" id="Debug" order="3" weight="0.39869282" />
+      <window_info anchor="bottom" id="Cvs" order="4" weight="0.25" />
+      <window_info anchor="bottom" id="Inspection" order="5" weight="0.4" />
+      <window_info anchor="bottom" id="TODO" order="6" />
+      <window_info anchor="bottom" id="Docker" order="7" show_stripe_button="false" />
+      <window_info anchor="bottom" id="Database Changes" order="8" />
+      <window_info anchor="bottom" id="Version Control" order="9" />
+      <window_info anchor="bottom" id="Terminal" order="10" weight="0.23366013" />
+      <window_info anchor="bottom" id="Event Log" order="11" side_tool="true" />
+      <window_info anchor="right" id="Commander" internal_type="SLIDING" order="0" type="SLIDING" weight="0.4" />
+      <window_info anchor="right" id="Ant Build" order="1" weight="0.25" />
+      <window_info anchor="right" content_ui="combo" id="Hierarchy" order="2" weight="0.25" />
+      <window_info anchor="right" id="Database" order="3" />
+    </layout>
+    <layout-to-restore>
+      <window_info content_ui="combo" id="Project" order="0" weight="0.25416037" />
+      <window_info id="Structure" order="1" side_tool="true" weight="0.25" />
+      <window_info id="Favorites" order="2" side_tool="true" />
+      <window_info anchor="bottom" id="Message" order="0" />
+      <window_info anchor="bottom" id="Find" order="1" />
+      <window_info active="true" anchor="bottom" id="Run" order="2" visible="true" weight="0.31535947" />
+      <window_info anchor="bottom" id="Debug" order="3" weight="0.39869282" />
+      <window_info anchor="bottom" id="Cvs" order="4" weight="0.25" />
+      <window_info anchor="bottom" id="Inspection" order="5" weight="0.4" />
+      <window_info anchor="bottom" id="TODO" order="6" />
+      <window_info anchor="bottom" id="Docker" order="7" show_stripe_button="false" />
+      <window_info anchor="bottom" id="Database Changes" order="8" />
+      <window_info anchor="bottom" id="Version Control" order="9" />
+      <window_info anchor="bottom" id="Terminal" order="10" weight="0.23366013" />
+      <window_info anchor="bottom" id="Event Log" order="11" side_tool="true" />
+      <window_info anchor="right" id="Commander" internal_type="SLIDING" order="0" type="SLIDING" weight="0.4" />
+      <window_info anchor="right" id="Ant Build" order="1" weight="0.25" />
+      <window_info anchor="right" content_ui="combo" id="Hierarchy" order="2" weight="0.25" />
+      <window_info anchor="right" id="Database" order="3" />
+    </layout-to-restore>
+  </component>
+  <component name="TypeScriptGeneratedFilesManager">
+    <option name="version" value="1" />
+  </component>
+  <component name="XDebuggerManager">
+    <breakpoint-manager>
+      <breakpoints>
+        <line-breakpoint enabled="true" type="DlvLineBreakpoint">
+          <url>file://$PROJECT_DIR$/main.go</url>
+          <line>58</line>
+          <option name="timeStamp" value="1" />
+        </line-breakpoint>
+      </breakpoints>
+    </breakpoint-manager>
+  </component>
+  <component name="editorHistoryManager">
+    <entry file="file://C:/Go/src/regexp/regexp.go">
+      <provider selected="true" editor-type-id="text-editor">
+        <state relative-caret-position="198">
+          <caret line="822" column="18" selection-start-line="822" selection-start-column="18" selection-end-line="822" selection-end-column="18" />
+        </state>
+      </provider>
+    </entry>
+    <entry file="file://C:/Go/src/path/filepath/path.go">
+      <provider selected="true" editor-type-id="text-editor">
+        <state relative-caret-position="194">
+          <caret line="60" column="4" selection-start-line="60" selection-start-column="4" selection-end-line="60" selection-end-column="4" />
+        </state>
+      </provider>
+    </entry>
+    <entry file="file://$PROJECT_DIR$/down_bang.go">
+      <provider selected="true" editor-type-id="text-editor">
+        <state relative-caret-position="204">
+          <caret line="49" column="47" selection-start-line="49" selection-start-column="39" selection-end-line="49" selection-end-column="47" />
+          <folding>
+            <element signature="e#14#62#0" expanded="true" />
+          </folding>
+        </state>
+      </provider>
+    </entry>
+    <entry file="file://$PROJECT_DIR$/main.go">
+      <provider selected="true" editor-type-id="text-editor">
+        <state relative-caret-position="162">
+          <caret line="20" selection-start-line="20" selection-end-line="20" />
+        </state>
+      </provider>
+    </entry>
+  </component>
+  <component name="masterDetails">
+    <states>
+      <state key="Copyright.UI">
+        <settings>
+          <splitter-proportions>
+            <option name="proportions">
+              <list>
+                <option value="0.2" />
+              </list>
+            </option>
+          </splitter-proportions>
+        </settings>
+      </state>
+      <state key="ScopeChooserConfigurable.UI">
+        <settings>
+          <splitter-proportions>
+            <option name="proportions">
+              <list>
+                <option value="0.2" />
+              </list>
+            </option>
+          </splitter-proportions>
+        </settings>
+      </state>
+    </states>
+  </component>
+</project>

+ 98 - 0
down_bang.go

@@ -0,0 +1,98 @@
+package main
+
+import (
+	"fmt"
+	"github.com/gocolly/colly"
+	"github.com/tidwall/gjson"
+	"io"
+	"io/ioutil"
+	"log"
+	"net/http"
+	"os"
+	"path/filepath"
+	"sync"
+	"time"
+)
+
+var (
+	downloadDestFolder = "D:\\music"
+	w                  sync.WaitGroup
+)
+
+func main() {
+	url := "http://www.kuwo.cn/bang/index"
+	c := colly.NewCollector()
+	c.OnHTML(".leftNav", func(element *colly.HTMLElement) {
+		element.ForEach("div.classify.bang > ul > li", func(i int, e *colly.HTMLElement) {
+			name := e.Attr("data-name")
+			bangid := e.Attr("data-bangid")
+			parseBangdan(name, bangid)
+		})
+	})
+	c.Visit(url)
+}
+func parseBangdan(BangName string, bangId string) {
+	fmt.Println("正在解析:", BangName, "……")
+	url := fmt.Sprintf("http://www.kuwo.cn/bang/content?name=%s&bangId=%s", BangName, bangId)
+	c := colly.NewCollector()
+	c.OnHTML(".tools", func(element *colly.HTMLElement) {
+		json := element.Attr("data-music")
+		musicId := gjson.Get(json, "id")
+		musicName := gjson.Get(json, "name")
+		//artist := gjson.Get(json, "artist")
+		//fmt.Println(json)
+		client := http.Client{Timeout: 900 * time.Second}
+		url := fmt.Sprintf("http://antiserver.kuwo.cn/anti.s?format=mp3&type=convert_url&rid=%s&response=url", musicId)
+		resp, _ := client.Get(url)
+		body, _ := ioutil.ReadAll(resp.Body)
+		resp.Body.Close()
+		w.Add(1)
+		go downloadGedan(string(body), BangName, fmt.Sprintf("%s.mp3", musicName))
+		//time.Sleep(time.Duration(200)*time.Millisecond)
+	})
+	c.Visit(url)
+	w.Wait()
+}
+func downloadGedan(url string, BangName string, filename string) {
+	defer w.Done()
+	nt := time.Now().Format("2006-01-02 15:04:05")
+	_ = os.MkdirAll(downloadDestFolder+string(filepath.Separator)+BangName, 0777)
+	fpath := downloadDestFolder + string(filepath.Separator) + BangName + string(filepath.Separator) + filename
+	if PathExists(fpath) {
+		fmt.Printf("[%s]已下载完成,跳过 %s\n", nt, filename)
+		return
+	} else {
+		os.Remove(fpath + ".tmp")
+	}
+	fmt.Printf("[%s]正在下载 %s\n", nt, filename)
+	newFile, err := os.Create(fpath + ".tmp")
+	if err != nil {
+		fmt.Println(err.Error())
+	}
+
+	client := http.Client{Timeout: 900 * time.Second}
+	resp, err := client.Get(url)
+	defer resp.Body.Close()
+
+	_, err = io.Copy(newFile, resp.Body)
+	if err != nil {
+		fmt.Println(err.Error())
+	}
+	newFile.Close()
+	err = os.Rename(fpath+".tmp", fpath)
+	if err != nil {
+		fmt.Println(err)
+		//panic(err)
+	}
+	log.Println("下载完成:", filename)
+}
+func PathExists(path string) bool {
+	_, err := os.Stat(path)
+	if err == nil {
+		return true
+	}
+	if os.IsNotExist(err) {
+		return false
+	}
+	return false
+}

+ 79 - 0
main.go

@@ -0,0 +1,79 @@
+package main
+
+import (
+	"flag"
+	"fmt"
+	"github.com/gocolly/colly"
+	"github.com/tidwall/gjson"
+	"io"
+	"io/ioutil"
+	"log"
+	"net/http"
+	"os"
+	"path/filepath"
+	"regexp"
+	"sync"
+	"time"
+)
+
+var downloadDestFolder = "D:\\music"
+var w sync.WaitGroup
+
+func main() {
+	url := flag.String("url", "http://yinyue.kuwo.cn/yy/cinfo_73195.htm", "酷我歌单地址")
+	//bangdan:=flag.Int("bang",1,"榜单ID:16-酷我热歌榜;17-酷我新歌榜")
+	flag.Parse()
+	c := colly.NewCollector()
+	c.OnResponse(func(response *colly.Response) {
+		reg := regexp.MustCompile(`jsonm = (.*);`)
+		pat := reg.FindSubmatch(response.Body)
+		jsonStr := string(pat[1])
+		ParseJson(jsonStr)
+	})
+	c.Visit(*url)
+}
+
+func ParseJson(content string) {
+	musiclist := gjson.Get(content, "musiclist")
+	//c := colly.NewCollector()
+	if musiclist.Exists() {
+		re := musiclist.Array()
+		for _, v := range re {
+			musicrid := v.Get("musicrid").String()
+			name := v.Get("name").String()
+			artist := v.Get("artist").String()
+			client := http.Client{Timeout: 900 * time.Second}
+			url := fmt.Sprintf("http://antiserver.kuwo.cn/anti.s?format=mp3&type=convert_url&rid=MUSI_%s&response=url", musicrid)
+			resp, _ := client.Get(url)
+			body, _ := ioutil.ReadAll(resp.Body)
+			resp.Body.Close()
+			w.Add(1)
+			go download(string(body), fmt.Sprintf("%s.mp3", name))
+			time.Sleep(time.Duration(200) * time.Millisecond)
+		}
+	}
+	w.Wait()
+}
+
+func download(url string, filename string) {
+	defer w.Done()
+	nt := time.Now().Format("2006-01-02 15:04:05")
+	fmt.Printf("[%s]To download %s\n", nt, filename)
+	_ = os.MkdirAll(downloadDestFolder, 0777)
+	fpath := downloadDestFolder + string(filepath.Separator) + filename
+	newFile, err := os.Create(fpath)
+	if err != nil {
+		fmt.Println(err.Error())
+	}
+	defer newFile.Close()
+
+	client := http.Client{Timeout: 900 * time.Second}
+	resp, err := client.Get(url)
+	defer resp.Body.Close()
+
+	_, err = io.Copy(newFile, resp.Body)
+	if err != nil {
+		fmt.Println(err.Error())
+	}
+	log.Println("Saved:", filename)
+}