def parse_current_seeds(cls, print_log=True): seeds = [] cmd_result = os.popen("transmission-remote -l").read() lines = cmd_result.split("\n")[1: -2] # remove first and last line now = datetime.datetime.now() for line in lines: seed = TransmissionSeed() seeds.append(seed) data = line.split() seed.id = data[0].replace("*", "") cmd_result = os.popen("transmission-remote -t {0} -i".format(seed.id)).read() seed_details = cmd_result.split("\n") for detail in seed_details: if detail.startswith(" Name: "): seed.name = detail.replace(" Name: ", "") elif detail.startswith(" State: "): seed.status = detail.replace(" State: ", "") elif detail.startswith(" Percent Done:"): seed.done = float(detail.replace(" Percent Done: ", "").replace('%', '')) elif detail.startswith(" ETA: "): seed.ETA = detail.replace(" ETA: ", "").replace(" ", "").split("(")[0] elif detail.startswith(" Download Speed: "): seed.down = HttpUtils.pretty_format( detail.replace(" Download Speed: ", "").replace(" ", "").split("/s")[0], "KB") elif detail.startswith(" Upload Speed: "): seed.up = HttpUtils.pretty_format( detail.replace(" Upload Speed: ", "").replace(" ", "").split("/s")[0], "KB") elif detail.startswith(" Total size: "): seed.size = HttpUtils.pretty_format( detail.replace(" Total size: ", "").replace(" ", "").split("(")[0], "MB") elif detail.startswith(" Ratio: "): ratio_str = detail.replace(" Ratio: ", "") if ratio_str == "None": seed.ratio = 0.0 else: seed.ratio = float(ratio_str) elif detail.startswith(" Date added: "): start_time = parser.parse(detail.replace(" Date added: ", "").strip()) seed.since = (now - start_time).seconds elif detail.startswith(" Downloaded: "): seed.done_size = HttpUtils.pretty_format( detail.replace(" Downloaded: ", ""), "KB") elif detail.startswith(" Location: "): seed.location = detail.replace(" Location: ", "") if print_log: for seed in seeds: print(seed) return seeds
def stat(self, unit="GB", update_cache=True): self.login_if_not() soup_obj = HttpUtils.get(self.site.stat_page, headers=self.site.login_headers) assert soup_obj is not None div_list = soup_obj.select( "table.mainouter tr td table tr td div[align='center']") assert len(div_list) == 1 content = div_list[0].contents[0] m = re.search(u"获取(\d+.\d+)个魔力", content) assert m mp = float(m.group(1)) span_list = soup_obj.select("#usermsglink span") up = HttpUtils.pretty_format(span_list[1].contents[2], unit) down = HttpUtils.pretty_format(span_list[1].contents[4], unit) prev_up = Cache().get(self.get_site_name() + "_up") prev_down = Cache().get(self.get_site_name() + "_down") if prev_up is None: prev_up = 0 else: prev_up = float(prev_up.decode()) if prev_down is None: prev_down = 0 else: prev_down = float(prev_down.decode()) delta_up = round(up - prev_up, 2) delta_down = round(down - prev_down, 2) if delta_down == 0: delta_ratio = "Inf" else: delta_ratio = round(delta_up / delta_down, 2) current_upload = round(up - down, 2) print( "%s, mp=%s, up=%s, down=%s, current=%s, delta_up=%s, delta_down=%s, delta_ratio=%s" % (str(time.strftime("%Y-%m-%d %H:%M:%S")), mp, up, down, current_upload, delta_up, delta_down, delta_ratio)) if update_cache: Cache().set(self.get_site_name() + "_up", up) Cache().set(self.get_site_name() + "_down", down) return mp, up, down
def parse_size(self, soup_obj): assert soup_obj is not None assert len(soup_obj.contents) == 3 size_num = round(float(soup_obj.contents[0]) * self.size_factor, 2) size_unit = soup_obj.contents[2] return HttpUtils.pretty_format(str(size_num) + str(size_unit), "MB")
def parse(self, soup_obj): assert soup_obj is not None info_block = soup_obj.select( "#info_block table tr td:nth-of-type(1) span")[0] prev_info = "" upload = 0 download = 0 for info in info_block.contents: if "上傳量" in prev_info: upload = HttpUtils.pretty_format(info, "GB") elif "下載量" in prev_info: download = HttpUtils.pretty_format(info, "GB") break prev_info = str(info) return upload, download
def parse_page(self, soup_obj): items = soup_obj.select("item") assert len(items) != 0 seeds = [] for item in items: try: info = HttpUtils.get_content(item, "title").split("[") seed = SeedInfo() seed.title = info[0].strip() seed.size = HttpUtils.pretty_format(info[1].split("]")[0], "MB") seed.url = HttpUtils.get_attr(item, "enclosure", "url") seed.id = self.parse_id(seed.url) #Cache().set(seed.id, str(seed)) seeds.append(seed) except Exception as e: print(e.getMessage()) return seeds
def parse_page(self, soup_obj): items = soup_obj.select("item") assert len(items) != 0 seeds = [] for item in items: try: info = HttpUtils.get_content(item, "title").split("[") seed = SeedInfo() seed.title = info[0].strip() seed.size = HttpUtils.pretty_format(info[1].split(" ")[-2] + info[1].split(" ")[-1], "MB") # seed.url = HttpUtils.get_content(item, "link") seed.url = item.contents[4] seed.id = self.parse_id(seed.url) seeds.append(seed) except Exception as e: pass return seeds
def parse_size_in_gb(self, size_str): assert size_str is not None return HttpUtils.pretty_format(size_str.replace(": ", ""), "GB")