def local_url(directory, filename, has_extension, media_type=None, **kwargs):
    '''判断本地文件是否存在,如果存在则返回相应url以及对应的文件名
    Args:
        filename:待搜索的文件名 或 url
        directory: filename 应存在的目录
        has_extension: filename 是否包含扩展名
        media_type: 指定 filename 的类型:图片、视频等
    Returns:
        url:
            若本地文件存在,返回其url,否则为空
        media_id:
            如果 filename 成功匹配某个待下载的资源,则为其对应 id

    '''

    media_id = ""
    if has_extension:
        full_filename = os.path.join(directory, filename)
        if os.path.exists(full_filename):
            return url_for("static", filename=full_filename.replace("\\", "/")), media_id
    else:
        file = search_file(directory, filename)
        if len(file) > 0:
            full_filename = os.path.join(directory, file)
            return url_for("static", filename=full_filename.replace("\\", "/")), media_id
        if kwargs.get("shuoshuo", False):
            r, media_id = search_shuoshuo_media_in_photo(
                directory, filename, media_type)
            if len(r) > 0:
                return r, media_id
        if kwargs.get("photo", False):
            r, media_id = search_photo(filename, directory, **kwargs)
            if len(r) > 0:
                return r, media_id
    return "", media_id
def serach_photo_todownload_file(photo_directory, filename, media_type=None):
    '''搜索相册中需要下载的文件是否包含 filename,如果有,返回相应url与id
    '''

    to_download_file = os.path.join(photo_directory, QzoneFileName.TO_DOWNLOAD)
    if not os.path.exists(to_download_file):
        return "", ""
    ids = get_media_ids(filename)
    m_id = ""
    with open(to_download_file, "r", encoding="utf-8") as fin:
        for line in fin:
            temp = line.split()
            if len(temp) != 3:
                continue
            media_id = temp[2]
            media_url = temp[0]
            for t in ids:
                if t in media_id or t in media_url:
                    m_id = media_id
                    break
            else:
                continue
            download_dir = temp[1]
            final_filename = search_file(download_dir, media_id)
            if match_media_type(final_filename, media_type):
                full_filename = os.path.join(download_dir, final_filename)
                return url_for("static", filename=full_filename.replace("\\", "/")), media_id
    return "", m_id
Beispiel #3
0
 def run(self) -> None:
     while True:
         fragment = receive_udp('', TCP_CON, 0)
         print('CON TCP receive', fragment.to_json())
         file_path = search_file(fragment.filename)
         if file_path:
             send_tcp(fragment.local_ip, fragment.local_port, file_path)
         else:
             print("file", fragment.filename, "is not exists")
Beispiel #4
0
    def run(self) -> None:
        """
        检索指定文件夹底下是否存在文件
        如果存在返回ACK
        不存在不返回任何消息
        :return:
        """
        while True:
            fragment = receive_udp('', UDP_SYN, 0)
            print('SYN receive', fragment.to_json())

            file_path = search_file(fragment.filename)
            if file_path is None:
                print(fragment.filename, 'not exist')
                continue
            print(fragment.filename, 'find in', file_path)

            fragment.dest_ip = fragment.local_ip
            fragment.local_ip = get_local_ip()
            fragment.dest_port = UDP_ACK
            fragment.local_port = UDP_SYN
            send_udp(fragment, 3)
            print('ACK send', fragment.to_json(), 3, 'times')