Skip to content

Instantly share code, notes, and snippets.

@divyasshree-BQ
Created July 19, 2024 15:50
Show Gist options
  • Select an option

  • Save divyasshree-BQ/84bc875bf1c3c3e9088653d5c9b5d7eb to your computer and use it in GitHub Desktop.

Select an option

Save divyasshree-BQ/84bc875bf1c3c3e9088653d5c9b5d7eb to your computer and use it in GitHub Desktop.
import asyncio
import pandas as pd
from gql import Client, gql
from gql.transport.websockets import WebsocketsTransport
async def run_subscription():
# Setup WebSocket connection
transport = WebsocketsTransport(
url="wss://streaming.bitquery.io/eap?token=your_token",
headers={"Sec-WebSocket-Protocol": "graphql-ws"})
# Establish the connection
await transport.connect()
print("Connected to WebSocket")
try:
general_df = pd.DataFrame()
print("General Table")
while True:
async for result in transport.subscribe(
gql("""
subscription {
Solana {
General: DEXTradeByTokens {
Block {
Time
}
Trade {
Amount
Price
Currency {
Symbol
Name
}
Side {
Amount
Currency {
Symbol
Name
MetadataAddress
}
}
Dex {
ProgramAddress
ProtocolFamily
ProtocolName
}
Market {
MarketAddress
}
Order {
LimitAmount
LimitPrice
OrderId
}
PriceInUSD
}
}
}
}
""")):
if result.data:
new_data = pd.json_normalize(
result.data['Solana']['General'])
general_df = pd.concat([general_df, new_data],
ignore_index=True)
print(general_df)
finally:
await transport.close()
def main():
asyncio.run(run_subscription())
if __name__ == "__main__":
main()
@sumittale5654

Copy link
Copy Markdown

this is giving me the bad request error

@divyasshree-BQ

Copy link
Copy Markdown
Author

@sumittale5654 please add your token.

@mccccccmike

Copy link
Copy Markdown

python test.py
Traceback (most recent call last):
File "test.py", line 78, in
main()
File "test.py", line 74, in main
asyncio.run(run_subscription())
File "/usr/lib/python3.8/asyncio/runners.py", line 44, in run
return loop.run_until_complete(main)
File "/usr/lib/python3.8/asyncio/base_events.py", line 616, in run_until_complete
return future.result()
File "test.py", line 14, in run_subscription
await transport.connect()
File "/usr/local/lib/python3.8/dist-packages/gql/transport/websockets_base.py", line 490, in connect
self.websocket = await asyncio.wait_for(
File "/usr/lib/python3.8/asyncio/tasks.py", line 494, in wait_for
return fut.result()
File "/usr/lib/python3.8/asyncio/tasks.py", line 695, in _wrap_awaitable
return (yield from awaitable.await())
File "/usr/local/lib/python3.8/dist-packages/websockets/legacy/client.py", line 647, in await_impl_timeout
return await self.await_impl()
File "/usr/local/lib/python3.8/dist-packages/websockets/legacy/client.py", line 654, in await_impl
await protocol.handshake(
File "/usr/local/lib/python3.8/dist-packages/websockets/legacy/client.py", line 325, in handshake
raise InvalidStatusCode(status_code, response_headers)
websockets.exceptions.InvalidStatusCode: server rejected WebSocket connection: HTTP 401

Please help I got HTTP 401

@divyasshree-BQ

Copy link
Copy Markdown
Author

@mccccccmike either your free plan is over or you haven't used your OAuth token. Check https://docs.bitquery.io/docs/authorisation/how-to-generate/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment