Skip to content
This repository has been archived by the owner on Nov 23, 2021. It is now read-only.

Commit

Permalink
Format: black code
Browse files Browse the repository at this point in the history
  • Loading branch information
chenjiandongx committed Feb 14, 2019
1 parent d81e2c0 commit 60f71fe
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 35 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Python3 DHT 网络磁力种子爬虫
# Python3 DHT 网络磁力种子采集器

## 😎 严肃的理论

Expand Down
12 changes: 3 additions & 9 deletions magnet_dht/crawler.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,13 @@


class HNode:

def __init__(self, nid, ip=None, port=None):
self.nid = nid
self.ip = ip
self.port = port


class DHTServer:

def __init__(self, bind_ip, bind_port, process_id):
self.bind_ip = bind_ip
self.bind_port = bind_port
Expand All @@ -78,9 +76,7 @@ def __init__(self, bind_ip, bind_port, process_id):
# nodes 节点是一个双端队列
self.nodes = deque(maxlen=MAX_NODE_QSIZE)
# KRPC 协议是由 bencode 编码组成的一个简单的 RPC 结构,使用 UDP 报文发送。
self.udp = socket.socket(
socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP
)
self.udp = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
# UDP 地址绑定
self.udp.bind((self.bind_ip, self.bind_port))
# redis 客户端
Expand Down Expand Up @@ -273,9 +269,7 @@ def receive_response_forever(self):
循环接受 udp 数据
"""
self.logger.info(
"receive response forever {}:{}".format(
self.bind_ip, self.bind_port
)
"receive response forever {}:{}".format(self.bind_ip, self.bind_port)
)
# 首先加入到 DHT 网络
self.bootstrap()
Expand All @@ -302,7 +296,7 @@ def _start_thread(offset):
threads = [
Thread(target=dht.send_find_node_forever),
Thread(target=dht.receive_response_forever),
Thread(target=dht.bs_timer)
Thread(target=dht.bs_timer),
]

for t in threads:
Expand Down
5 changes: 1 addition & 4 deletions magnet_dht/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@


class RedisClient:

def __init__(
self, host=REDIS_HOST, port=REDIS_PORT, password=REDIS_PASSWORD
):
def __init__(self, host=REDIS_HOST, port=REDIS_PORT, password=REDIS_PASSWORD):
conn_pool = redis.ConnectionPool(
host=host,
port=port,
Expand Down
5 changes: 1 addition & 4 deletions magnet_dht/magnet_to_torrent_aria2c.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,7 @@ def exec_rpc(magnet):
],
}
conn.request(
"POST",
"/jsonrpc",
json.dumps(req),
{"Content-Type": "application/json"},
"POST", "/jsonrpc", json.dumps(req), {"Content-Type": "application/json"}
)

res = json.loads(conn.getresponse().read())
Expand Down
1 change: 0 additions & 1 deletion magnet_dht/parse_torrent.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@


class ParserTorrent:

def __init__(self, torrent):
self.meta_info = self.get_meta_info(torrent)

Expand Down
10 changes: 4 additions & 6 deletions magnet_dht/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ def get_nodes_info(nodes):
return []

for i in range(0, length, PER_NODE_LEN):
nid = nodes[i:i + PER_NID_LEN]
nid = nodes[i : i + PER_NID_LEN]
# 利用 inet_ntoa 可以返回节点 ip
ip = inet_ntoa(nodes[i + PER_NID_LEN:i + PER_NID_NIP_LEN])
ip = inet_ntoa(nodes[i + PER_NID_LEN : i + PER_NID_NIP_LEN])
# 解包返回节点端口
port = unpack("!H", nodes[i + PER_NID_NIP_LEN:i + PER_NODE_LEN])[0]
port = unpack("!H", nodes[i + PER_NID_NIP_LEN : i + PER_NODE_LEN])[0]
yield (nid, ip, port)


Expand All @@ -62,8 +62,6 @@ def get_logger(logger_name):
logger = logging.getLogger(logger_name)
logger.setLevel(LOG_LEVEL)
fh = logging.StreamHandler()
fh.setFormatter(
logging.Formatter("%(asctime)s - %(levelname)s - %(message)s")
)
fh.setFormatter(logging.Formatter("%(asctime)s - %(levelname)s - %(message)s"))
logger.addHandler(fh)
return logger
14 changes: 4 additions & 10 deletions manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,10 @@ def get_parser():
解析命令行参数
"""
parser = argparse.ArgumentParser(description="start manage.py with flag.")
parser.add_argument(
"-s", action="store_true", help="run start_server func."
)
parser.add_argument(
"-m", action="store_true", help="run magnet2torrent func"
)

parser.add_argument(
"-p", action="store_true", help="run parse_torrent func"
)
parser.add_argument("-s", action="store_true", help="run start_server func.")
parser.add_argument("-m", action="store_true", help="run magnet2torrent func")

parser.add_argument("-p", action="store_true", help="run parse_torrent func")
return parser


Expand Down

0 comments on commit 60f71fe

Please sign in to comment.