Пример #1
0
def main():
    with requests.Session() as session:
        login_response = session.post(LOGIN_URL, data=SUBMIT_DATA)
        login_response.encoding = ENCODING

        session_id = get_session_id(login_response.text)
        codes_url = CODES_URL_FORMAT.format(
            session=session_id,
            name=urlparse.quote(NAME_ENCODED),
            last_name=urlparse.quote(LAST_NAME_ENCODED))
        codes_response = session.get(codes_url)
        codes_response.encoding = ENCODING
        codes_soup = BeautifulSoup(codes_response.text, "html.parser")
        codes_text = remove_extra_whitespace(codes_soup.text)

        values = [
            remove_extra_whitespace(code).split()[0]
            for code in codes_text.split(':')[2:8]
        ]
        keys = [
            'clientPublicAddress', 'clientListeningPort',
            'serverListeningPort', 'echoRequestCode', 'imageRequestCode',
            'soundRequestCode'
        ]
        dict_to_export = {
            key: int(value) if value.isdecimal() else value
            for key, value in zip(keys, values)
        }
        with open('codes.json', 'w') as results_file:
            json.dump(dict_to_export, results_file)

    return 0
Пример #2
0
 def toPathValue(self, obj):
     """Convert a string or object to a path-friendly value
     Args:
         obj -- object or string value
     Returns:
         string -- quoted value
     """
     if type(obj) == list:
         return urlparse.quote(','.join(obj))
     else:
         return urlparse.quote(str(obj))
Пример #3
0
    def send_state_event(self, room_id, event_type, content, state_key=""):
        """Perform /rooms/$room_id/state/$event_type

        Args:
            room_id(str): The room ID to send the state event in.
            event_type(str): The state event type to send.
            content(dict): The JSON content to send.
            state_key(str): Optional. The state key for the event.
        """
        path = ("/rooms/%s/state/%s" %
                (urlparse.quote(room_id), urlparse.quote(event_type)))
        if state_key:
            path += "/%s" % (urlparse.quote(state_key))
        return self._send("PUT", path, content)
Пример #4
0
    def send_state_event(self, room_id, event_type, content, state_key=""):
        """Perform /rooms/$room_id/state/$event_type

        Args:
            room_id(str): The room ID to send the state event in.
            event_type(str): The state event type to send.
            content(dict): The JSON content to send.
            state_key(str): Optional. The state key for the event.
        """
        path = ("/rooms/%s/state/%s" %
            (urlparse.quote(room_id), urlparse.quote(event_type))
        )
        if state_key:
            path += "/%s" % (quote(state_key))
        return self._send("PUT", path, content)
Пример #5
0
    def instance_url(self, instance_id):
        class_url = self.class_url()

        if sys.version_info[0] >= 3:
            return class_url + '/' + urlparse.quote(instance_id.encode('utf8'))

        return class_url + '/' + urllib.quote(instance_id.encode('utf8'))
Пример #6
0
def tweet(public_token, secure_code, message):
    def_message = urlparse.quote(message)
    url = "http://library.123go.it/post.php"
    response = requests.get(url, params={"tk": public_token, "msg": def_message, "auth_token": secure_code})
    if response.status == 200:
        return True
    raise FramanTwitterException
Пример #7
0
def handdle_message(msg):
    text = msg.text
    if "/start" in text:
        bot.sendDocument(chat_id=msg.chat.id,
                         document="BQADBQADBQADPAsZA1QWNplWJQ03Ag")
        helpInfo(msg)
    elif "/echo" in text:
        bot.sendDocument(chat_id=msg.chat.id,
                         document="BQADBQADBQADPAsZA1QWNplWJQ03Ag")
    elif "/greek" in text:
        bot.sendDocument(
            chat_id=msg.chat.id,
            document='AgADBQADv6cxGzwLGQNcPYQn0TV48JzvvTIABG-YPC7ADExKZ3oBAAEC'
        )
    elif "/emoji" in text:
        query = parseCommand(text) or [""]
        results = os.popen("emoji-query {}".format(query[0]), mode='r')
        sendTxtMsg(msg, "".join(results))
    elif "/tex" in text or "/latex" in text:
        query = parseCommand(text) or ["\Psi"]
        latex = urlparse.quote(" ".join(query))
        bot.sendPhoto(
            chat_id=msg.chat.id,
            photo="http://latex.codecogs.com/gif.latex?%5Chuge%20{}".format(
                latex))
    elif "/help" in text:
        helpInfo(msg)
    else:
        helpInfo(msg)
Пример #8
0
 def tostring(self):
     """Assembles a string from the Url."""
     return "gopher://{}{}/{}/{}{}".format(
         self.host,
         "" if self.port == 70 else ":70",
         self.type,
         self.path,
         ("?" + urlparse.quote(self.query)) if self.query else "",
     )
Пример #9
0
    def send_message_event(self, room_id, event_type, content, txn_id=None):
        """Perform /rooms/$room_id/send/$event_type

        Args:
            room_id(str): The room ID to send the message event in.
            event_type(str): The event type to send.
            content(dict): The JSON content to send.
            txn_id(int): Optional. The transaction ID to use.
        """
        if not txn_id:
            txn_id = self.txn_id

        self.txn_id = self.txn_id + 1

        path = ("/rooms/%s/send/%s/%s" %
                (urlparse.quote(room_id), urlparse.quote(event_type),
                 urlparse.quote(str(txn_id))))
        return self._send("PUT", path, content)
Пример #10
0
def scan(profile, smoonURL, gate):
    print(_("Scanning %s for known errata.\n" % smoonURL))
    devices = []
    for VendorID, DeviceID, SubsysVendorID, SubsysDeviceID, Bus, Driver, Type, Description in profile.deviceIter(
    ):
        if VendorID:
            devices.append(
                '%s/%04x/%04x/%04x/%04x' %
                (Bus, int(VendorID or 0), int(DeviceID or 0),
                 int(SubsysVendorID or 0), int(SubsysDeviceID or 0)))
    searchDevices = 'NULLPAGE'
    devices.append('System/%s/%s' % (parse.quote(
        profile.host.systemVendor), parse.quote(profile.host.systemModel)))
    for dev in devices:
        searchDevices = "%s|%s" % (searchDevices, dev)
    scanURL = '%ssmolt-w/api.php' % smoonURL
    scanData = 'action=query&titles=%s&format=json' % searchDevices
    try:
        r = json.load(urlopen(scanURL, bytes(scanData, 'latin1')))
    except ValueError:
        print("Could not wiki for errata!")
        return
    found = []

    for page in r['query']['pages']:
        try:
            if int(page) > 0:
                found.append('\t%swiki/%s' %
                             (smoonURL, r['query']['pages'][page]['title']))
        except KeyError:
            pass

    if found:
        print(_("\tErrata Found!"))
        for f in found:
            print("\t%s" % f)
    else:
        print(
            _("\tNo errata found, if this machine is having issues please go to"
              ))
        print(
            _("\tyour profile and create a wiki page for the device so others can"
              ))
        print(_("\tbenefit"))
Пример #11
0
def rating(profile, smoonURL, gate):
    print("")
    print(_("Current rating for vendor/model."))
    print("")
    scanURL = '%s/client/host_rating?vendor=%s&system=%s' % (
        smoonURL, parse.quote(
            profile.host.systemVendor), parse.quote(profile.host.systemModel))
    r = json.load(urlopen(scanURL))['ratings']
    rating_system = {
        '0': _('Unrated/Unknown'),
        '1': _('Non-working'),
        '2': _('Partially-working'),
        '3': _('Requires 3rd party drivers'),
        '4': _('Works, needs additional configuration'),
        '5': _('Works out of the box')
    }
    print("\tCount\tRating")
    print("\t-----------------")
    for rate in r:
        print("\t%s\t%s" % (r[rate], rating_system[rate]))
Пример #12
0
    def join_room(self, room_id_or_alias):
        """Performs /join/$room_id

        Args:
            room_id_or_alias(str): The room ID or room alias to join.
        """
        if not room_id_or_alias:
            raise MatrixError("No alias or room ID to join.")

        path = "/join/%s" % urlparse.quote(room_id_or_alias)

        return self._send("POST", path)
Пример #13
0
    def generate_oauth_signature(self, params, method, base_url):
        """
            Generate Signature using HMAC algorithm of key/secret.
        """
        oauth_consumer_secret = None
        if params.get('oauth_consumer_key'):
            user = self.get_authorize_user(params['oauth_consumer_key'])
            oauth_consumer_secret = user.consumer_secret + '&'
            if not user:
                return False
            if params.get('for_access_token'):
                verifier = self.env['auth.request.token'].sudo().search([
                    ('request_token', '=', params['oauth_token'])
                ])
                oauth_consumer_secret += verifier.request_token_secret
                del params['for_access_token']
            if params.get('for_request_data'):
                oauth_consumer_secret += user.access_token_secret
                del params['for_request_data']
        if dict(params).get('oauth_signature_method') == 'PLAINTEXT':
            return oauth_consumer_secret

        parse_url = urlparse.urlparse(base_url)
        scheme = parse_url.scheme
        if request.httprequest.environ.get(
                'HTTP_X_FORWARDED_PROTO'
        ) and request.httprequest.environ['HTTP_X_FORWARDED_PROTO'] == 'https':
            scheme = 'https'
        base_url = "%s://%s%s%s" % (
        scheme,
        parse_url.hostname,
        "" if not parse_url.port or {"http":80,"https":443}[parse_url.scheme]\
                == parse_url.port else ":%d" % parse_url.port,
        parse_url.path,)
        base_url = urlparse.quote(base_url).replace('+',
                                                    '%20').replace('/', '%2F')
        for key, val in list(params.items()):
            params[key] = urlparse.unquote(val)
        params = self.normalize_params(params)
        query_string = ''
        query_string = params
        base_str = method + '&' + base_url + '&' + query_string
        _hash = hmac.new(oauth_consumer_secret.encode('utf-8'),
                         base_str.encode('utf-8'), hashlib.sha1).digest()
        _hash = binascii.b2a_base64(_hash).decode('utf-8')
        signature = urlparse.quote_plus(_hash.rstrip('\n'))
        return signature
Пример #14
0
def escape(s):
    """Escape a URL including any /."""
    try:
        return urllib.quote(s.encode('utf-8'), safe='~')
    except AttributeError:
        return urlparse.quote(s.encode('utf-8'), safe='~')
Пример #15
0
def update_file(args):
    input_file = args.input_file
    uniprot_file = args.uniprot_file or os.getcwd(
    ) + "/uniprot_gene_name_with_protein_length.tab"
    output_file = args.output_file or os.path.dirname(
        input_file) + "/output_" + ntpath.basename(input_file)
    source = args.source or "combined"
    genome_nexus_domain = args.genome_nexus_domain or "https://www.genomenexus.org"
    mapping_file = args.mapping_file or os.getcwd() + "/mapping_per_gene.tsv"

    # write files
    input = open(input_file, 'rt')
    output = open(output_file, 'wt')

    for row in input.readlines():
        split_row = row.split(":")
        count = 0
        # find the gene list
        if len(split_row) == 2 and split_row[0] == "gene_list":
            output.write(row)
            genes = split_row[1].split('\t')
            if genes[0] == "":
                genes.pop(0)

            # first check mapping spreadsheet, if gene not found then check Uniprot file.
            if source == "combined":
                # get protein length by gene name from mapping file
                gene_name_to_length_dict = dict()
                get_protein_length_from_mapping_sheet(
                    mapping_file, gene_name_to_length_dict)
                # get protein length from Uniprot
                gene_name_to_length_dict_primary = dict()
                gene_name_to_length_dict_synonym = dict()
                get_protein_length_from_uniprot(
                    uniprot_file, gene_name_to_length_dict_primary,
                    gene_name_to_length_dict_synonym)
                for gene in genes:
                    gene = gene.strip()
                    if gene in gene_name_to_length_dict:
                        count += gene_name_to_length_dict[gene]
                    elif gene in gene_name_to_length_dict_primary:
                        count += gene_name_to_length_dict_primary[gene]
                    elif gene in gene_name_to_length_dict_synonym:
                        count += gene_name_to_length_dict_synonym[gene]
                    else:
                        print(gene + " not found")
                # CDS size is calculated by sum(protein length * 3)
                count = count * 3
            # source is local map
            elif source == "map":
                # get protein length by gene name from mapping file
                gene_name_to_length_dict = dict()
                get_protein_length_from_mapping_sheet(
                    mapping_file, gene_name_to_length_dict)
                for gene in genes:
                    gene = gene.strip()
                    if gene in gene_name_to_length_dict:
                        count += gene_name_to_length_dict[gene]
                    else:
                        print(gene + " not found in mapping file")
                # CDS size is calculated by sum(protein length * 3)
                count = count * 3

            # source is genome nexus
            elif source == "genomenexus":
                for gene in genes:
                    gene = gene.strip()
                    gn_request = genome_nexus_domain + '/ensembl/canonical-transcript/hgnc/' + urlparse.quote(
                        gene, safe='') + '?isoformOverrideSource=uniprot'
                    # get json response from genome nexus
                    raw_gn_response = requests.get(gn_request)
                    gn_response_status = raw_gn_response.status_code
                    if gn_response_status == 200:
                        gn_response = raw_gn_response.json()
                        if 'message' in gn_response:
                            print(gn_response['message'])
                        elif 'proteinLength' in gn_response:
                            count += gn_response['proteinLength']
                        # proteinLength is not in genome nexus response
                        else:
                            print(gene + " does not have protein length")
                    else:
                        print("HTTP Status " + str(gn_response_status) +
                              " for " + gene)
                # CDS size is calculated by sum(protein length * 3)
                count = count * 3

        # source is uniprot
            elif source == "uniprot":
                # get protein length by gene name from uniprot
                gene_name_to_length_dict_primary = dict()
                gene_name_to_length_dict_synonym = dict()
                get_protein_length_from_uniprot(
                    uniprot_file, gene_name_to_length_dict_primary,
                    gene_name_to_length_dict_synonym)
                for gene in genes:
                    gene = gene.strip()
                    if gene in gene_name_to_length_dict_primary:
                        count += gene_name_to_length_dict_primary[gene]
                    elif gene in gene_name_to_length_dict_synonym:
                        count += gene_name_to_length_dict_synonym[gene]
                    else:
                        print(gene + " not found in UniProt file")
                # CDS size is calculated by sum(protein length * 3)
                count = count * 3

            # write number_of_profiled_coding_base_pairs to output file
            if not genes[-1].endswith("\n"):
                output.write("\n")
            output.write("number_of_profiled_coding_base_pairs: " + str(count))

        # for files already have number_of_profiled_coding_base_pairs, ignore the existing value
        elif len(split_row) == 2 and split_row[
                0] == "number_of_profiled_coding_base_pairs":
            pass
        else:
            output.write(row)
    input.close()
    output.close()
Пример #16
0
 def tostring(self):
     """Assembles a string from the Url."""
     return "gopher://{}{}/{}/{}{}".format(
         self.host, "" if self.port == 70 else ":70", self.type, self.path,
         ("?" + urlparse.quote(self.query)) if self.query else "")
Пример #17
0
 def escape(s):
     """Escape a URL including any /."""
     return urlparse.quote(s, safe='~')
Пример #18
0
def canonurl(url):
    # turn string into unicode
    if sys.version[0] == '2':
        if not isinstance(url, unicode):
            url = url.decode('utf8')

    # parse it
        parsed = urlparse.urlsplit(url)

        # divide the netloc further
        userpass, at, hostport = parsed.netloc.rpartition('@')
        user, colon1, pass_ = userpass.partition(':')
        host, colon2, port = hostport.partition(':')

        # encode each component
        scheme = parsed.scheme.encode('utf8')
        user = urllib.quote(user.encode('utf8'))
        colon1 = colon1.encode('utf8')
        pass_ = urllib.quote(pass_.encode('utf8'))
        at = at.encode('utf8')
        host = host.encode('idna')
        colon2 = colon2.encode('utf8')
        port = port.encode('utf8')
        path = '/'.join(  # could be encoded slashes!
            pce.encode('idna') if i == 0 else pce
            for i, pce in enumerate(parsed.path.split('/')))
        query = urllib.quote(
            urllib.unquote(parsed.query).encode('utf8'), '=&?/')
        fragment = urllib.quote(urllib.unquote(parsed.fragment).encode('utf8'))

        # put it back together
        netloc = ''.join((user, colon1, pass_, at, host, colon2, port))
        return urlparse.urlunsplit((scheme, netloc, path, query, fragment))

    else:
        # parse it
        parsed = urlparse.urlsplit(url)

        # divide the netloc further
        userpass, at, hostport = parsed.netloc.rpartition('@')
        user, colon1, pass_ = userpass.partition(':')
        host, colon2, port = hostport.partition(':')

        # encode each component
        scheme = parsed.scheme
        user = urlparse.quote(user)
        pass_ = urlparse.quote(pass_)

        host = host.encode('idna').decode('utf-8')

        path = '/'.join(  # could be encoded slashes!
            pce.encode('idna').decode('utf-8') if i == 0 else pce
            for i, pce in enumerate(parsed.path.split('/')))

        query = urlparse.quote(
            urlparse.unquote(parsed.query).encode('utf8'), '=&?/')
        fragment = urlparse.quote(
            urlparse.unquote(parsed.fragment).encode('utf8'))

        # put it back together
        netloc = ''.join((user, colon1, pass_, at, host, colon2, port))
        return urlparse.urlunsplit((scheme, netloc, path, query, fragment))