Django веб-приложение, скраппинг torrent seed, peer и полная загрузка делает сайт медленным

<

scraper.py

from tracker_scraper import scrape
def getinfo(path):
    #total peers and seed
    total_peers = 0
    total_seeds = 0


    #parse file using torrent tool
    my_torrent = Torrent.from_file(path)

    #get info_hash using torrent tool
    info_hash = my_torrent.info_hash

    #Torrent urls by torrent tool
    announce_url = my_torrent.announce_urls
    url_list = []
    host_list = []
    port_list = []

    #keep all tracker responses
    result = []

    #parse urls using urlparse
    for list in announce_url:
        for urls in list:
            m = urlparse(urls)
            port_list.append(m.port)
            host_list.append(m.hostname)
            url_list.append(urls.rstrip("/announce")) 

    #connect and record the response of tracker
    for url in url_list:
        try:
            res = scrape(tracker=url,hashes=[info_hash])[info_hash]
            response = {
            "seeds" : res['seeds'],
            "peers" : res['peers'],
            "completed" : res['complete'],
                "url" : url,
            }
            result.append(response)
        except Exception as e:
            print(e)


    #calculate total peers and seeds
    for item in result:
        total_seeds = total_seeds + item['seeds']
        total_peers = total_peers + item['peers']

    return result,total_peers,total_seeds

views.py

def resources(response):
    all_files = TorFile.objects.all().exclude(user=2)
    BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    # for files in all_files:
    #     path = BASE_DIR + "/media/" +str(files.file_field)
    #     result = getinfo(path)
    #     print(result[1])
    #     print(result[2])

    return render(response, "main/resources.html",{'all_files':all_files})

Вернуться на верх