GoogleTrans API Error - Expecting value: line 1 column 1 (char 0)
This is what I had to do to bypass their API call restriction... I use a VPN, specifically Nord-Vpn, so to do it the way I did you would need to be able to connect/disconnect from/to a VPN through the terminal...
def translate_text(text, dest_language="en"):
# Used to translate using the googletrans library
import json
translator = googletrans.Translator()
try:
translation = translator.translate(text=text, dest=dest_language)
except json.decoder.JSONDecodeError:
# api call restriction
process = subprocess.Popen(["nordvpn", "d"], stdout=subprocess.PIPE)
process.wait()
process = subprocess.Popen(["nordvpn", "c", "canada"], stdout=subprocess.PIPE)
process.wait()
return Process_Data.translate_text(text=text, dest_language=dest_language)
return translation
I kind of figured out the problem. I think that this is about Google API's request limit.
I solved this by reinitializing the translator API on every iteration:
import copy
from googletrans import Translator
translatedList = []
for index, row in df.iterrows():
# REINITIALIZE THE API
translator = Translator()
newrow = copy.deepcopy(row)
try:
# translate the 'text' column
translated = translator.translate(row['text'], dest='en')
newrow['translated'] = translated.text
except Exception as e:
print(str(e))
continue
translatedList.append(newrow)