Esempio n. 1
0
def get_magick_version(gm_or_im):
    """ get version of *Magick """

    version = ""
    if gm_or_im is None:
        gm_or_im = ""

    file_version = common.spacja(os.path.join(tempfile.gettempdir(),
                                              "fotokilof_" + \
                                              os.getlogin() + "_version"))

    command = "-Version > "
    result = magick(command, "", common.spacja(file_version),
                    gm_or_im + "convert")
    if result is not None:
        try:
            file = open(file_version, "r")
        except:
            log.write_log("get_magick_version: cannot read file_version", "W")
        else:
            version_object = re.search("\\d+[.]\\d+([.]\\d+)*", file.readline())
            if version_object is not None:
                version = version_object[0]
            file.close()
            try:
                os.remove(file_version)
            except:
                log.write_log("get_magick_version: cannot remove file_version", "W")

    return version
Esempio n. 2
0
def get_image_size(file_in, gm_or_im):
    """
    identify width and height of picture
    input: file name
    output: width and height
    """

    width = 1
    height = 1
    size = ""
    file_info = common.spacja(os.path.join(tempfile.gettempdir(),
                                           "fotokilof_" + os.getlogin() \
                                           + "_image_info"))

    command = ' -format "%w\\n%h\\n%b" '
    command = command + common.spacja(file_in) + ' > '
    result = magick(command, "", file_info, gm_or_im + "identify")
    if result is not None:
        try:
            file = open(file_info, "r")
        except:
            log.write_log("get_image_size: cannot read file_info", "W")
        else:
            width = int(file.readline())
            height = int(file.readline())
            size = file.readline()
            file.close()
            try:
                os.remove(file_info)
            except:
                log.write_log("get_image_size: cannot remove image_info", "W")
    return (width, height, size)
Esempio n. 3
0
def pre_magick(file_in, destination, extension):
    """
    file_in - original file for processing
    destination - output directory
    extension - extension of result file, for change format (jpg->png)
    file_out - fullname file for processing in destination
    """
    result = "OK"  # initial value
    if file_in is not None:
        if os.path.isfile(file_in):
            # making output diretory if not exist
            out_dir = os.path.join(os.path.dirname(file_in), destination)
            if os.path.isdir(out_dir) is False:
                try:
                    os.mkdir(out_dir)
                except:
                    log.write_log("pre_imagick: Cannot make directory for output pictures", "E")
                    result = None
        else:
            result = None
    else:
        result = None

    if result == "OK":
        # preparing output filename
        file_in_without_ext = os.path.splitext(file_in)
        file_out = os.path.join(out_dir,
                                os.path.basename(file_in_without_ext[0] \
                                                 + extension))
    else:
        file_out = None
    return file_out
Esempio n. 4
0
async def search(searchterm):
    magnet_links = []
    timeout = aiohttp.ClientTimeout(total=60)
    try:
        async with aiohttp.ClientSession(timeout=timeout) as session:
            async with session.get(
                    f"https://{settings.PIRATEBAY_HOST}/search/{searchterm}/1/99/0"
            ) as resp:
                data = await resp.text()
        soup = BeautifulSoup(data, "html.parser")
        trs = soup.find(id="searchResult").find_all("tr")
        for tr in trs:
            try:
                tds = tr.find_all("td")
                if len(tds) < 2:
                    continue
                td = tds[1]
                try:
                    seeds = int(tr.find_all("td")[2].contents[0])
                except ValueError:
                    seeds = 0
                a = td.find_all("a")
                title = str(a[0].contents[0])
                magnet_link = str(a[1]["href"])
                if seeds:
                    magnet_links.append(
                        dict(title=title, magnet=magnet_link, seeds=seeds))
            except Exception:
                log.write_log()
    except Exception:
        log.write_log()
    return magnet_links
Esempio n. 5
0
def log(msg):
    """
    logging function, print screen & log to file
    """
    print(msg)  # print to screen
    logging.info(msg.lstrip())  # write to log file
    write_log('_init', '0000-00-00', 'initialize', 'warning', msg.lstrip())
Esempio n. 6
0
    def ident(self, ident, host, realname):
        """ ident the user """

        self.send("USER %s %s r :%s\r\n" % (ident, host, realname))

        #log the action
        log.write_log("Ident %s %s %s." % (ident, host, realname))
Esempio n. 7
0
def preview_convert(file_in, command, size, gm_or_im):
    """
    preview generation
    file_in - fullname image file
    dir_temp - fullname temporary directory
    command - additional command for imagemagick or space
    --
    return: fullname preview file and size
    """
    try:
        image_size = magick.get_image_size(file_in, gm_or_im)
        width = str(image_size[0])
        height = str(image_size[1])
        filesize = common.humansize(os.path.getsize(file_in))

        cmd_magick = gm_or_im + "convert"
        command = " -resize " + str(size) + "x" + str(size) + command
        file_preview = os.path.join(tempfile.gettempdir(),
                                    "fotokilof_" + getpass.getuser() \
                                    + "_preview.ppm")
        magick.magick(command, file_in, file_preview, cmd_magick)

        result = {'filename': file_preview, 'size': filesize, \
                'width': width, 'height': height}
    except:
        log.write_log("Error in preview_convert: return", "E")
        result = None

    return result
Esempio n. 8
0
    def set_nick(self, nick):
        """ sets/changes nickname of the bot """

        self.send("NICK %s\r\n" % (nick))

        #log the action
        log.write_log("Nick changed to %s." % (nick))
Esempio n. 9
0
    def end(self):
        """ end this connection """

        self.sock.close()

        #log the action
        log.write_log("End of connection.")
Esempio n. 10
0
    def __init__(self, server, port, nick, ident, host, realname, useSsl):
        """ new bot constructor """

        #log the action
        log.write_log("New bot alive.")

        self.server = server
        self.port = port

        #start a new connection
        self.connection = Connection(server, port, useSsl)

        #set nickname
        self.nick = nick
        self.connection.set_nick(self.nick)
        self.ident = ident
        self.host = host
        self.realname = realname

        #ident
        self.connection.ident(ident, host, realname)

        #used for uptime
        self.startTime = time.time()

        #used to identify the ircd of the current server
        self.ircd = ""

        self.channels = [] # each element is a (channel, topic, userlist) tuple
        self.users = [] # each element is a (nick, whois) tuple
Esempio n. 11
0
    def join_channel(self, channel):
        """ join a new channel """
        
        self.send("JOIN %s\n" % (channel))

        #log the action
        log.write_log("Joined channel %s." % (channel))
Esempio n. 12
0
    def part_channel(self, channel):
        """ join a new channel """
        
        self.send("PART %s\n" % (channel))

        #log the action
        log.write_log("Parted from channel %s." % (channel))
Esempio n. 13
0
    def get_names(self, channel):
        """ request user list for channel """

        self.send("NAMES %s\r\n" % (channel))

        #log the action
        log.write_log("Requested NAMES from %s" % (channel))
Esempio n. 14
0
    def ping(self, response):
        """ respond to pings """

        self.send("PONG %s\r\n" % (response))

        # log the action
        log.write_log("PING.")
Esempio n. 15
0
    def msg_channel(self, channel, message):
        """ send message to a channel """

        self.send("PRIVMSG %s :%s\r\n" % (channel, message))

        #log the action
        log.write_log("Messaged %s : %s" % (channel, message))
Esempio n. 16
0
    def msg_user(self, user, message):
        """ send message to a user """

        self.send("PRIVMSG %s :%s\r\n" % (user, message))

        #log the action
        log.write_log("Messaged %s : %s" % (user, message))
Esempio n. 17
0
    def nickserv_identify(self, passwd):
        """ identifies using NickServ """

        self.send("PRIVMSG Nickserv :IDENTIFY %s\r\n" % (passwd))

        #log the action
        log.write_log("Identified with password %s" % (passwd))
def add_config(key, value):
    if not os.path.exists(
            os.path.expanduser('~') + r'\NeteaseCloudMusicProxy'):
        os.mkdir(os.path.expanduser('~') + r'\NeteaseCloudMusicProxy')
    try:
        if userconfig_exists():
            with open(os.path.expanduser('~') +
                      r'\NeteaseCloudMusicProxy\UserConfig.json',
                      mode='r') as configfile:
                config = json.load(configfile)
                config[key] = value
                jsonfile = json.dumps(config)
            with open(os.path.expanduser('~') +
                      r'\NeteaseCloudMusicProxy\UserConfig.json',
                      mode='w') as configfile:
                configfile.write(jsonfile)
            return True
        else:

            config = {}
            with open(os.path.expanduser('~') +
                      r'\NeteaseCloudMusicProxy\UserConfig.json',
                      mode='w') as configfile:
                config[key] = value
                configfile.write(json.dumps(config))
            return True
    except Exception as e:
        write_log(e)
        return False
Esempio n. 19
0
def login():

    url='http://poro.ws/auth/login'
    data={
        'email':username,
        'passwd':password,
        'remeber_me':'week',
        }

    try:

        response=requests.post(url=url,headers=headers,data=data,timeout=30)
        content=response.json()
        ret=content['ret']
        msg=content['msg']
        # print type(msg)
        # msg=msg.encode('utf-8')
        write_log(log=msg)
        if ret:
            # print msg
            print u'login successfully!'
            return response.cookies

        else:
            # print msg
            print u'login failed!'
            return 0


    except Exception,e:
        print e
        log=traceback.format_exc()
        write_log(log=log)
        return None
Esempio n. 20
0
 def test_write_log(self):
     User.create(username='******')
     expected = 'username'
     write_log(datetime.datetime(2011, 11, 11, 0, 0), 'Test', 60, 'Notes')
     log = Log.select()
     actual = log[0].username
     self.assertEqual(actual, expected)
Esempio n. 21
0
	def save_file(self):
		while 1:
			self.get_window()
			self.check_window()
			for key, value in self.active_windows.items():
				#print key, value
				self.send_key(key, win32con.VK_CONTROL, ord('S'))
				write_log('info', 'save file %s' % value['text'])
			time.sleep(self.save_time) #保存文件需要一定时间
Esempio n. 22
0
def parse_column_list(columns, raw=False):
    """
    convert daily column list to a string for MySQL query
    """
    ##column_string = ', '.join('{0}'.format(col) for col in columns)

    if raw:
        cond = 'raw'
    else:
        cond = 'raw, indicator, decision'

    tmp = cond.split()  # convert to list
    conds = [x.strip(',') for x in tmp]  # strip out ','

    col_name_str = ''
    # column_names = []
    # column_types = []
    for column in columns:
        try:
            column[3]
        except IndexError:
            col_name_str = col_name_str + column[0] + ', '
        else:
            if column[3] in conds:
                col_name_str = col_name_str + column[0] + ', '

            # if only need raw-data, for non-raw-data, change to "default as column"
            else:
                if (column[1] == 'float') or (column[1] == 'int'):
                    col_name_str = col_name_str + 'Null as ' + column[0] + ', '
                elif column[1] == 'str':
                    col_name_str = col_name_str + '"' + column[
                        2] + '" as ' + column[0] + ', '
                elif (column[1] == 'bool') and (column[2] == True):
                    col_name_str = col_name_str + '1 as ' + column[0] + ', '
                elif (column[1] == 'bool') and (column[2] == False):
                    col_name_str = col_name_str + '0 as ' + column[0] + ', '
                else:
                    write_log('N/A', 'N/A', '0000-00-00', 'parse_column_list', 'critical', \
                        'WRONG columns constant')

                # if column[1] == 'float':
                #     col_name_str = col_name_str + str(column[2]) + ' as ' + column[0] + ', '
                # elif column[1] == 'str':
                #     col_name_str = col_name_str + '"' + column[2] + '" as ' + column[0] + ', '
                # elif column[1] == 'bool' and column[2] == True:
                #     col_name_str = col_name_str + '1 as ' + column[0] + ', '
                # elif column[1] == 'bool' and column[2] == False:
                #     col_name_str = col_name_str + '0 as ' + column[0] + ', '
                # else:
                #     write_log('N/A', '0000-00-00', 'parse_column_list', 'critical', \
                #         'WRONG columns constant')

    # removing the last ', '
    col_name_str = col_name_str[:-2]

    return col_name_str
Esempio n. 23
0
def get_cached_url(magnet_hash, filename):

    if not any([DEVICE_CODE, CLIENT_ID, CLIENT_SECRET]):
        return None

    try:

        creds = requests.post(
            "https://api.real-debrid.com/oauth/v2/token",
            dict(
                client_id=CLIENT_ID,
                client_secret=CLIENT_SECRET,
                code=DEVICE_CODE,
                grant_type="http://oauth.net/grant_type/device/1.0",
            ),
        ).json()

        access_token = creds["access_token"]

        def get(path):
            return requests.get(
                f"https://api.real-debrid.com/rest/1.0{path}",
                headers=dict(authorization=f"Bearer {access_token}"),
            ).json()

        def post(path, data):
            try:
                return requests.post(
                    f"https://api.real-debrid.com/rest/1.0{path}",
                    data,
                    headers=dict(authorization=f"Bearer {access_token}"),
                ).json()
            except:
                return None

        instant = get(f"/torrents/instantAvailability/{magnet_hash}")
        result = instant.get(magnet_hash)
        rd = result.get("rd") if isinstance(result, dict) else None
        if rd:
            result = post(
                "/torrents/addMagnet/",
                dict(magnet=f"magnet:?xt=urn:btih:{magnet_hash}"),
            )
            torrent_id = result["id"]
            post(f"/torrents/selectFiles/{torrent_id}", dict(files="all"))
            links = get(f"/torrents/info/{torrent_id}")["links"][:30]
            unrestricted_links = [
                post("/unrestrict/link", dict(link=link))["download"] for link in links
            ]
            for i, link in enumerate(unrestricted_links):
                if unquote(link).endswith(filename):
                    return link

    except Exception as e:
        log.write_log()

    return None
Esempio n. 24
0
def write_cookies(cookies):
    '''将网络cookies写入本地'''

    j = json.dumps(cookies, ensure_ascii=False)
    log = u'write cookies dict into cookies.json......'
    print log
    write_log(log=log)
    with open(cookies_file, 'w') as f:
        f.write(j)
Esempio n. 25
0
def update():
    times = 0
    while True:
        try:
            times += 1
            if not os.path.exists('./RuleSet'):
                os.mkdir('./RuleSet')
            if not os.path.exists('./Proxy'):
                os.mkdir('./Proxy')
            Threadlist = []
            ThreadPool = ThreadPoolExecutor(max_workers=3)
            Threadlist.append(
                ThreadPool.submit(
                    get_github_config, 'config.yaml',
                    'https://151.101.184.133/DesperadoJ/Rules-for-UnblockNeteaseMusic/master/Clash/UnblockNeteaseMusic.yaml'
                ))
            Threadlist.append(
                ThreadPool.submit(
                    get_github_config, './Proxy/NeteaseMusic.yaml',
                    'https://151.101.184.133/DesperadoJ/Rules-for-UnblockNeteaseMusic/master/Clash/Proxy/NeteaseMusic.yaml'
                ))
            Threadlist.append(
                ThreadPool.submit(
                    get_github_config, './RuleSet/NeteaseMusic.yaml',
                    'https://151.101.184.133/DesperadoJ/Rules-for-UnblockNeteaseMusic/master/Clash/RuleSet/NeteaseMusic.yaml'
                ))
            wait(Threadlist)
            ClashProviderMerge.ConfigMerge()
            # with open('config.yaml', mode='w', encoding='utf-8') as file:
            #     response = requests.get(
            #         url='https://151.101.184.133/DesperadoJ/Rules-for-UnblockNeteaseMusic/master/Clash/UnblockNeteaseMusic.yaml',
            #         headers={"Host": 'raw.githubusercontent.com'}, verify=False)
            #     response.encoding = response.apparent_encoding
            #     file.write(response.text)
            # with open('./Proxy/NeteaseMusic.yaml', mode='w', encoding='utf-8') as file:
            #     response = requests.get(
            #         url='https://151.101.184.133/DesperadoJ/Rules-for-UnblockNeteaseMusic/master/Clash/Proxy/NeteaseMusic.yaml',
            #         headers={"Host": 'raw.githubusercontent.com'}, verify=False)
            #     response.encoding = response.apparent_encoding
            #     file.write(response.text)
            # with open('./RuleSet/NeteaseMusic.yaml', mode='w', encoding='utf-8') as file:
            #     response = requests.get(
            #         url='https://151.101.184.133/DesperadoJ/Rules-for-UnblockNeteaseMusic/master/Clash/RuleSet/NeteaseMusic.yaml',
            #         headers={"Host": 'raw.githubusercontent.com'}, verify=False)
            #     response.encoding = response.apparent_encoding
            #     file.write(response.text)
            print("更新订阅成功!")
            break
        except Exception as e:
            if times >= 5:
                showerror('警告', '在更新订阅时遇到了一个错误,请联系管理员修复程序!\n错误信息:{}'.format(e))
                write_log(e)
                sys.exit(0)
            else:
                continue
Esempio n. 26
0
def check_cloudmusic_process():
    try:
        WMI = win32com.client.GetObject('winmgmts:')
        processCodeCov = WMI.ExecQuery(
            'select * from Win32_Process where Name="cloudmusic.exe"')
        if len(processCodeCov) > 0:
            return True
        else:
            return False
    except Exception as e:
        showerror('错误', '检测网易云进程出错!错误信息:{}'.format(e))
        write_log(e)
def read_config(key):
    try:
        if not userconfig_exists():
            return False
        with open(os.path.expanduser('~') +
                  r'\NeteaseCloudMusicProxy\UserConfig.json',
                  mode='r') as configfile:
            config = json.loads(configfile.read())
            return config[key]
    except Exception as e:
        write_log(e)
        return False
Esempio n. 28
0
def read_cookies():
    '''从本地载入cookies '''

    if not os.path.exists(cookies_file):
        log = u'localtion cookies.json doest exists,will load cookies from internet'
        write_log(log=log)
        print log
        return None

    with open(cookies_file, 'r') as f:
        r = f.read()
    cookies = json.loads(r)
    return cookies
Esempio n. 29
0
def getResult1(disease, matrix, table_name, city, people_locat):
    """
    得到最终结果
    :param matrix:预测的矩阵
    :param disease: 疾病
    :return:
    """
    global hos_rank
    global scort
    global tt
    global length
    global cal
    hos_rank = {
        '9': 1,
        '8': 2,
        '7': 3,
        '6': 4,
        '5': 5,
        '4': 6,
        '3': 7,
        '2': 8,
        '1': 9
    }
    scort = []
    tt = []
    length = 0
    cal = 0

    # doc_info:[[doc_id,doc_name,hos_name,score,rank,hos_locat]]
    doc_info = filter_hos_By_sorted100(table_name, matrix, disease, city)
    # 筛选指定距离以内的医院的医生
    doc_filter = filter_by_distance(doc_info, people_locat)

    #未多样性前的排序
    f = log.write_log()
    print "预测后顺序======================================================="
    for doc in doc_filter:
        print("%-20s%-20s" % (doc[1].encode('utf-8'), doc[2].encode('utf-8')))

    print "完成筛选指定距离以内的医院"
    last_result = get_weight(doc_filter)

    # 输出最终结果
    f = log.write_log()
    print >> f, "疾病:", disease
    print >> f, ("%-20s%-20s" % ("姓名", "医院"))
    for doc in last_result:
        print >> f, ("%-20s%-20s" %
                     (doc[1].encode('utf-8'), doc[2].encode('utf-8')))

    return last_result
Esempio n. 30
0
def checkin(cookies=None):

    url = 'http://poro.ws/user/checkin'
    response = requests.post(url=url,
                             headers=headers,
                             cookies=cookies,
                             timeout=30)
    j = response.json()

    ret = j['ret']
    msg = j['msg']

    write_log(log=msg)

    return msg
Esempio n. 31
0
def getResult(pre_top, disease, distance):
    """
    得到最终结果
    :param pre_top:前k个医生 [[doc_id,doc_name,hos_name,score]] list
    :param disease: 疾病
    :return:
    """
    global length
    global tt
    global scort  # 排序的所有情况
    global cal
    scort = []
    tt = []
    length = 0
    cal = 0
    pre_top, pz_list = filter_top(pre_top,
                                  distance)  #对pre_top进行过滤(距离过远的舍去),并返回车程信息

    f = log.write_log()
    lt = len(pre_top)
    length = lt
    tt = range(lt)
    hvar(range(lt))  #更新scort,得到排序的所有情况
    s_g = []
    hos_dic = gethosdic(pre_top)  # 各个医院的数量
    for s in scort:
        s_g.append(dh_weight(s, pre_top, hos_dic, pz_list))
    i, grade = getmax(s_g)
    # 输出最终结果
    print >> f, "疾病:", disease
    print >> f, ("%-20s%-20s" % ("姓名", "医院"))
    for doc in scort[i]:
        print >> f, (
            "%-20s%-20s" %
            (pre_top[doc][1].encode('utf-8'), pre_top[doc][2].encode('utf-8')))
Esempio n. 32
0
def getResult(pre_top, disease):
    """
    得到最终结果
    :param pre_top:前k个矩阵 [[doc_id,doc_name,hos_name,score]]
    :param disease: 疾病
    :return:
    """
    global length
    global tt
    global scort  # 排序的所有情况
    f = log.write_log()
    lt = len(pre_top)
    length = lt
    tt = range(lt)
    hvar(range(lt))
    s_g = []
    hos_dic = gethosdic(pre_top, 2)  # 各个医院的数量
    for s in scort:
        s_g.append(dh_weight(s, pre_top, hos_dic))
    i, grade = getmax(s_g)
    # 输出最终结果
    print >> f, "疾病:", disease
    print >> f, ("%-20s%-20s" % ("姓名", "医院"))
    for doc in scort[i]:
        print >> f, (
            "%-20s%-20s" %
            (pre_top[doc][1].encode('utf-8'), pre_top[doc][2].encode('utf-8')))
Esempio n. 33
0
def cookies_from_internet():
    '''网络载入cookies'''
    log = u'load cookies from internet!'
    write_log(log=log)

    cookiesjar = login()
    if cookiesjar == 0:  #登录失败
        return cookiesjar

    elif cookiesjar == None:  #网络故障
        return cookiesjar

    else:  #登录成功
        cookies = cookiesjar_to_dict(cookiesjar=cookiesjar)
        write_cookies(cookies=cookies)
        return cookies
Esempio n. 34
0
async def search(searchterm):
    magnet_links = []
    timeout = aiohttp.ClientTimeout(total=10)
    try:
        async with aiohttp.ClientSession(timeout=timeout) as session:
            async with session.get(
                f"https://kickasstorrents.bz/usearch/{searchterm}/",
                headers={"User-Agent": USER_AGENT},
            ) as resp:
                data = await resp.text()
        soup = BeautifulSoup(data, "lxml")
        trs = soup.find("table", {"class": "data"}).find_all("tr")
        for tr in trs:
            try:
                tds = tr.find_all("td")

                if not tds:
                    continue

                try:
                    seeds = int(tds[3].contents[0])
                except ValueError:
                    seeds = 0

                title = tds[0].find("a", {"class": "cellMainLink"}).contents[0]
                try:
                    title = title.getText()
                except AttributeError:
                    pass
                title = str(title)

                magnet_link = tds[0].find("a", {"title": "Torrent magnet link"})["href"]
                magnet_link = magnet_link[magnet_link.find("magnet") :]
                magnet_link = unquote(magnet_link)
                magnet_link = unquote(magnet_link)
                magnet_link = str(magnet_link)

                if seeds:
                    magnet_links.append(
                        dict(title=title, magnet=magnet_link, seeds=seeds)
                    )
            except Exception as e:
                log.write_log()
    except Exception:
        log.write_log()
    return magnet_links
Esempio n. 35
0
def get_fonts_dict(gm_or_im):
    """ get available font dict (name: path) from imagemagick """

    fonts_dict = {}
    file_font = os.path.join(tempfile.gettempdir(),
                             "fotokilof_" + os.getlogin() + "_fonts_list")
    command = " -list font > "
    result = magick(command, "", file_font, gm_or_im + "convert")
    if result is not None:
        try:
            file = open(file_font, "r")
        except:
            log.write_log("get_fonts_list: cannot read file_font", "E")
        else:
            fonts_name = []
            fonts_path = []
            if gm_or_im == "gm ":
                # GraphicsMagick format
                for line in file:
                    if re.search("\\d$", line) is not None:
                        line = re.findall('^[-a-zA-Z]+', line)
                        fonts_dict[line] = ""
            else:
                # ImageMagick format
                for line in file:
                    if re.search("^[ ]+Font:", line) is not None:
                        line = re.sub('^[ ]+Font:[ ]*', "", line)
                        line = re.sub('\n', "", line)
                        fonts_name.append(line)
                    elif re.search("^[ ]+glyphs:", line) is not None:
                        line = re.sub('^[ ]+glyphs:[ ]*', "", line)
                        line = re.sub('\n', "", line)
                        fonts_path.append(line)

                # conversion two list into dictionary
                fonts_dict = dict(zip(fonts_name, fonts_path))
            file.close()
            try:
                os.remove(file_font)
            except:
                log.write_log("get_fonts_list: cannot remove file_font", "W")

    if fonts_dict is None or len(fonts_dict) == 0:
        fonts_dict["Helvetica"] = ""

    return fonts_dict
def start_sample_matrix(ratio, lambdas, lr, table_name, l):
    MAE = 0
    RMSE = 0
    start_time = time.strftime('%Y-%m-%d-%H-%M-%S',
                               time.localtime(time.time()))
    log.start_log(
        start_time, "../matrix/" + table_name.decode('utf-8') + "/" +
        "MF矩阵分解结果.txt".decode('utf-8'))
    f = log.write_log()

    lc_table_name = 'lc_' + table_name
    tp_table_name = 'tp_' + table_name
    # start预测部分
    # # 得到的矩阵是挖取过值的矩阵
    # C, original_matrix, changed_zero = dm.getMatrix(table_name, ratio)
    C, original_matrix, changed_zero = dm.get_Matrix_from_lc_tp(
        lc_table_name, tp_table_name, ratio, 1)
    # C = np.array(C)
    d = C.shape
    U = np.random.rand(d[0], l)
    V = np.random.rand(d[1], l)
    print "开始矩阵分解"
    matrix, X, Y = simple_mat.matrix_factorization(C, U, V, lambdas, step, lr)
    # 开始验证
    print "开始验证"
    matrix0, pre_or_mat, num = de.norma_matrix(matrix, original_matrix)
    MAE, RMSE = vali.validate(matrix, original_matrix, changed_zero)
    # #end
    # end预测部分
    file_path = "../matrix/" + table_name.decode('utf-8')
    t = str(ratio) + "_" + str(num) + "_" + start_time + ".txt"
    # start将矩阵分解后的矩阵保存
    np.savetxt(file_path + "/matrix_factorization/MF_matrix_factorization_" +
               str(ratio) + ".txt",
               matrix,
               fmt='%.8f')
    # end将矩阵分解后的矩阵保存
    # start将原矩阵经预测填充后的矩阵保存
    np.savetxt(file_path + "/result/MF_pre_" + str(ratio) + ".txt",
               pre_or_mat,
               fmt='%.8f')
    # end 将原矩阵经预测填充后的矩阵保存
    # start将矩阵分解后的矩阵(处理过的,负数变0)保存
    np.savetxt(file_path + "/out/MF_" + t.decode('utf-8'), matrix0, fmt='%.8f')
    # end 将矩阵分解后的矩阵(处理过的,负数变0)保存
    # end
    end_time = time.strftime('%Y-%m-%d-%H-%M-%S', time.localtime(time.time()))
    print >> f, "开始时间:", start_time
    print >> f, "结束时间:", end_time
    # 显示梯度下降情况
    title = 'lr:{0} alpha:{1} beta:{2} step:{3} lambdas:{4} sim:{5}'
    title = title.format(lr, ab[0], ab[1], step, lambdas, simlambda)
    print >> f, "参数:", title
    figure_path = "../matrix/" + table_name.decode(
        'utf-8') + "/figure/MF_" + str(ratio) + "_" + start_time + ".jpg"
    figure.paint1(X, Y, title, figure_path)
    log.close_log()
    return MAE, RMSE
Esempio n. 37
0
def matrix_factorization(C, U, V, lamdas, steps, alpha, beta, simlamda, lr, sk,
                         table_name):
    global X
    global Y
    global asv
    global asu
    global judge_uv
    X = []
    Y = []
    asv = []
    asu = []
    judge_uv = [0, 0]

    # log.start_log()
    f = log.write_log()
    step = 0
    loss1, def_v, all_sim_u, all_sim_v = getloss(C, U, V, alpha, beta, lamdas,
                                                 sk, table_name, simlamda)
    print 'loss1:', loss1
    while step <= steps:
        print "开始"
        # print >> f, 'step:', step
        U, V = gardient(C, U, V, def_v, all_sim_u, all_sim_v, alpha, beta,
                        lamdas, lr)
        loss2, def_v, all_sim_u, all_sim_v = getloss(C, U, V, alpha, beta,
                                                     lamdas, sk, table_name,
                                                     simlamda)
        # 用于显示梯度下降的效果
        if (step > 100):
            X.append(step)
            Y.append(loss2)
        # print "X:", X
        # print "Y:", Y
        # if(step%1000==0 and step>0):
        #     TT = np.dot(U, V.transpose())
        #     print >>f, 'step:', step
        #     print >>f,"loss:",loss2
        #     print >>f, TT
        #     print >>f, '======================================================'
        if (np.abs(loss1 - loss2) < 0.0001 or loss2 < 0.0004):
            print "small"
            break
        print "loss1:", loss1
        # if(loss1<loss2):
        #     break
        loss1 = loss2
        step = step + 1
        print 'loss2:', loss2
        print "steps:", step
    print "steps:", step - 1
    # print '完成!!!!!!!!!!!'
    TT = np.dot(U, V.transpose())
    print >> f, '======================================================'
    print >> f, '迭代次数(step):', step - 1
    print >> f, "loss:", loss2
    # print >> f, TT
    return TT, X, Y, loss2
Esempio n. 38
0
def get_yd_dir():
    base_path = os.path.dirname(__file__)

    try:
        f = open(base_path + '/../config/path_to_screen_yd.txt', 'r')
        path_yd = f.read()
        f.close()
    except OSError:
        log.write_log('File not found: /config/path_to_screen_yd.txt')

        f = open(base_path + '/../config/path_to_screen_yd.txt', 'w')
        path_yd = input(
            "Enter the path to screen in YandexDisk folder(simple: image/screen/): "
        )
        f.write(path_yd)
        f.close()

    return path_yd
Esempio n. 39
0
def terminate_cloudmusic():
    times = 0
    while True:
        try:
            times += 1
            process_list = []
            for i in psutil.pids():
                p = psutil.Process(i)
                if p.name() == 'cloudmusic.exe':
                    process_list.append(p)
            for i in process_list:
                i.terminate()
            break
        except Exception as e:
            if times >= 5:
                showerror('错误', '结束网易云进程出错!错误信息:{}'.format(e))
                write_log(e)
            else:
                continue
Esempio n. 40
0
def search(searchterm):
    magnet_links = []
    try:
        resp = requests.get(
            f"{settings.JACKETT_HOST}/api/v2.0/indexers/all/results?apikey={settings.JACKETT_API_KEY}&Query={searchterm}"
        )
        data = resp.json()
        results = data["Results"]
        for result in results:
            if (result.get("Link")
                    or result.get("MagnetUri")) and result.get("Title"):
                magnet_links.append(
                    dict(seeds=result.get("Seeders", 0),
                         title=result["Title"],
                         magnet=result.get("MagnetUri"),
                         torrent_link=result.get("Link")))
    except Exception:
        log.write_log()
    return magnet_links
Esempio n. 41
0
    def run(self):
        period = 0
        while True:
            self.imap.chec_mail()
            # print(self.imap.get_comand())
            for comand in self.imap.get_comand():
                try:
                    print('-', comand)
                    self.executor.execute(comand)  # return status and comand
                    #self.executor.transfer() # ?? try &&
                except Exception as e:
                    str_exeption = str(type(e)) + str(e.args)
                    write_log(log=self.reply_on_comand(
                        comand['address'], comand['command'], str_exeption))
                else:
                    write_log(log=self.reply_on_comand(
                        comand['address'], comand['command'], 'SUCCESS'))

            period += 1
            time.sleep(25)
Esempio n. 42
0
def create_parents_2c(log):
    survivors = []
    evals = 0
    for initial_parents in range(g.mu):
        survivors.append(gpTree(generate_strategy_tree()))

    for ip in survivors:
        temp = list(survivors)  # creates a copy of the survivor list
        temp.remove(ip)         # and removes the current item so it doesn't battle itself

        fitness = 0
        num_opponents = int(ceil(len(temp)*g.percent))
        for op in g.rand.sample(temp, num_opponents):
            am = generate_agent_mem()
            #fitness += play_2c(am, ip.tree, generate_strategy_tree())
            fitness += play_2c(am, ip.tree, op.tree)
            evals += 1
        ip.fitness = float(fitness)/num_opponents  # assigns fitness to be average fitness over all opponents

    write_log(log, survivors, evals)
    return survivors, evals
Esempio n. 43
0
def magick(cmd, file_in, file_out, command):
    """
    run imagemagick command.
    cmd - command for imagemagick
    file_in - fullname picture for processing
    file_out - fullname output picture
    command:
      convert, mogrify, composite, import - ImageMagick
      gm convert, gm mogrify, gm composite, gm import - GraphicsMagick
    """
    result = None
    if cmd != "":
        if file_in is not None:
            file_in = common.spacja(file_in)
            file_out = common.spacja(file_out)
            command = magick_command(command)
            command = command + " " + file_in  + " " + cmd + file_out
            log.write_log("Execute: " + command, "M")
            try:
                os.system(command)
            except:
                log.write_log("Errot in imagick: " + command, "E")
                result = None
            else:
                result = "OK"
        else:
            log.write_log("imagick: No file for imagick", "W")
            result = None
    else:
        result = None
    return result
Esempio n. 44
0
#!/usr/bin/env python
#encoding:utf-8

import getpass

from course import CourseConnect
from log import write_log

username = '******'
password = getpass.getpass('请输入选课密码:')
connect = CourseConnect(username, password)

course_id = raw_input('请输入选课号:')

while True:
    result = connect.select_course(course_id)
    print(result)
    write_log(result)
Esempio n. 45
0
            is_first = False
        else:
            s += ';'
        s += row[4] + ':' + row[6]
    return s


# Get URP password and connect to the URP.
username = '******'
password = getpass.getpass('请输入URP密码:')
connect = FduConnect(username, password)

exercise = connect.get_exercise()
for x in exercise:
    print x[0] + x[1]
write_log(exercise)
# exercise[0] = exercise[1] = (1, 2)

gpa = connect.get_gpa()
for x in gpa:
    s = ''
    for y in x:
        s += y + '\t'
    print s
write_log(gpa)
# gpa = []

del connect

# Get the fetion password and try to send a message.
fet_user = '******'
 def test_exist_log(self):
     write_log(BASE_DIR)
     self.assertTrue(os.path.isfile("log"))
Esempio n. 47
0
browser = webdriver.Firefox()
url_index = 'https://app-test.jimustock.com/#/login'
browser.get(url_index)
browser.maximize_window()
time.sleep(5)
res_username = browser.find_elements_by_xpath("html/body/div[1]/div/main/div/section/article/validate-input[1]/div/input")
for r_username in res_username:
    r_username.send_keys('18070505645')
res_admin_password = browser.find_elements_by_xpath("html/body/div[1]/div/main/div/section/article/validate-input[2]/div/input")
for r_admin_password in res_admin_password:
    r_admin_password.send_keys('bst52571314.com')
res_admin_post = browser.find_elements_by_xpath("html/body/div[1]/div/main/div/section/article/button")
for r_admin_post in res_admin_post:
    r_admin_post.click()
    print '登录成功'
    write_log('=========================================\nlogin succeed\n=======================================','stock_picture')
#====================================================下一步============================================================================================================================
time.sleep(25)
res_message = browser.find_elements_by_xpath("html/body/div[1]/section/main/main/ng-include[1]/div/div/div[2]/div/div/section[1]/article/div[1]/stock-item[1]/div")
print res_message
for r_message in res_message:
    r_message.click()
    print '指数打开'
    time.sleep(5)
res_title = browser.find_elements_by_css_selector(".stock-detail.ng-scope")
print res_title
print 'yes'
for r_title in res_title:
    print 'gogogo!'
    text = r_title.fill
    print 'ok'
    def free_book_title(self):
        h2_title_book = self.soup.find_all("div", { "class" : "dotd-title" })[0].find('h2')
        return self._clear_element(h2_title_book.string)

    def all_books_user(self):
        try:
            html_user_ebooks = self.session.get('https://www.packtpub.com/account/my-ebooks').text
            my_ebooks = BeautifulSoup(html_user_ebooks, 'html.parser')
            books = my_ebooks.find(id='product-account-list').find_all('div', {'class': 'product-line unseen'})
            return [book['title'].replace(' [eBook]', "") for book in books]
        except:
            raise ValueError("Login or Passowrd is incorrect")


    def link_free_book(self):
        clain_book_input = self.soup.find('div', {'class': 'dotd-main-book-form cf'})
        return clain_book_input.find('a', {'class': 'twelve-days-claim'})['href']

    def claim_free_book(self):
        if self.free_book_title() not in self.all_books_user():
            return self.session.get('https://www.packtpub.com/{link}'.format(link=self.link_free_book())).status_code
        return 200

if __name__ == '__main__':
    packt = PacktFreeLearningCrawler()
    if packt.claim_free_book() == 200:
        write_log(BASE_DIR, title=packt.free_book_title(), status="Added")
    else:
        write_log(BASE_DIR, title=packt.free_book_title(), status="Error in adding the book")
 def test_write_log(self):
     write_log(BASE_DIR, "Python", "Added")
     content_file = open(os.path.join(BASE_DIR, "log")).readlines()
     self.assertIn("Book: Python -- Status: Added", content_file[0])
Esempio n. 50
0
 def list_channels(self):
     self.send("LIST\r\n")
     #log the action
     log.write_log("Requested LIST")