I am trying to use python googletrans package, but it produces lower-quality translations than the web interface of Google Translate.
Here is my code:
from googletrans import Translator
translator = Translator()
text = 'Проверим, насколько качественным получается перевод, если пользоваться веб-интерфейсом.'
result = translator.translate(text, src='ru', dest='en')
print(result.text)
The output is:
We check to see how well it turns out the translation, if you use the web interface.
The translation I obtain using the web interface is as follows: Let's check how high-quality the translation is if you use the web interface.
How can this difference be explained and is there anything I can do about it?
According to the docs, it's not actually using the official translate API:
The maximum character limit on a single text is 15k.
Due to limitations of the web version of google translate, this API does not
guarantee that the library would work properly at all times. (so
please use this library if you don’t care about stability.)
If you want to use a stable API, I highly recommend you to use Google’s
official translate API.
https://py-googletrans.readthedocs.io/en/latest/
They link to the official API documentation here: https://cloud.google.com/translate/docs
Related
I have created a twitter bot using the Tweepy API. It works great, it tweets a tweet with a photo attached to it. However, I would like to add "mentions" or "In this photo" #accounts (tags) to the attached image. This is possible on twitter. I have red the Tweepy documentation and searched online, looking for a parameter but I could not find anything. Any suggestion would appriciated!
code snippet:
api = tweepy.API(oauth)
api.update_with_media(filename='screenshot.png', status=masterStatus)
First of all, please do not use update_with_media, as this is a deprecated API. It works for now, but will get no support. The current correct route is the two-step use of media_upload, followed by update_status (you can also add image alt text using create_media_metadata in between those two steps, if you like). The Tweepy 3.9.0 documentation mentions this.
For the main part of the question - the Twitter API itself does not currently support adding people tags to images, so this is also unavailable in Tweepy. If you would like to request this feature in a future version of the Twitter API, you can do so here.
I tried to use some of the services provided by the Google cloud API, followed the tutorial, and after installing the SDK and authorization files, I started calling. But either way, the program gets stuck after it executes, and then it waits, with no results or any errors. Trying to use Java and python is the same thing. How do you troubleshoot this problem now
I use it in China, but I already use the global proxy model. Otherwise I wouldn't be able to download some of the python packages
import six
from google.cloud import language
from google.cloud.language import enums
from google.cloud.language import types
text = 'President Kennedy spoke at the White House.'
client = language.LanguageServiceClient()
if isinstance(text, six.binary_type):
text = text.decode('utf-8')
# Instantiates a plain text document.
document = types.Document(
content=text,
type=enums.Document.Type.PLAIN_TEXT)
# Detects syntax in the document. You can also analyze HTML with:
# document.type == enums.Document.Type.HTML
tokens = client.analyze_syntax(document).tokens
for token in tokens:
part_of_speech_tag = enums.PartOfSpeech.Tag(token.part_of_speech.tag)
print(u'{}: {}'.format(part_of_speech_tag.name,
token.text.content))
No useful information or error messages could be obtained, and the program did not interrupt, waiting for the result to be returned
China blocks many/most Google services (endpoints) from being accessed inside China. If you are doing this at home, this is almost a certainty.
Your best option is to signup with Alibaba Cloud and then use a VM to access Google services. Alibaba's network will "usually" get you thru the great firewall.
What's the most current form of Oauth for Python 3?
I'm trying to create a stock screener using my broker's API, which uses Oauth. Most of the info I find is out of date or conflicting. I've seen the following modules referenced:
Oauth - Seems to be the original, now outdated. I get an error of "'module' object has no attribute 'Consumer'"
Oauth2 - Newer version, apparently also outdated? The one most referenced one online. Glitches out in pip/can't figure out how to install it.
Oauthlib - IIRC, claims to be the new replacement for Oauth and Oauth2
Rauth.OAuth2Service - Also potentially replacement for Oauth and Oauth2?
Requests - ?
Oauth_hook - ?
pyoauth2 - I get an error about not having a module named "client" in pyoauth2's init.
None of them seem to work as expected, and I have a feeling that this is due to low Oauth 3 support. Have you gotten OAuth to work in Python 3? If so, how did you do it?
It looks like Requets_oauthlib works. Here's code I used that works in Python 3. I'm posting it because most of the example code I found used formats that I couldn't get working.
from requests_oauthlib import OAuth1
client_key = ''
client_secret = ''
resource_owner_key = ''
resource_owner_secret = ''
def query(queryurl):
headeroauth = OAuth1(client_key, client_secret, resource_owner_key,
resource_owner_secret, signature_type = 'auth_header')
return requests.get(queryurl, auth = headeroauth)
query('http://website.com')
Author of rauth here: rauth is a client library which currently does not officially support Python 3.
However, we are working on it, and there's an active branch (aptly named "python-3") over at GitHub which works. You're free to use it, but bear in mind that things may change slightly when we officially release support for it later on. With that said, it would be great to have people out in the real world testing it so that we can make changes to accommodate the Python 3 ecosystem.
Also note: oauthlib is not a replacement for rauth and not a client library. It attempts to be a generic solution, much like python-oauth2 was, but it doesn't provide a client, unlike python-oauth2.
I am attempting to read the raw text/content of a Google Doc (just a plain document, not a spreadsheet or presentation) from within a Python script, but so far have had little success.
Here's what I've tried:
import gdata.docs.service
client = gdata.docs.service.DocsService()
client.ClientLogin('email', 'password')
q = gdata.docs.service.DocumentQuery()
q.AddNamedFolder('email', 'Folder Name')
feed = client.Query(q.ToUri())
doc = feed.entry[0] # extract one of the documents
However, this variable doc, which is of type gdata.docs.DocumentListEntry, doesn't seem to contain any content, just meta information about the document.
Am I doing something wrong here? Can somebody point me in the right direction? Thank you!
UPDATE (Mar 2019) Good news! The Google Docs REST API is now available. More info about it from my SO answer to a similar question, but to get you going, here's the official Python "quickstart" sample showing you how to get the title of a Google Doc in plain text.
Both the Apps Script and Drive REST API solutions originally answered below are still valid and are alternate ways to get the contents of a Google Doc. (The Drive API works on both Python 2 & 3, but Apps Script is JavaScript-only.)
Bottom-line: if you want to download the entire Doc in plain text, the Drive API solution is best. If you want to programmatically CRUD different parts of a Doc, then you must use either the Docs API or Apps Script.
(Feb 2017) The code in the OP and the only other answer are both now out-of-date as ClientLogin authentication was deprecated back in 2012(!), and GData APIs are the previous generation of Google APIs. While not all GData APIs have been deprecated, all newer Google APIs do not use the Google Data protocol.
There isn't a REST API available (at this time) for Google Docs documents, although there is an "API-like" service provided by Google Apps Script, the JavaScript-in-the-cloud solution which provides programmatic access to Google Docs (via its DocumentService object), including Docs add-ons.
To read plain text from a Google Doc, considered file-level access, you would use the Google Drive API instead. Examples of using the Drive API:
Exporting a Google Sheet as CSV (blog post)
"Poor man's plain text to PDF" converter (blog post) (*)
(*) - TL;DR: upload plain text file to Drive, import/convert to Google Docs format, then export that Doc as PDF. Post above uses Drive API v2; this follow-up post describes migrating it to Drive API v3, and here's a developer video combining both "poor man's converter" posts.
The solution to the OP is to perform similar operations as what you see in both posts above but ensure you're using the text/plain export MIMEtype. For other import/export formats to/from Drive, see this related question SO answer as well as the downloading files from Drive docs page. Here's some pseudocode that searches for Google Docs documents called "Hello World" in my Drive folder and displays the contents of the first matching file found on-screen (assuming DRIVE is your API service endpoint):
from __future__ import print_function
NAME = 'Hello World'
MIME = 'text/plain'
# using Drive API v3; if using v2, change 'pageSize' to 'maxResults',
# 'name=' to 'title=', and ".get('files')" to ".get('items')"
res = DRIVE.files().list(q="name='%s'" % NAME, pageSize=1).execute().get('files')
if res:
fileID = res[0]['id'] # 1st matching "Hello World" name
res = DRIVE.files().export(fileId=fileID, mimeType=MIME).execute()
if res:
print(res.decode('utf-8')) # decode bytes for Py3; NOP for Py2
If you need more than this, see these videos on how to setup using Google APIs, OAuth2 authorization, and creating a Drive service endpoint to list your Drive files, plus a corresponding blog post for all three.
To learn more about how to use Google APIs with Python in general, check out my blog as well as a variety of Google developer videos (series 1 and series 2) I'm producing.
A DocumentQuery doesn't return you all the documents with their contents—that would take forever. It just returns a list of documents, with metadata about each. (Actually, IIRC you can get a preview page this way, so if your document is only one page that might be enough…)
You then need to download the content in a separate request. The content element has a type (the MIME type) and a src (the URL to the actual data). You can just download that src, and parse it. However, you can override the default type by adding an exportFormat parameter, so you don't need to do any parsing.
See the section Downloading documents and files in the docs, which has an example showing how to download a document and specify a format. (It's in .NET rather than Python, and it uses HTML rather than plain text, but you should be able to figure it out.)
I have read documentation about Windows Live API: http://msdn.microsoft.com/en-us/library/bb463989.aspx
But how can I retrieve contacts from hotmail with python ?
Is there any example ?
Your program will first need to obtain "delegated authentication", for which the Python samples are here.
After that, the interface is REST-like: you only need to HTTP GET the appropriate URI (per the docs, that's '/LiveContacts/contacts' to retrieve all contacts. The REST Schema is documented here. You can make an HTTP GET request in Python with such standard library modules as urllib and urllib2, though the lower-level httplib module is also fine.
For those who are searching for the download link for the library
http://download.microsoft.com/download/6/2/a/62adfe67-6fee-487f-9c3e-911ce5d0bc9d/webauth-python-1.2.tar.gz