Retrieve data from Tuya sdk - python-3.x

I use that guide to retrieve data from my Fingerbot: https://www.home-assistant.io/integrations/tuya/
I also use that repo to extract the data:
https://github.com/redphx/tuya-local-key-extractor
When I use the extract.py file I get that error:
Exception in thread Thread-1:
Traceback (most recent call last):
openapi <tuya_iot.openapi.TuyaOpenAPI object at 0x7f52fff924c0>
File "/usr/lib/python3.8/threading.py", line 932, in _bootstrap_inner
openmq <TuyaOpenMQ(Thread-1, started 139994399807232)>
self.run()
File "/home/xavier/.local/lib/python3.8/site-packages/tuya_iot/openmq.py", line 158, in run
self.__run_mqtt()
File "/home/xavier/.local/lib/python3.8/site-packages/tuya_iot/openmq.py", line 164, in __run_mqtt
mq_config = self._get_mqtt_config()
File "/home/xavier/.local/lib/python3.8/site-packages/tuya_iot/openmq.py", line 67, in _get_mqtt_config
"uid": self.api.token_info.uid,
AttributeError: 'NoneType' object has no attribute 'uid'
Traceback (most recent call last):
File "./extract.py", line 41, in <module>
device_manager.update_device_list_in_smart_home()
File "/home/xavier/.local/lib/python3.8/site-packages/tuya_iot/device.py", line 239, in update_device_list_in_smart_home
response = self.api.get(f"/v1.0/users/{self.api.token_info.uid}/devices")
AttributeError: 'NoneType' object has no attribute 'uid'
Here is the code I use:
import json
import logging
import os
from config import (
ENDPOINT,
APP,
EMAIL,
PASSWORD,
ACCESS_ID,
ACCESS_KEY,
)
from tuya_iot import (
TuyaOpenAPI,
AuthType,
TuyaOpenMQ,
TuyaDeviceManager,
)
openapi = TuyaOpenAPI(ENDPOINT, ACCESS_ID, ACCESS_KEY, AuthType.SMART_HOME)
openapi.connect(EMAIL, PASSWORD, country_code=84, schema=APP.value)
openmq = TuyaOpenMQ(openapi)
openmq.start()
print('openapi {}'.format(openapi))
print('openmq {}'.format(openmq))
device_manager = TuyaDeviceManager(openapi, openmq)
device_manager.update_device_list_in_smart_home()
devices = []
for tuya_device in device_manager.device_map.values():
device = {
'device_id': tuya_device.id,
'device_name': tuya_device.name,
'product_id': tuya_device.product_id,
'product_name': tuya_device.product_name,
'category': tuya_device.category,
'uuid': tuya_device.uuid,
'local_key': tuya_device.local_key,
}
try:
resp = openapi.get('/v1.0/iot-03/devices/factory-infos?device_ids={}'.format(tuya_device.id))
factory_info = resp['result'][0]
if 'mac' in factory_info:
mac = ':'.join(factory_info['mac'][i:i + 2] for i in range(0, 12, 2))
device['mac_address'] = mac
except Exception as e:
print(e)
devices.append(device)
print(json.dumps(devices, indent=2))
os._exit(0)
I am on Ubuntu 20.04 and I install the python sdk with tha t command:
pip3 install tuya-iot-py-sdk

Related

How can i import keccak with python3?

i am trying to run the following python3 script however i am struggling to get keccak working:
import sha3
import requests
import time
SAFE_ADDRESS = '0x***'
OWNER_PRIVATE_KEY = '****'
DELEGATE_ADDRESS = '****'
TX_SERVICE_BASE_URL = 'https://safe-transaction.rinkeby.gnosis.io'
totp = int(time.time()) // 3600
hash_to_sign = keccak(text=DELEGATE_ADDRESS + str(totp))
account = Account.from_key(OWNER_PRIVATE_KEY)
signature = account.signHash(hash_to_sign)
add_payload = {
"safe": SAFE_ADDRESS,
"delegate": DELEGATE_ADDRESS,
"signature": signature.signature.hex(),
"label": "My new delegate2"
}
add_response = requests.post(f'{TX_SERVICE_BASE_URL}/api/v1/safes/{SAFE_ADDRESS}/delegates/', json=add_payload, headers = {'Content-type': 'application/json'})
print(add_response.text)
print(add_response.status_code)
i get the following error:
Traceback (most recent call last):
File "test.py", line 14, in <module>
hash_to_sign = keccak(text=DELEGATE_ADDRESS + str(totp))
NameError: name 'keccak' is not defined
additionally i have tried to replace import sha3 with from Crypto.Hash import keccak
but this time i get this error:
Traceback (most recent call last):
File "test.py", line 14, in <module>
hash_to_sign = keccak(text=DELEGATE_ADDRESS + str(totp))
TypeError: 'module' object is not callable
how can i fix this?

I want to know how to read the value of headers in SetTask class after configuring tasks = [SetTask]?

from locust import HttpUser, task, between,SequentialTaskSet,TaskSet,User,events
import json
#events.test_start.add_listener
def on_start(**kwargs):
print("A test will start...")
#events.test_stop.add_listener
def on_stop(**kwargs):
print("A test is ending...")
class SetTask(TaskSet):
#task
def getLogDetail(self):
deatil_url = "/auth/online?page=0&size=10&sort=id%2Cdesc"
with self.client.request(method='GET',
url=deatil_url,
headers=self.headers,
name='get log detail') as response:
print(response.text)
class FlaskTask(SequentialTaskSet):
def __init__(self,*args,**kwargs):
super().__init__(*args,**kwargs)
def on_start(self):
res = self.client.request(method='GET', url="/auth/code",name="get code")
uuid = res.json()['uuid']
headers = {
"content-type": "application/json;charset=UTF-8",
}
datda_info = {
"code": "8",
"password": "B0GdcVWB+XtTwsyBjzoRkn8VnSgtPVKpl8mp7AuQ+BTeU030grUkRwmOHXFCjEhKXB7yezBS7dFEJ63ykR2piQ==",
"username": "admin",
"uuid": uuid
}
with self.client.request(method='POST',url="/auth/login", headers=headers, catch_response=True,data=json.dumps(datda_info),name="get token") as response:
self.token = response.json()['token']
if response.status_code == 200:
self.token = response.json()['token']
response.success()
else:
response.failure("get token failed")
self.headers = {
"Authorization": self.token
}
tasks = [SetTask]
#task
def getUserDetail(self):
deatil_url = "/api/dictDetail?dictName=user_status&page=0&size=9999"
with self.client.request(method='GET',
url=deatil_url,
headers=self.headers,
name='get user detail') as response:
print(response.text)
def function_task():
print("This is the function task")
class FlaskUser(HttpUser):
host = 'http://192.168.31.243:8888'
wait_time = between(1,3)
tasks = [FlaskTask,function_task]
I got error:
A test will start...
[2022-03-26 00:04:56,146] wangguilin/INFO/locust.runners: Ramping to 1 users at a rate of 1.00 per second
[2022-03-26 00:04:56,147] wangguilin/INFO/locust.runners: All users spawned: {"FlaskUser": 1} (1 total users)
[2022-03-26 00:04:56,148] wangguilin/ERROR/locust.user.task: function_task() takes 0 positional arguments but 1 was given
Traceback (most recent call last):
File "c:\users\Users\pycharmprojects\testlocust\venv\lib\site-packages\locust\user\task.py", line 319, in run
self.execute_next_task()
File "c:\users\Users\pycharmprojects\testlocust\venv\lib\site-packages\locust\user\task.py", line 344, in execute_next_task
self.execute_task(self._task_queue.pop(0))
File "c:\users\Users\pycharmprojects\testlocust\venv\lib\site-packages\locust\user\task.py", line 457, in execute_task
task(self.user)
TypeError: function_task() takes 0 positional arguments but 1 was given
[2022-03-26 00:04:58,154] wangguilin/ERROR/locust.user.task: 'SetTask' object has no attribute 'headers'
Traceback (most recent call last):
File "c:\users\Users\pycharmprojects\testlocust\venv\lib\site-packages\locust\user\task.py", line 319, in run
self.execute_next_task()
File "c:\users\Users\pycharmprojects\testlocust\venv\lib\site-packages\locust\user\task.py", line 344, in execute_next_task
self.execute_task(self._task_queue.pop(0))
File "c:\users\Users\pycharmprojects\testlocust\venv\lib\site-packages\locust\user\task.py", line 356, in execute_task
task(self)
File "C:\Users\Users\PycharmProjects\testlocust\locustflask.py", line 22, in getLogDetail
headers=self.headers,
AttributeError: 'SetTask' object has no attribute 'headers'
Traceback (most recent call last):
File "c:\users\Users\pycharmprojects\testlocust\venv\lib\site-packages\gevent_ffi\loop.py", line 270, in python_check_callback
def python_check_callback(self, watcher_ptr): # pylint:disable=unused-argument
question:
I want to know how to read the value of headers in SetTask class after configuring tasks = [SetTask]?
my locust version is: 2.8.3
So can parameters be passed in this case?

Flask Deployment to AWS getting permission errno 13

I have working app on the localhost but when I deployed to the AWS it shows several errors.It can receive my sent image but it cant save it. I tried change permissions with chmod,still its not working. This is first time i am deploying flask app,so I dont know much about i My code is as follow:
***modules***
ImageFile.LOAD_TRUNCATED_IMAGES = True
app = flask.Flask(__name__)
#app.route('/', methods = ['GET','POST'])
def hello():
return 'hey there!!!'
#app.route('/imageupload', methods = ['GET','POST'])
def handle_request():
try:
imagefile = flask.request.files['image']
filename = werkzeug.utils.secure_filename(imagefile.filename)
print("\nReceived image file name:" + imagefile.filename)
imagefile.save(filename)
print("hey")
for image in glob.glob('./frames/*.*'):
img_filter(imagefile,image)
json = flask.request.values['Id']
return {"status": "true","message": "Uploaded Successfully", "Id": json }
except:
return {"status" : "false"}
if __name__ == '__main__' :
app.run()
And these are the errors :
Received image file name:1.jpg
ERROR:image_flask:Exception on /imageupload [POST]
Traceback (most recent call last):
File "/home/ubuntu/pic-edit/env/lib/python3.6/site-packages/flask/app.py", line 2446, in ws$
response = self.full_dispatch_request()
File "/home/ubuntu/pic-edit/env/lib/python3.6/site-packages/flask/app.py", line 1951, in fu$
rv = self.handle_user_exception(e)
File "/home/ubuntu/pic-edit/env/lib/python3.6/site-packages/flask/app.py", line 1820, in ha$
reraise(exc_type, exc_value, tb)
File "/home/ubuntu/pic-edit/env/lib/python3.6/site-packages/flask/_compat.py", line 39, in $
raise value
File "/home/ubuntu/pic-edit/env/lib/python3.6/site-packages/flask/app.py", line 1949, in fu$
rv = self.dispatch_request()
File "/home/ubuntu/pic-edit/env/lib/python3.6/site-packages/flask/app.py", line 1935, in di$
return self.view_functions[rule.endpoint](**req.view_args)
File "/var/www/html/pic-edit/image_flask.py", line 26, in handle_request
imagefile.save(f'/home/ubuntu/pic-edit/{filename}')
File "/home/ubuntu/pic-edit/env/lib/python3.6/site-packages/werkzeug/datastructures.py", li$
dst = open(dst, "wb")
PermissionError: [Errno 13] Permission denied: '/home/ubuntu/pic-edit/1.jpg'
you don't have the right permission. Try to open first the folder with chmod -R 700 /home/ubuntu/pic-edit/
Please note open the permissions like this can be dangerous. Rather I would assign the permission to the "Flask" - Running user
Further for investigations under which user are you running the flask app? How do you start the app?
Also you might change the handle func to:
def handle_request():
try:
f = request.files['file']
f.save(secure_filename(f.filename))
json = flask.request.values['Id']
return {"status": "true","message": "Uploaded Successfully", "Id": json }
note you nead to import from flask import request
I got it where I went wrong, that is because of permissions to the folder. I already tried "chmod 755" to it but it did not worked. After that I tried
chmod a+rwx
And that work fluently.

Scrapy error catching in scrapy/middleware.py file: TypeError: __init__() missing 1 required positional argument: 'uri'

I am catching this error while starting a crawl. I have searched for an answer in several forums, and looked at the code in scrapy/middleware.py (came standard with scrapy and I have not altered it) and cannot figure out why I am getting an error.
The scraper is using both an ImagesPipeline and S3FilesStore pipeline for storing a json file and downloaded images directly into different S3 folders. I am using Python 3.6.
Any help is appreciated. The error message and my scraper settings are below, please let me know if anything else would be useful.
Traceback (most recent call last):
File "/Users/user/anaconda/envs/python3/lib/python3.6/site-
packages/twisted/internet/defer.py", line 1386, in _inlineCallbacks
result = g.send(result)
File "/Users/user/anaconda/envs/python3/lib/python3.6/site-
packages/scrapy/crawler.py", line 77, in crawl
self.engine = self._create_engine()
File "/Users/user/anaconda/envs/python3/lib/python3.6/site-
packages/scrapy/crawler.py", line 102, in _create_engine
return ExecutionEngine(self, lambda _: self.stop())
File "/Users/user/anaconda/envs/python3/lib/python3.6/site-
packages/scrapy/core/engine.py", line 70, in __init__
self.scraper = Scraper(crawler)
File "/Users/user/anaconda/envs/python3/lib/python3.6/site-
packages/scrapy/core/scraper.py", line 71, in __init__
self.itemproc = itemproc_cls.from_crawler(crawler)
File "/Users/user/anaconda/envs/python3/lib/python3.6/site-
packages/scrapy/middleware.py", line 58, in from_crawler
return cls.from_settings(crawler.settings, crawler)
File "/Users/user/anaconda/envs/python3/lib/python3.6/site-
packages/scrapy/middleware.py", line 40, in from_settings
mw = mwcls()
TypeError: __init__() missing 1 required positional argument: 'uri'
ITEM_PIPELINES = {
'scrapy.pipelines.files.S3FilesStore': 1,
'scrapy.pipelines.images.ImagesPipeline': 1
}
AWS_ACCESS_KEY_ID = 'xxxxxx'
AWS_SECRET_ACCESS_KEY= 'xxxxxx'
IMAGES_STORE = 's3 path'
FEED_URI = 's3 path'
FEED_FORMAT = 'jsonlines'
FEED_EXPORT_FIELDS = None
FEED_STORE_EMPTY = False
FEED_STORAGES = {}
FEED_STORAGES_BASE = {
'': 'scrapy.extensions.feedexport.FileFeedStorage',
'file': 'scrapy.extensions.feedexport.FileFeedStorage',
'stdout': 'scrapy.extensions.feedexport.StdoutFeedStorage',
's3': 'scrapy.extensions.feedexport.S3FeedStorage',
'ftp': 'scrapy.extensions.feedexport.FTPFeedStorage',
}
FEED_EXPORTERS = {}
FEED_EXPORTERS_BASE = {
'json': 'scrapy.exporters.JsonItemExporter',
'jsonlines': 'scrapy.exporters.JsonLinesItemExporter',
'jl': None,
'csv': None,
'xml': None,
'marshal': None,
'pickle': None,
}

Google Calendar API FileNotFoundError:

I am trying to create events for Google Calendar but am getting a FileNotFoundError for 'client_secret.json'.
from __future__ import print_function
from apiclient.discovery import build
from httplib2 import Http
from oauth2client import file, client, tools
try:
import argparse
flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
except ImportError:
flags = None
SCOPES = 'https://www.googleapis.com/auth/calendar'
store = file.Storage('storage.json')
creds = store.get()
if not creds or creds.invalid:
flow = client.flow_from_clientsecrets('client_secret.json', SCOPES)
creds = tools.run_flow(flow, store, flags) \
if flags else tools.run(flow, store)
CAL = build('calendar', 'v3', http=creds.authorize(Http()))
GMT_OFF = '-07:00' # PDT/MST/GMT-7
EVENT = {
'summary': 'Dinner with friends',
'start': {'dateTime': '2015-09-15T19:00:00%s' % GMT_OFF},
'end': {'dateTime': '2015-09-15T22:00:00%s' % GMT_OFF},
'attendees': [
{'email': 'friend1#example.com'},
{'email': 'friend2#example.com'},
],
}
e = CAL.events().insert(calendarId='primary',
sendNotifications=True, body=EVENT).execute()
print('''*** %r event added:
Start: %s
End: %s''' % (e['summary'].encode('utf-8'),
e['start']['dateTime'], e['end']['dateTime']))
Here is the error:
Warning (from warnings module):
File "C:\Users\bakat\AppData\Local\Programs\Python\Python35-32\lib\site-packages\oauth2client_helpers.py", line 255
warnings.warn(_MISSING_FILE_MESSAGE.format(filename))
UserWarning: Cannot access storage.json: No such file or directory
Traceback (most recent call last):
File "C:\Users\bakat\AppData\Local\Programs\Python\Python35-32\lib\site-packages\oauth2client\clientsecrets.py", line 121, in _loadfile
with open(filename, 'r') as fp:
FileNotFoundError: [Errno 2] No such file or directory: 'client_secret.json'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:/Users/bakat/AppData/Local/Programs/Python/Python35-32/Diet Buddy/Google_Calendar.py", line 16, in
flow = client.flow_from_clientsecrets('client_secret.json', SCOPES)
File "C:\Users\bakat\AppData\Local\Programs\Python\Python35-32\lib\site-packages\oauth2client_helpers.py", line 133, in positional_wrapper
return wrapped(*args, **kwargs)
File "C:\Users\bakat\AppData\Local\Programs\Python\Python35-32\lib\site-packages\oauth2client\client.py", line 2125, in flow_from_clientsecrets
cache=cache)
File "C:\Users\bakat\AppData\Local\Programs\Python\Python35-32\lib\site-packages\oauth2client\clientsecrets.py", line 165, in loadfile
return _loadfile(filename)
File "C:\Users\bakat\AppData\Local\Programs\Python\Python35-32\lib\site-packages\oauth2client\clientsecrets.py", line 125, in _loadfile
exc.strerror, exc.errno)
oauth2client.clientsecrets.InvalidClientSecretsError: ('Error opening file', 'client_secret.json', 'No such file or directory', 2)
I found the problem - you must go to https://developers.google.com/gmail/api/quickstart/python and follow the instructions to download your client_secrets.json.

Resources