Using Microsoft Custom Translator in Python - python

I trained a custom model for German - English translation of text from the Life Science Industry.
I wrote a small Python script to test but this throws an error.
Any hints to find a working example (could be any other language)?
import http.client, urllib.parse, uuid, json
subscriptionKey = 'xxxxxxxxxxxxxxx'
host = 'api.cognitive.microsofttranslator.com'
path = '/translate?api-version=3.0'
ToLanguage = "&to=en"
Category = "yyyyyy"
text = 'Klinische Anwendung'
def translate (content, cat):
headers = {
'Ocp-Apim-Subscription-Key': subscriptionKey,
'Content-type': 'application/json',
'X-ClientTraceId': str(uuid.uuid4())
}
conn = http.client.HTTPConnection(host)
conn.request("POST", path + ToLanguage + "&category=" + cat, content, headers)
response = conn.getresponse()
print(response.readlines())
return response.read()
requestBody = [{
'Text' : text,
}]
content = json.dumps(requestBody, ensure_ascii=False).encode('utf-8')
# Translate
result2 = translate(content, Category)
output2 = json.dumps(json.loads(result2), indent=4, ensure_ascii=False)
print('translation with custom model')
print(output2)
Error looks like this:
[b'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">\r\n', b'<html xmlns="http://www.w3.org/1999/xhtml">\r\n', b'<head>\r\n', b'<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/>\r\n', b'<title>404 - File or directory not found.</title>\r\n', b'<style type="text/css">\r\n', b'<!--\r\n', b'body{margin:0;font-size:.7em;font-family:Verdana, Arial, Helvetica, sans-serif;background:#EEEEEE;}\r\n', b'fieldset{padding:0 15px 10px 15px;} \r\n', b'h1{font-size:2.4em;margin:0;color:#FFF;}\r\n', b'h2{font-size:1.7em;margin:0;color:#CC0000;} \r\n', b'h3{font-size:1.2em;margin:10px 0 0 0;color:#000000;} \r\n', b'#header{width:96%;margin:0 0 0 0;padding:6px 2% 6px 2%;font-family:"trebuchet MS", Verdana, sans-serif;color:#FFF;\r\n', b'background-color:#555555;}\r\n', b'#content{margin:0 0 0 2%;position:relative;}\r\n', b'.content-container{background:#FFF;width:96%;margin-top:8px;padding:10px;position:relative;}\r\n', b'-->\r\n', b'</style>\r\n', b'</head>\r\n', b'<body>\r\n', b'<div id="header"><h1>Server Error</h1></div>\r\n', b'<div id="content">\r\n', b' <div class="content-container"><fieldset>\r\n', b' <h2>404 - File or directory not found.</h2>\r\n', b' <h3>The resource you are looking for might have been removed, had its name changed, or is temporarily unavailable.</h3>\r\n', b' </fieldset></div>\r\n', b'</div>\r\n', b'</body>\r\n', b'</html>\r\n']
b''

The message you got is a 404 not found, so your call is not pointing to the right endpoint.
The 1st thing that I have in mind is https:// missing in the beginning of your host value: can you change:
host = 'api.cognitive.microsofttranslator.com'
by
host = 'https://api.cognitive.microsofttranslator.com'
As I'm not used to Python, it may not be sufficient. If it does not work, can you share more details of your variables?
For your information, with Postman, when http is used instead of https, I also got a 404:
Just adding the S, it works:

Related

Roblox Purchasing an item from catalog

I have written a script that should purchase an asset from catalog.
import re
from requests import post, get
cookie = "blablabla"
ID = 1562150
# getting x-csrf-token
token = post("https://auth.roblox.com/v2/logout", cookies={".ROBLOSECURITY": cookie}).headers['X-CSRF-TOKEN']
print(token)
# getting item details
detail_res = get(f"https://www.roblox.com/library/{ID}")
text = detail_res.text
productId = int(get(f"https://api.roblox.com/marketplace/productinfo?assetId={ID}").json()["ProductId"])
expectedPrice = int(re.search("data-expected-price=\"(\d+)\"", text).group(1))
expectedSellerId = int(re.search("data-expected-seller-id=\"(\d+)\"", text).group(1))
headers = {
"x-csrf-token": token,
"content-type": "application/json; charset=UTF-8"
}
data = {
"expectedCurrency": 1,
"expectedPrice": expectedPrice,
"expectedSellerId": expectedSellerId
}
buyres = post(f"https://economy.roblox.com/v1/purchases/products/{productId}", headers=headers,
data=data,
cookies={".ROBLOSECURITY": cookie})
if buyres.status_code == 200:
print("Successfully bought item")
The problem is that it somehow doesn't purchase any item with error 500 (InternalServerError).
Someone told me that if I add json.dumps() to the script it might work.
How to add json.dumps() here (I don't understand it though I read docs) and how to fix this so the script purchases item?
Big thanks to anyone who can help me.
Import the json package.
json.dumps() converts a python dictionary to a json string.
I'm guessing this is what you want.
buyres =
post(f"https://economy.roblox.com/v1/purchases/products/{productId}",
headers=json.dumps(headers),
data=json.dumps(data),
cookies={".ROBLOSECURITY": cookie})
I found the answer finally, I had to do it like this:
dataLoad = json.dumps(data)
buyres = post(f"https://economy.roblox.com/v1/purchases/products/{productId}", headers=headers,
data=dataLoad,
cookies={".ROBLOSECURITY": cookie})

How to show images from s3?

I'm finishing an application by django that generates pdf certificates
it was working great before I pull it into heroke and set up S3 amazom to host static files.
I have an html with the certificate template, and by HTML2PDF I render it to pdf. But it's not showing the background by css, it works just in the tag
Weird that if we opent the image url in s3 amazom it's shown perfectly
here the template css part
<meta charset="utf-8" />
{% load static %}
<style type="text/css">
#page {
size: 1122.52px 1587.4px ;
/*size: A4 landscape;*/
margin: 0cm;
background-image: url({{bg_front}});
height: 1588;
}
</style>
My view:
class ViewPDF(View):
def get(self, request, *args, **kwargs):
data = {}
pdf = True
if kwargs['pk']:
try:
participant = Participant.objects.get(pk=kwargs['pk'])
print(participant.cpf)
if participant.name:
certificate = Certificate.objects.get(pk=participant.certificate.pk)
pathBack = str(certificate.template.template_back.url)
pathFront = str(certificate.template.template_front.url)
print(pathFront)
#
# CONFIGURA OS BACKGROUNDS E TEXTO
#
data['bg_front'] = pathFront
data['bg_back'] = pathBack
setting = certificate.template.settings
start_date = datetime.strftime(certificate.start_date,'%d/%m/%Y')
end_date = datetime.strftime(certificate.start_date,'%d/%m/%Y')
data['text_front'] = setting.replace('<<nome>>',participant.name).replace('<<cpf>>',str(participant.cpf)).replace('<<ch>>',str(certificate.ch)).replace('<<instituicao>>',str(certificate.institution)).replace('<<DataInicio>>',start_date).replace('<<DataFim>>',end_date)
data['cpf'] = participant.cpf
pdf = render_to_pdf('app_certificates/body_front_pdf.html', data)
return HttpResponse(pdf, content_type='application/pdf')
except TypeError as e:
return HttpResponse(e)

How to determine if my Python Requests call to API returns no data

I have a query to an job board API using Python Requests. It then writes to a table, that is included in a web page. Sometimes the request will return no data(if there are no open jobs). If so, I want to write a string to the included file instead of the table. What is the best way to identify a response of no data? Is it as simple as: if response = "", or something along those lines?
Here is my Python code making the API request:
#!/usr/bin/python
import requests
import json
from datetime import datetime
import dateutil.parser
url = "https://data.usajobs.gov/api/Search"
querystring = {"Organization":"LF00","WhoMayApply":"All"}
headers = {
'authorization-key': "ZQbNd1iLrQ+rPN3Rj2Q9gDy2Qpi/3haXSXGuHbP1SRk=",
'user-agent': "jcarroll#fec.gov",
'host': "data.usajobs.gov",
'cache-control': "no-cache",
}
response = requests.request("GET", url, headers=headers, params=querystring)
responses=response.json()
with open('/Users/jcarroll/work/infoweb_branch4/rep_infoweb/trunk/fec_jobs.html', 'w') as jobtable:
jobtable.write("Content-Type: text/html\n\n")
table_head="""<table class="job_table" style="border:#000">
<tbody>
<tr>
<th>Vacancy</th>
<th>Grade</th>
<th>Open Period</th>
<th>Who May Apply</th>
</tr>"""
jobtable.write(table_head)
for i in responses['SearchResult']['SearchResultItems']:
start_date = dateutil.parser.parse(i['MatchedObjectDescriptor']['PositionStartDate'])
end_date = dateutil.parser.parse(i['MatchedObjectDescriptor']['PositionEndDate'])
jobtable.write("<tr><td><strong><a href='" + i['MatchedObjectDescriptor']['PositionURI'] + "'>" + i['MatchedObjectDescriptor']['PositionID'] + ", " + i['MatchedObjectDescriptor']['PositionTitle'] + "</a></strong></td><td>" + i['MatchedObjectDescriptor']['JobGrade'][0]['Code'] + "-" + i['MatchedObjectDescriptor']['UserArea']['Details']['LowGrade']+ " - " + i['MatchedObjectDescriptor']['UserArea']['Details']['HighGrade'] + "</td><td>" + start_date.strftime('%b %d, %Y')+ " - " + end_date.strftime('%b %d, %Y')+ "</td><td>" + i['MatchedObjectDescriptor']['UserArea']['Details']['WhoMayApply']['Name'] + "</td></tr>")
jobtable.write("</tbody></table>")
jobtable.close
You have a couple of options depending on what the response actually is. I assume, case 3 applies best:
# 1. Test if response body contains sth.
if response.text: # body as str
# ...
# body = response.content: # body as bytes, useful for binary data
# 2. Handle error if deserialization fails (because of no text or bad format)
try:
json_data = response.json()
# ...
except ValueError:
# no JSON returned
# 3. check that .json() did NOT return an empty dict/list
if json_data:
# ...
# 4. safeguard against malformed/unexpected data structure
try:
data_point = json_data[some_key][some_index][...][...]
except (KeyError, IndexError, TypeError):
# data does not have the inner structure you expect
# 5. check if data_point is actually something useful (truthy in this example)
if data_point:
# ...
else:
# data_point is falsy ([], {}, None, 0, '', ...)
If your APIs has been written with correct status codes, then
200 means successful response with a body
204 means successful response without body.
In python you can check your requirement as simply as the following
if 204 == response.status_code :
# do something awesome

Image won't load on python-based webserver

I've built a simple web server that gets a request and send a response. So when the server gets an invalid request, like "localhost/not-a-page", the server will send a response with the content of the HTML file "404.html" the webpage should display an image. So far, so good.
But when the 404 page loads up, the page can't find the image. The HTML part is correct and works offline. I've tried to move the image to serval locations, relative to the Python script, relative to the HTML. But it just can't find it. Hi I'm trying to make the server as low-level as I can, I want to learn how servers work. So I'm not using any server-related libraries. I'm using only the socket library of Python.
I'll appreciate any help to resolve this problem without using other libraries,
EDIT
Here is the relevant Python part :
import socket
import threading
import os
default_error_page = """\
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<title>Error response</title>
</head>
<body>
<center>
<h1>Response</h1>
<p>Error code: %(code)d</p>
<p>Message: %(status)s.</p>
</center>
</body>
</html>
"""
default_header_status = "HTTP/1.1 %(code)d %(status)s\r\n"
default_header_content_type = "Content-Type: text/html; charset=utf-8\r\n\r\n"
buffer_size = 1024
def get_page(code):
    page = default_error_page
    if code == 200:
        pass
    else:
        file = open(os.path.dirname(__file__) + "/www/not-found.html", 'r')
        page = file.read()
    return page
class BaseServer:
    server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server_name = ""
    host_name = ""
    host_port = 8000 # default port
    is_shutdown = False
    def __init__(self, name):
        self.server_name = name
    def start_server(self):
        thread = threading.Thread(target=self.run_server(), name=self.server_name)
        thread.start()
    def run_server(self):
        self.server_socket.bind((self.host_name, self.host_port)) # bind to host
        self.server_socket.listen()
        while not self.is_shutdown:
            conn, addr = self.server_socket.accept()
            self.handle_request(conn, addr)
    def handle_request(self, connection, address):
        print(str(address[0]) + " Connected! (port " + str(address[1]) + ")")
        result = self.parse_request(connection.recv(buffer_size))
        if result == 0:
            page = self.parse_response(200)
        else:
            page = self.parse_response(404)
        connection.sendall(bytes(page))
    def parse_request(self, data):
        if len(data) == 0:
            return
        strings = str(bytes(data).decode('utf-8')).split('\r\n')
        command, path, version = strings[0].split()
        print("command - " + command)
        print("path - " + path)
        print("version - " + version)
        status = 1
        if path == "/":
            status = 0
        return status
    def parse_response(self, code):
        status = "ERROR"
        if code == 200:
            status = "OK"
        elif code == 404:
            status = "NOT FOUND"
        base_header = (default_header_status % {'code': code, 'status': status})
        base_content_type = default_header_content_type
        # page = (default_error_page % {'code': code, 'status': status})
        page = str(get_page(code))
        return_string = str(base_header + base_content_type + page).encode('utf-8')
        print(return_string)
        return return_string
def main():
    server = BaseServer("Home Server")
    server.start_server()
if __name__ == "__main__":
    main()
And this is the HTML:
<html>
<head>
<link rel="stylesheet" type="text/css" href="/style/main.css"/>
<style>
*{
padding:0;
margin:0;
}
body{
background-color:#ffe6b3;
}
h1{
margin-top:30px;
background-color:#ffcc66;
font-size:3em;
display:inline-block;
color:#3a0000;
}
p{
margin-top:80px;
font-size:2em;
color:#3a0000;
}
#img404{
background-image:url(../images/404.gif);
width:100%;
height:50%;
background-repeat:no-repeat;
background-position:center 20%;
}
</style>
</head>
<body>
<center>
<div class=top>
<h1>ERROR 404</h1>
</div>
<p>
Sorry, we could not find the page :(
</p>
<div id="img404">
</div>
</center>
</body>
</html>
Sorry if it's not very readable, but I'm on the phone.
Dima.
Don't use relative paths for image like ../images/img.gif. Rather use full url or url relative to the root.
http://localhost/images/img.gif - full url
/images/img.gif - path relative to root url
Figured out my problem.
After I saw the logs, I realized that the browser sends another request, for the image.
And silly me, my code is:
if path ="/":
status = 0
else:
status = 1
So for every request which is not root("/") the server will return 404.
Oops

How do i parse an email into meaningful content(especially text/html part)? - python/pop3

OK. Here's my code:
hotmail = poplib.POP3_SSL("pop3.live.com", 995)
hotmail.user("username")
hotmail.pass_("password")
mail_1= hotmail.retr(1)
message_1 = email.message_from_string("\n".join(mail_1[1]))
for part in message.walk():
print part.get_content_type()
print "\n"
print part.get_payload()
print "----------------------------------------------"
mail_2 = hotmail.retr(2)
message_2 = email.message_from_string("\n".join(mail_2[1]))
for part in message.walk():
print part.get_content_type()
print "\n"
print part.get_payload()
print "----------------------------------------------"
The problem i'm having right now is, for mail_1 which is a hotmail welcome mail, everything works all right especially text/html part, it's correctly formatted. But for mail_2 which is a multipart/alternative message, what i got from text/html is a mess, there's lots of fillers like =0D and 3D and = all over the place, some typical lines look like this:
<td id=3D"middle_column=
_content_cell" style=3D"background-color:#ffffff;=0D
-webkit-border-radius:6=
px; -moz-border-radius:6px; border-radius:6px;=0D
-webkit-box-shadow:0 1p=
x 3px rgba(0,0,0, 0.2);=0D
-moz-box-shadow:0 1px 3=
px rgba(0,0,0, 0.2);=0D
box-shadow:0 1px 3px rg=
ba(0,0,0, 0.2);">=0D
=0D
<span style=3D"font=
-family:'Helvetica Neue', Helvetica, Arial, sans-serif;=0D
font-size:16px; lin=
e-height:22px; color:#424242;">=0D
So what do I do to get a meaningful text/html message body?
You want the quopri module:
import quopri
decoded_text = quopri.decodestring(encoded_text)

Categories

Resources