I am trying to insert timestamp to the json curl get request and publish to pubnub and failing to serialize to right format
#!/usr/bin/python
import requests
import json
import sys
import datetime
from pubnub import Pubnub
now = datetime.datetime.now()
pubnub = Pubnub(
publish_key = "key",
subscribe_key = "my_key")
channel = "my_channel"
payload = {'Postman-Token': 'sometoken', 'title': "pythontest"}
message = requests.get("http://localhost:8080/", data=json.dumps(payload))
print.message.text gives {"code":"200","message":"Success","itemCount":0,"items":[]}
I would like to add time stamp to the above message and would like the result to be like this: {"code":"200","message":"Success",date:"2016-07-31 15:26"}
print now.strftime("%Y-%m-%d %H:%M")
timestamp=str(now.strftime("%Y-%m-%d %H:%M"))
print message.text,now.strftime("%Y-%m-%d %H:%M")
Hello = str(message.text)
pubnub.publish(
channel = channel,
message = (Hello,timestamp))
But, when I publish it to pubnub, the result looks like this.
[u'{"code":"200","message":"Success","itemCount":0,"items":[]}', u'2016-07-31 15:26']
Please help
You get this result because you are sending a tuple containing a dictionary and the timestamp, so obviously it will return the tuple as the response. Try to inserting the timestamp in the message you will send.
See if the below works:
message = json.loads(requests.get("http://localhost:8080/", data=json.dumps(payload)))
message["date"] = timestamp
It seems to fit for what you want.
Related
I am receiving data from a website via websocket however sometimes the websocket turns out to be empty outputting in {}. How can I give out a filter so that I only get the outputs of the data that is not empty. The if data != '{}' function does not work what could i do so that the function is valid?
import websocket
import pprint
import json
while True:
data= ws.fetch(dataset)
if data != '{}':
pprint.pprint(data)
check the length of the data
if len(data)>0
len(someDict) outputs number of items in the dictionary
First you have to change the "While" to "while".
Try this:
if data:
pprint.pprint(data)
I am trying to create a Telegram bot that sends a message at a specific time, 5:30pm. However, the ways a was trying are not correct.
I wanted to trigger send_message regarding to the time and without the necessity of the user to send any /command.
import telebot
import datetime
TOKEN = 'MyToken'
bot = telebot.TeleBot(TOKEN)
#bot.message_handler(commands=['start'])
def send_welcome(message):
message_id=message.chat.id
bot.reply_to(message,"Welcome")
bot.polling()
Until now I was trying to add something like that, of course it is not python but kind of pseudocode just to explain:
if currenttime=17:30
send_message(idchat, "mymessage")
Thank you in advance.
If I understand correctly, you need to check your system time before sending a message, you could use the following code [source]:
from datetime import datetime
now = datetime.now()
current_time = now.strftime("%H:%M:%S")
print("Current Time =", current_time)
To send a message you could use the following code [source]:
def test_send_message():
text = 'CI Test Message'
tb = telebot.TeleBot(TOKEN)
ret_msg = tb.send_message(CHAT_ID, text)
assert ret_msg.message_id
To compare the time, you may use:
if current_time=='17:30:00':
test_send_message()
You can use schedule python library.
First of all import the necessary modules
import schedule
import time
import telegram
Then you have to create a function that will repeat in every x time.
Here the bot will send me message in every 5 seconds.
TOKEN='your_token'
bot = telegram.Bot(token=TOKEN)
def job():
bot.sendMessage(chat_id=<chat_id>, text="Hello")
schedule.every(5).seconds.do(job)
while True:
schedule.run_pending()
time.sleep(1)
For more informations about schedule Read the documentation
I am trying to get live data in Python 2.7.13 from Poloniex through the push API.
I read many posts (including How to connect to poloniex.com websocket api using a python library) and I arrived to the following code:
from autobahn.twisted.wamp import ApplicationSession
from autobahn.twisted.wamp import ApplicationRunner
from twisted.internet.defer import inlineCallbacks
import six
class PoloniexComponent(ApplicationSession):
def onConnect(self):
self.join(self.config.realm)
#inlineCallbacks
def onJoin(self, details):
def onTicker(*args):
print("Ticker event received:", args)
try:
yield self.subscribe(onTicker, 'ticker')
except Exception as e:
print("Could not subscribe to topic:", e)
def main():
runner = ApplicationRunner(six.u("wss://api.poloniex.com"), six.u("realm1"))
runner.run(PoloniexComponent)
if __name__ == "__main__":
main()
Now, when I run the code, it looks like it's running successfully, but I don't know where I am getting the data. I have two questions:
I would really appreciate if someone could walk me through the process of subscribing and getting ticker data, that I will elaborate in python, from step 0: I am running the program on Spyder on Windows. Am I supposed to activate somehow Crossbar?
How do I quit the connection? I simply killed the process with Ctrl+c and now when I try to run it agan, I get the error: ReactorNonRestartable.
I ran into a lot of issues using Poloniex with Python2.7 but finally came to a solution that hopefully helps you.
I found that Poloniex has pulled support for the original WAMP socket endpoint so I would probably stray from this method altogether. Maybe this is the entirety of the answer you need but if not here is an alternate way to get ticker information.
The code that ended up working best for me is actually from the post you linked to above but there was some info regarding currency pair ids I found elsewhere.
import websocket
import thread
import time
import json
def on_message(ws, message):
print(message)
def on_error(ws, error):
print(error)
def on_close(ws):
print("### closed ###")
def on_open(ws):
print("ONOPEN")
def run(*args):
# ws.send(json.dumps({'command':'subscribe','channel':1001}))
ws.send(json.dumps({'command':'subscribe','channel':1002}))
# ws.send(json.dumps({'command':'subscribe','channel':1003}))
# ws.send(json.dumps({'command':'subscribe','channel':'BTC_XMR'}))
while True:
time.sleep(1)
ws.close()
print("thread terminating...")
thread.start_new_thread(run, ())
if __name__ == "__main__":
websocket.enableTrace(True)
ws = websocket.WebSocketApp("wss://api2.poloniex.com/",
on_message = on_message,
on_error = on_error,
on_close = on_close)
ws.on_open = on_open
ws.run_forever()
I commented out the lines that pull data you don't seem to want, but for reference here is some more info from that previous post:
1001 = trollbox (you will get nothing but a heartbeat)
1002 = ticker
1003 = base coin 24h volume stats
1010 = heartbeat
'MARKET_PAIR' = market order books
Now you should get some data that looks something like this:
[121,"2759.99999999","2759.99999999","2758.00000000","0.02184376","12268375.01419869","4495.18724321",0,"2767.80020000","2680.10000000"]]
This is also annoying because the "121" at the beginning is the currency pair id, and this is undocumented and also unanswered in the other stack overflow question referred to here.
However, if you visit this url: https://poloniex.com/public?command=returnTicker it seems the id is shown as the first field, so you could create your own mapping of id->currency pair or parse the data by the ids you want from this.
Alternatively, something as simple as:
import urllib
import urllib2
import json
ret = urllib2.urlopen(urllib2.Request('https://poloniex.com/public?command=returnTicker'))
print json.loads(ret.read())
will return to you the data that you want, but you'll have to put it in a loop to get constantly updating information. Not sure of your needs once the data is received so I will leave the rest up to you.
Hope this helps!
I made, with the help of other posts, the following code to get the latest data using Python 3.x. I hope this helps you:
#TO SAVE THE HISTORICAL DATA (X MINUTES/HOURS) OF EVERY CRYPTOCURRENCY PAIR IN POLONIEX:
from poloniex import Poloniex
import pandas as pd
from time import time
import os
api = Poloniex(jsonNums=float)
#Obtains the pairs of cryptocurrencies traded in poloniex
pairs = [pair for pair in api.returnTicker()]
i = 0
while i < len(pairs):
#Available candle periods: 5min(300), 15min(900), 30min(1800), 2hr(7200), 4hr(14400), and 24hr(86400)
raw = api.returnChartData(pairs[i], period=86400, start=time()-api.YEAR*10)
df = pd.DataFrame(raw)
# adjust dates format and set dates as index
df['date'] = pd.to_datetime(df["date"], unit='s')
df.set_index('date', inplace=True)
# Saves the historical data of every pair in a csv file
path=r'C:\x\y\Desktop\z\folder_name'
df.to_csv(os.path.join(path,r'%s.csv' % pairs[i]))
i += 1
I have the following script:
import boto3
# Get the service resource
sqs = boto3.resource('sqs')
# Get the queue
queue = sqs.get_queue_by_name(QueueName='')
for message in queue.receive_messages():
print(message.body)
# Let the queue know that the message is processed
message.delete()
It returns the following as the message's body:
{"Records":[{"eventVersion":"2.0","eventSource":"aws:s3","awsRegion":"us-west-2","eventTime":"2017-03-03T11:06:25.329Z","eventName":"ObjectCreated:Copy","userIdentity":{"principalId":"AWS:<id>:<lambda_name>"},"requestParameters":{"sourceIPAddress":"54.186.104.49"},"responseElements":{"x-amz-request-id":"8577BEEB91F199BF","x-amz-id-2":"<>="},"s3":{"s3SchemaVersion":"1.0","configurationId":"PutFromSisterBucket","bucket":{"name":"<bucket_name>","ownerIdentity":{"principalId":"<>"},"arn":"arn:aws:s3:::<bucket_nmae>"},"object":{"key":"<object_key>","size":1990,"versionId":"anHi0ukirRiApp4jyoSTz2oVGOejR6tJ","sequencer":"0058B94E3141A83718"}}}]}
How do I get the value of the "key" inside the "object"?
Currently, the entire result is a string. Is there any way I can do without string indexing or regex match?
If the variable message is string you need to load it with json.loads, to parse to dict type in python, so you can use it like a json:
import json
for message in queue.receive_messages():
message_dict = json.loads(message)
record = message_dict["Records"][0]
content = record["s3"]["object"]["key"]
print(content)
Im new to python and trying to insert the date/time into my firebase.put Im unsure of what this should be and would appreciate some help. Ive tried this, strip() and a couple other unsuccessful things. Please help! Thank you
from firebase import firebase
import time
import datetime
whattime = datetime.datetime.now()
firebase = firebase.FirebaseApplication('my_firebase_link',None)
result = firebase.put('/user',str(whattime),'testing')
There are 2 approaches for timestamps in Firebase, the local machine one and Firebase's own timestamp, the following examples demonstrate each one.
Both examples will require to import the following modules:
import urllib.request
import urllib.error
import json
import time #For the first example only
In this example we use the built in time module from Python (the datetime module works the same). The value from time.time() will be stored as a float and you should convert it back into a date object when you read it back from Firebase.
def send_message(message, username):
my_data = dict()
my_data["message"] = message
my_data["username"] = username
my_data["timestamp"] = time.time()
json_data = json.dumps(my_data).encode()
try:
loader = urllib.request.urlopen("https://<YOUR-PROJECT-ID>.firebaseio.com/messages.json", data=json_data)
except urllib.error.URLError as e:
message = json.loads(e.read())
print(message["error"])
else:
print(loader.read())
This example will use Firebase internal timestamp. The value {".sv": "timestamp"} is interpreted by Firebase and it's converted into a timestamp when the request is processed.
def send_message(message, username):
my_data = dict()
my_data["message"] = message
my_data["username"] = username
my_data["timestamp"] = {".sv": "timestamp"}
json_data = json.dumps(my_data).encode()
try:
loader = urllib.request.urlopen("https://<YOUR-PROJECT-ID>.firebaseio.com/messages.json", data=json_data)
except urllib.error.URLError as e:
message = json.loads(e.read())
print(message["error"])
else:
print(loader.read())
This code solved the problem for me:
import firebase_admin
from firebase_admin import credentials
cred = credentials.Certificate("/home/pi/Documents/gpioZero/iot-maison-firebase- adminsdk-a0c7t-666fdeb9bd.json")
firebase_admin.initialize_app(cred)
db = firestore.client()
pir = MotionSensor(4)
led = LED(16)
def motionOn():
doc_ref = db.collection(u'motion').document(u'test1')
dateNow = datetime.datetime.now()
doc_ref.set({u'started': True,u'date': dateNow })
led.on()
print("On")
pir.when_activated=motionOn