def _timerUpDate(self): self.currDateTime = QDateTime.currentDateTime() self.hello = TextUtil.getHelloWord(self.currDateTime) self.currDate = self.currDateTime.toString("yyyy年MM月dd日") self.currTime = self.currDateTime.toString("hh:mm:ss") self.welcomeWidget.lblDate.setText(self.currDate) self.welcomeWidget.lblTime.setText(self.hello[0] + ' ' + self.currTime)
def _chooseDir(self): self.dir = QFileDialog.getExistingDirectory(self, "选取文件夹", TextUtil.get_desktop()) if self.dir == '': return self.batchDownloadForm.lblDir.setText(self.dir) pass
def getAllLinks(self, result, log): """ 获取动漫的全部链接 :param result: 由detail()函数获取的结果 如: {1: ['第1集', 'url'], 2: ['第2集', 'url']} :param log: 打印日志函数 :return: 不返回值,直接把链接保存到文本中 """ # 捕获异常 try: log('正在抓取链接') log('【提示】在未出现成功提示之前最好不要离开此页面!') log('...') Num = len(result) # 格式化集数:第1集 --> 第01集 根据集数而定 n = TextUtil.getIntegerDigits(Num) f = '第%0{}d集'.format(n) for i in range(1, Num + 1): name = result[i][0] if re.match('第(.*?)集', name): num = re.findall('第(.*?)集', name)[0] # 看下num是不是整数 if re.match(r'\d *', num): name = f % int(num) result.update({i: [name, result[i][1]]}) pass # 创建文件 desktop = TextUtil.get_desktop() file = open(desktop + '/download.txt', 'w') file.close() for i in range(1, Num + 1): time.sleep(1) # print(result[i][0]) with open(desktop + '/download.txt', 'a') as f: f.write(result[i][0] + '@' + self.getVideoUrl(result[i][1]) + '\n') log('【{}】'.format(result[i][0])) print('【{}】'.format(result[i][0])) pass pass log('抓取完成!') log('以保存至 ' + desktop + '\\download.txt') except Exception as e: print('yc') print(e.args) pass
def url_changed(self): # 如果是返回来的话直接不处理了 if self.isBack: print('返回操作,不处理') self.isBack = False return self.btnGetAllLinks.setEnabled(False) currUrl = TextUtil.QUrl_2_str(self.browser.url()) self.urlBar.setText(currUrl) if currUrl == R.string.HOME_URL: self.log('进入主页') pass # 首先有两种链接: # 第一种: # http://susudm.com/acg/2130/ # http://fcdm.in/acg/2130/ 出现这种形式的链接 为可以爬取所有视频链接的 # 第二种: # http://susudm.com/acg/2130/1.html # http://fcdm.in/acg/2130/1.html 出现这种形式的链接 为可以调用potplayer播放的 reg1 = r'http://.*?/.*?/([1-9]\d*)/$' # 匹配第一种 reg2 = r'http://.*?/.*?/[1-9]\d*/(.*?).html$' # 匹配第二种 if re.match(reg1, currUrl): # 每当进入此类链接,already置为False self.already = False # 开启线程执行 t = threading.Thread(target=self.do_reg1, name='', args=(currUrl,)) t.start() return if re.match(reg2, currUrl): # 判断是否完成获取 if self.already: # 开启线程执行 t = threading.Thread(target=self.do_reg2, name='', args=(currUrl,)) t.start() pass else: self.showWarning('亲的手速太快了\n现在还没有完成获取番剧信息!请耐心等待!') pass self.isBack = True self.browser.back() pass pass
def _getAllLinks(self): # 抓取所有链接(如果是m3u8链接那也没有办法) self.safeLog('抓取所有链接...') # 获取桌面路径 desktop = TextUtil.get_desktop() # print(self.jsonLinkDir) print(desktop) f = open(desktop + '/download.txt', 'w') f.close() f = open(desktop + '/download.txt', 'a') for i in range(1, self.episodeNum + 1): time.sleep(1) lk = CrawlUtil.getPlayLink(self.jsonLinkDir[i]) self.safeLog('获取【{}】'.format(self.episodeDir[i])) s = '{}@{}'.format(self.episodeDir[i], lk) f.write(s + '\n') # print(s) pass f.close() self.safeLog('抓取所有链接完成!!') self.safeLog('已保存至:' + desktop + '/download.txt') pass
def _changeBG(self): # 选择图片 fileName_choose, filetype = QFileDialog.getOpenFileName( self, '选择背景图', TextUtil.get_desktop(), "背景图(只支持png) (*png);") if fileName_choose == '': return img_path = 'resource/imgs/welcome/welcome_01.png' # 备份原来的图片 # 生成乱码后缀 sur = ''.join( random.sample([ 'z', 'y', 'x', 'w', 'v', 'u', 't', 's', 'r', 'q', 'p', 'o', 'n', 'm', 'l', 'k', 'j', 'i', 'h', 'g', 'f', 'e', 'd', 'c', 'b', 'a' ], 6)) img_path_ = 'resource/imgs/welcome/welcome_01{}.png'.format(sur) shutil.copyfile(img_path, img_path_) # 复制选择的图片 shutil.copyfile(fileName_choose, img_path) # 生效 self.parent.syncInterface() pass
class BatchDownloadForm(QWidget): links = path = TextUtil.get_desktop() + '/download.txt' dir = '' def __init__(self): super().__init__() self.batchDownloadForm = Ui_batchDownloadForm() self.batchDownloadForm.setupUi(self) self.initAppearance() self.batchDownloadForm.btnChooseDir.clicked.connect(self._chooseDir) self.batchDownloadForm.btnOK.clicked.connect(self.finished) self.batchDownloadForm.btnCheckIDM.clicked.connect(self._checkIDM) self.batchDownloadForm.btnCheckLinks.clicked.connect(self._checkLinks) pass def initAppearance(self): # 设置图标 icon = QtGui.QIcon() icon.addPixmap(QtGui.QPixmap("resource/imgs/logo.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off) self.setWindowIcon(icon) pass def _chooseDir(self): self.dir = QFileDialog.getExistingDirectory(self, "选取文件夹", TextUtil.get_desktop()) if self.dir == '': return self.batchDownloadForm.lblDir.setText(self.dir) pass def finished(self): if self.batchDownloadForm.lblDir.text() == '无': return t = threading.Thread(target=self._doBatch, name='doBatch') t.start() pass def _doBatch(self): f = open(self.links) lines = f.readlines() f.close() print(lines) self._checkIDM() for line in lines: line = line.strip() ls = re.split(r'@', line) name = ls[0] + '.mp4' link = ls[1] call([ Configuration().idm_path, '/d', link, '/p', self.dir, '/f', name, '/n', '/a' ]) pass pass def _checkIDM(self): path = Configuration().idm_path t = threading.Thread(target=lambda: call(path), name='testIDM') t.start() pass def _checkLinks(self): t = threading.Thread(target=lambda: call(['notepad', self.links]), name='testLinks') t.start() pass pass
def do_reg1(self, url): """ 此函数被一个线程调用 后台执行获取番剧一些信息等操作 :param url: 番剧的链接 :return: None """ self.safeLog('进入链接:{}'.format(url)) self.safeLog('正在获取此番剧信息... 请耐心等待获取信息完毕后再点击观看!') self.safeLog('......') # 下载源码 self.safeLog('下载Html源码...') time.sleep(1) self.HtmlSrc = CrawlUtil.getHtmlSrc(url) self.safeLog('成功!') # print(self.HtmlSrc) # 获取番名 self.episodeName = CrawlUtil.getEpisodeName(self.HtmlSrc) print(self.episodeName) self.safeLog('正在访问:【{}】'.format(self.episodeName)) # 获取番剧路径 self.episodePath = CrawlUtil.getEpisodePath(url) # print(self.episodePath) # 获取集数名的dir self.episodeDir = CrawlUtil.getEpisodeDir(self.HtmlSrc, self.episodePath) # print(self.episodeDir) # 获取总集数 self.episodeNum = len(self.episodeDir) self.safeLog('总集数:{}'.format(self.episodeNum)) # 格式化集数名 '第1集' --> '第01集' 或 '第001集' 根据集数设置位数 n = TextUtil.getIntegerDigits(self.episodeNum) f = '第%0{}d集'.format(n) for i in range(1, self.episodeNum + 1): episode = self.episodeDir[i] if re.match('第(.*?)集', episode): num = re.findall('第(.*?)集', episode)[0] # 看下num是不是整数 if re.match(r'\d *', num): episode = f % int(num) self.episodeDir.update({i: episode}) pass # print(self.episodeDir) # 如果集数过多,就打印进度信息 if self.episodeNum > 20: self.safeLog('当前番剧集数过多!请耐心等待!') pass # 获取改番剧js文件的源码,后期找每一集的链接用 self.safeLog('正在获取js文件...') self.jsSrc = CrawlUtil.getJsSrc(url) self.safeLog('获取js文件成功!') # print(self.jsSrc) # 获取 {集数:每一集的json文件}字典 self.safeLog('正在整合每一集的播放链接...') self.jsonLinkDir = CrawlUtil.getJsonLinkDir(self.jsSrc, self.episodeNum) self.safeLog('成功!') # print(self.jsonLinkDir) # self.safeLog('获取完毕:') # self.safeLog('正在访问:【{}】 总集数:{}'.format(self.episodeName, self.episodeNum)) self.safeLog('【提示】可以爬取此页面所有下载链接!!请点击【抓取链接】按钮进行此操作!!') self.safeLog('番剧获取完毕!') self.already = True self.btnGetAllLinks.setEnabled(True) pass