My function below is supposed to trigger the sending of a pub/sub message. However, I get a key error. I am not sure why it is throwing this error.
def run(event, context):
pubsub_message = base64.b64decode(event['data']).decode('utf-8')
week = datetime.date.today().isocalendar()[1]
year = datetime.date.today().year
file_name = f"Times week {year}.{week}.xlsx"
file_path = f"/tmp/{file_name}"
data = DataIngestion().get_data(pubsub_message)
data = pd.DataFrame.from_records(data)
data.drop(columns='_type', inplace=True)
for col in data.columns:
if "Open" in col or "Close" in col:
data[col] = pd.to_datetime(data[col], format= '%H:%M', errors="coerce").dt.time
data[col] = data[col].replace(np.nan, 'closed', regex=True)
check_unique(data)
data.to_excel(file_path, index=False, encoding='utf-8')
filetoSend(file_path, file_name, week)
Here is the error:
File "/layers/google.python.pip/pip/lib/python3.8/site-packages/flask/app.py", line 2073, in wsgi_app
response = self.full_dispatch_request()
File "/layers/google.python.pip/pip/lib/python3.8/site-packages/flask/app.py", line 1518, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/layers/google.python.pip/pip/lib/python3.8/site-packages/flask/app.py", line 1516, in full_dispatch_request
rv = self.dispatch_request()
File "/layers/google.python.pip/pip/lib/python3.8/site-packages/flask/app.py", line 1502, in dispatch_request
return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
File "/layers/google.python.pip/pip/lib/python3.8/site-packages/functions_framework/__init__.py", line 171, in view_func
function(data, context)
File "/workspace/main.py", line 92, in run
pubsub_message = base64.b64decode(event['data']).decode('utf-8')
KeyError: 'data'
Related
Hey I've been trying to encrypt some inputted data into a SQLalchemy database. However I keep receiving this error when trying to retrieve the decyrpted data from the database. Any help would be much appreciated.
key = Fernet.generate_key()
fernet = Fernet(key)
Encryption part:
fnam = str(form.first_name.data.lower())
Cfname = bytes(fnam, 'utf-8')
Efname = str(fernet.encrypt(Cfname))
lnam = str(form.last_name.data.lower())
Clname = bytes(lnam,'utf-8')
Elname = str(fernet.encrypt(Clname))
print(Efname)
print(Elname)
appoint.first_name =Efname
appoint.last_name = Elname
Decryption part:
info = user.query.filter_by(id=user.id).first()
finam = info.first_name
lanam = info.last_name
first = str(fernet.decrypt(bytes(finam,'utf-8')))
last = str(fernet.decrypt(bytes(lanam,'utf-8')))
Error message
Traceback (most recent call last):
File "appsec2\venv\lib\site-packages\flask\app.py", line 2095, in __call__
return self.wsgi_app(environ, start_response)
File "appsec2\venv\lib\site-packages\flask\app.py", line 2080, in wsgi_app
response = self.handle_exception(e)
File "appsec2\venv\lib\site-packages\flask\app.py", line 2077, in wsgi_app
response = self.full_dispatch_request()
File "appsec2\venv\lib\site-packages\flask\app.py", line 1525, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Users\txzsp\OneDrive\Documents\Poly sch stuff\Y2, S1\appsec2\venv\lib\site-packages\flask\app.py", line 1523, in full_dispatch_request
rv = self.dispatch_request()
File "appsec2\venv\lib\site-packages\flask\app.py", line 1509, in dispatch_request
return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
File "appsec2\appsec\routes.py", line 908, in retrieveConsultation
first = str(fernet.decrypt(bytes(finam,'utf-8')))
File appsec2\venv\lib\site-packages\cryptography\fernet.py", line 83, in decrypt
timestamp, data = Fernet._get_unverified_token_data(token)
File "appsec2\venv\lib\site-packages\cryptography\fernet.py", line 115, in _get_unverified_token_data
raise InvalidToken
cryptography.fernet.InvalidToken
Hi I am trying to use google cloud text to speech with appengine. When I send API requests to the text to speech servers, I receive a response with an audio file attached. Now I am trying to write this file to my bucket. Here's my code:
def speak(translation, target_lang):
lang_dict = {'ar': 'ar-XA', 'en': 'en-GB', 'cs': 'cs-CZ', 'da': 'da-DK', 'nl': 'nl-NL', 'fi': 'fi-FI', 'fr': 'fr-FR', 'de': 'de-DE', 'el': 'el-GR', 'hi': 'hi-IN', 'hu': 'hu-HU', 'id': 'id-ID', 'it': 'it-IT',
'ja': 'ja-JP', 'ko': 'ko-KR', 'zh': 'cmn-CN', 'no': 'nb-NO', 'pl': 'pl-PL', 'pt': 'pt-BR', 'ru': 'ru-RU', 'sk': 'sk-SK', 'es': 'es-ES', 'sv': 'sv-SE', 'tr': 'tr-TR', 'uk': 'uk-UA', 'vi': 'vi-VN'}
lang_code = ''
for key, val in lang_dict.items():
if target_lang == key:
lang_code = val
synthesis_input = texttospeech.SynthesisInput(text=translation)
voice = texttospeech.VoiceSelectionParams(
language_code=lang_code,
ssml_gender=texttospeech.SsmlVoiceGender.NEUTRAL)
audio_config = texttospeech.AudioConfig(
audio_encoding=texttospeech.AudioEncoding.MP3)
response = ttsClient.synthesize_speech(
input=synthesis_input, voice=voice, audio_config=audio_config
)
bucket = storage_client.bucket("MY_BUCKET_NAME")
blob = bucket.blob("output.mp3")
blob.upload_from_file(response.audio_content)
return " "
I have removed the actual bucket name from this code snippet but everything else is the same. Also, I tried just writing to the local file system instead of using buckets but appengine does not allow it.
EDIT
Here's the error I am receiving:
Traceback (most recent call last):
File "/layers/google.python.pip/pip/lib/python3.9/site-packages/flask/app.py", line 2447, in wsgi_app
response = self.full_dispatch_request()
File "/layers/google.python.pip/pip/lib/python3.9/site-packages/flask/app.py", line 1952, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/layers/google.python.pip/pip/lib/python3.9/site-packages/flask/app.py", line 1821, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/layers/google.python.pip/pip/lib/python3.9/site-packages/flask/_compat.py", line 39, in reraise
raise value
File "/layers/google.python.pip/pip/lib/python3.9/site-packages/flask/app.py", line 1950, in full_dispatch_request
rv = self.dispatch_request()
File "/layers/google.python.pip/pip/lib/python3.9/site-packages/flask/app.py", line 1936, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/srv/main.py", line 80, in hello
speak(translation[0], translation[1])
File "/srv/main.py", line 59, in speak
blob.upload_from_file(response.audio_content)
File "/layers/google.python.pip/pip/lib/python3.9/site-packages/google/cloud/storage/blob.py", line 2343, in upload_from_file
created_json = self._do_upload(
File "/layers/google.python.pip/pip/lib/python3.9/site-packages/google/cloud/storage/blob.py", line 2180, in _do_upload
response = self._do_resumable_upload(
File "/layers/google.python.pip/pip/lib/python3.9/site-packages/google/cloud/storage/blob.py", line 2024, in _do_resumable_upload
upload, transport = self._initiate_resumable_upload(
File "/layers/google.python.pip/pip/lib/python3.9/site-packages/google/cloud/storage/blob.py", line 1911, in _initiate_resumable_upload
upload.initiate(
File "/layers/google.python.pip/pip/lib/python3.9/site-packages/google/resumable_media/requests/upload.py", line 397, in initiate
method, url, payload, headers = self._prepare_initiate_request(
File "/layers/google.python.pip/pip/lib/python3.9/site-packages/google/resumable_media/_upload.py", line 463, in _prepare_initiate_request
if stream.tell() != 0:
AttributeError: 'bytes' object has no attribute 'tell'
Hi I am scraping data from a website and converting text to mp3 but every time it give me this error :
Traceback (most recent call last):
File "/home/awais/.local/lib/python2.7/site-packages/flask/app.py", line 2447, in wsgi_app
response = self.full_dispatch_request()
File "/home/awais/.local/lib/python2.7/site-packages/flask/app.py", line 1952, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/home/awais/.local/lib/python2.7/site-packages/flask/app.py", line 1821, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/home/awais/.local/lib/python2.7/site-packages/flask/app.py", line 1950, in full_dispatch_request
rv = self.dispatch_request()
File "/home/awais/.local/lib/python2.7/site-packages/flask/app.py", line 1936, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/awais/Desktop/sipgateio-incomingcall-python-master/incoming_call/__main__.py", line 93, in handle_on_dtmf
ttmp3.save("bbc.mp3")
File "/home/awais/.local/lib/python2.7/site-packages/gtts/tts.py", line 295, in save
self.write_to_fp(f)
File "/home/awais/.local/lib/python2.7/site-packages/gtts/tts.py", line 272, in write_to_fp
raise gTTSError(tts=self)
gTTSError: Failed to connect. Probable cause: Host 'https://translate.google.en/' is not reachable
This is what my code look like :
URL = requests.get("https://www.bbc.co.uk/news")
soup = BeautifulSoup(URL.text, 'html.parser')
headlines = soup.select(".gs-c-promo-heading__title")
all = ""
for h in headlines:
h=h.text
h = h.encode('ascii', 'ignore').decode('ascii')
all = str(all) + " " + str(h)
print(all)
ttmp3 = gTTS(all, "en")
ttmp3.save("bbc.mp3")
I am stuck here for last 1 day , please help me
Thank you very much guys , I resolved it by changing this parameter :
ttmp3 = gTTS(text=all,lang="en",tld="com")
By putting tld="com" we can fix it
I want to try to make a dataframe from pdfreader, with the column name is isi1. but why do I get an error ValueError: DataFrame constructor not properly called !. what should i do to fix this error. help from anyone when needed for this problem.
this is my code
if request.method == 'POST':
f = request.files['file']
f.save(f.filename)
pdfreader = PyPDF2.PdfFileReader(open('C:/Users/Novilia/PycharmProjects/tesaja/' + f.filename, 'rb'))
from pandas import DataFrame
df1 = DataFrame(pdfreader, columns=['isi1'])
#vect = count_vect.transform(['isi1']).toarray()
df1['label1'] = text_clf.predict(df1['isi1'])
df1.append(['label1'])
hasil = (df1.isi1[df1['label1'] == 'positif'])
len(hasil)
hasil_list = hasil.values.tolist()
stringList = ' '.join([str(item) for item in hasil_list])
hasil_ringkas = stringList
return render_template('result.html', ringkasan = hasil_ringkas)
the error is
ValueError
ValueError: DataFrame constructor not properly called!
Traceback (most recent call last)
File "C:\Users\Novilia\PycharmProjects\tesaja\venv\lib\site-packages\flask\app.py", line 2463, in __call__
return self.wsgi_app(environ, start_response)
File "C:\Users\Novilia\PycharmProjects\tesaja\venv\lib\site-packages\flask\app.py", line 2449, in wsgi_app
response = self.handle_exception(e)
File "C:\Users\Novilia\PycharmProjects\tesaja\venv\lib\site-packages\flask\app.py", line 1866, in handle_exception
reraise(exc_type, exc_value, tb)
File "C:\Users\Novilia\PycharmProjects\tesaja\venv\lib\site-packages\flask\_compat.py", line 39, in reraise
raise value
File "C:\Users\Novilia\PycharmProjects\tesaja\venv\lib\site-packages\flask\app.py", line 2446, in wsgi_app
response = self.full_dispatch_request()
File "C:\Users\Novilia\PycharmProjects\tesaja\venv\lib\site-packages\flask\app.py", line 1951, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Users\Novilia\PycharmProjects\tesaja\venv\lib\site-packages\flask\app.py", line 1820, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "C:\Users\Novilia\PycharmProjects\tesaja\venv\lib\site-packages\flask\_compat.py", line 39, in reraise
raise value
File "C:\Users\Novilia\PycharmProjects\tesaja\venv\lib\site-packages\flask\app.py", line 1949, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Users\Novilia\PycharmProjects\tesaja\venv\lib\site-packages\flask\app.py", line 1935, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "C:\Users\Novilia\PycharmProjects\tesaja\app.py", line 79, in summarize
df1 = DataFrame(pdfreader, columns=['isi1'])
File "C:\Users\Novilia\PycharmProjects\tesaja\venv\lib\site-packages\pandas\core\frame.py", line 509, in __init__
raise ValueError("DataFrame constructor not properly called!")
ValueError: DataFrame constructor not properly called!
From my understanding, I guess you need to iterate over the pages and get the text from each page. can you try the following:
import nltk
pdfreader = PyPDF2.PdfFileReader(open('C:/Users/Novilia/PycharmProjects/tesaja/' + f.filename, 'rb'))
page_contents = [sent for page_no in range(pdfreader.getNumPages())
for sent in nltk.sent_tokenize(pdfreader.getPage(page_no).extractText())]
df1 = DataFrame(page_contents, columns=['isi1'])
Kindly let me know if you find any problem with the code.
I have mysql data like this
id | name | time_stamp |
which time_stamp field contains data in datetime format
So this is the code to export that mysql data to xls :
w = Workbook()
ws = w.add_sheet("Sheet Title") # worksheet name
# Database connection
con = MySQLdb.connect(host='localhost', user='', passwd='', db='dbtest')
cur = con.cursor()
cur.execute("SELECT name, time_stamp FROM log")
# iterate row and col
rowNum = 0
colNum = 0
# Print all to worksheet
for row in cur.fetchall():
ws.write(rowNum, colNum, row)
rowNum =+1
colNum =+1
and this is the results :
Traceback (most recent call last)
File "/usr/share/pyshared/flask/app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File "/usr/share/pyshared/flask/app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/usr/share/pyshared/flask/app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
File "/usr/share/pyshared/flask/app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "/usr/share/pyshared/flask/app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/share/pyshared/flask/app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/usr/share/pyshared/flask/app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/share/pyshared/flask/app.py", line 1461, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/pi/script/app2.py", line 132, in report
ws.write(rowNum, colNum, row)
File "/usr/local/lib/python2.7/dist-packages/xlwt/Worksheet.py", line 1088, in write
self.row(r).write(c, label, style)
File "/usr/local/lib/python2.7/dist-packages/xlwt/Row.py", line 252, in write
self.__rich_text_helper(col, label, style, style_index)
File "/usr/local/lib/python2.7/dist-packages/xlwt/Row.py", line 278, in __rich_text_helper
raise Exception ("Unexpected data type %r" % type(data))
Exception: Unexpected data type <type 'datetime.datetime'>
I notice there's error with datetime format data from the SQL data while trying to export to XLS. Is there a way to export datetime data from SQL to XLS? Already trying several solutions from stackoverflow but to no avail