Skip to content

Commit

Permalink
Merge pull request #11 from itzmestar/query2
Browse files Browse the repository at this point in the history
Query2
  • Loading branch information
itzmestar authored Jun 9, 2022
2 parents bdccd5b + 47c3266 commit 30dd554
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 20 deletions.
31 changes: 31 additions & 0 deletions .github/workflows/python-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# This workflows will upload a Python Package using Twine when a release is created
# For more information see: https://help.github.com/en/actions/language-and-framework-guides/using-python-with-github-actions#publishing-to-package-registries

name: Upload Python Package

on:
release:
types: [created]

jobs:
deploy:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: '3.x'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install setuptools wheel twine
- name: Build and publish
env:
TWINE_USERNAME: ${{ secrets.PYPI_USERNAME }}
TWINE_PASSWORD: ${{ secrets.PYPI_PASSWORD }}
run: |
python setup.py sdist bdist_wheel
twine upload dist/*
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ dune.fetch_auth_token()
# fetch query result id using query id
# query id for any query can be found from the url of the query:
# for example:
# https://duneanalytics.com/queries/4494/8769 => 4494
# https://duneanalytics.com/queries/3705/7192 => 3705
# https://duneanalytics.com/queries/3751/7276 => 3751
# https://dune.com/queries/4494/8769 => 4494
# https://dune.com/queries/3705/7192 => 3705
# https://dune.com/queries/3751/7276 => 3751
result_id = dune.query_result_id(query_id=5508)
Expand Down
2 changes: 1 addition & 1 deletion duneanalytics/__version__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
__title__ = 'duneanalytics'
__description__ = 'Unofficial library for Dune Analytics.'
__url__ = 'https://github.com/itzmestar/duneanalytics'
__version__ = '1.1.0'
__version__ = '1.2.0'
__build__ = 0x010001
__author__ = 'Tarique Anwer'
__author_email__ = '[email protected]'
Expand Down
37 changes: 25 additions & 12 deletions duneanalytics/duneanalytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,19 @@
"""This provides the DuneAnalytics class implementation"""

from requests import Session
import logging

# --------- Constants --------- #

BASE_URL = "https://dune.xyz"
BASE_URL = "https://dune.com"
GRAPH_URL = 'https://core-hsr.duneanalytics.com/v1/graphql'

# --------- Constants --------- #
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s : %(levelname)s : %(funcName)-9s : %(message)s'
)
logger = logging.getLogger("dune")


class DuneAnalytics:
Expand Down Expand Up @@ -70,6 +76,8 @@ def login(self):

self.session.post(auth_url, data=form_data)
self.auth_refresh = self.session.cookies.get('auth-refresh')
if self.auth_refresh is None:
logger.warning("Login Failed!")

def fetch_auth_token(self):
"""
Expand All @@ -81,8 +89,10 @@ def fetch_auth_token(self):
response = self.session.post(session_url)
if response.status_code == 200:
self.token = response.json().get('token')
if self.token is None:
logger.warning("Fetching Token Failed!")
else:
print(response.text)
logger.error(response.text)

def query_result_id(self, query_id):
"""
Expand All @@ -93,22 +103,23 @@ def query_result_id(self, query_id):
"""
query_data = {"operationName": "GetResult", "variables": {"query_id": query_id},
"query": "query GetResult($query_id: Int!, $parameters: [Parameter!]) "
"{\n get_result(query_id: $query_id, parameters: $parameters) "
"{\n job_id\n result_id\n __typename\n }\n}\n"
"{\n get_result_v2(query_id: $query_id, parameters: $parameters) "
"{\n job_id\n result_id\n error_id\n __typename\n }\n}\n"
}

self.session.headers.update({'authorization': f'Bearer {self.token}'})

response = self.session.post(GRAPH_URL, json=query_data)
if response.status_code == 200:
data = response.json()
# print(data)
logger.debug(data)
if 'errors' in data:
logger.error(data.get('errors'))
return None
result_id = data.get('data').get('get_result').get('result_id')
result_id = data.get('data').get('get_result_v2').get('result_id')
return result_id
else:
print(response.text)
logger.error(response.text)
return None

def query_result(self, result_id):
Expand All @@ -118,10 +129,12 @@ def query_result(self, result_id):
:return:
"""
query_data = {"operationName": "FindResultDataByResult",
"variables": {"result_id": result_id},
"query": "query FindResultDataByResult($result_id: uuid!) "
"variables": {"result_id": result_id, "error_id": "00000000-0000-0000-0000-000000000000"},
"query": "query FindResultDataByResult($result_id: uuid!, $error_id: uuid!) "
"{\n query_results(where: {id: {_eq: $result_id}}) "
"{\n id\n job_id\n error\n runtime\n generated_at\n columns\n __typename\n }"
"{\n id\n job_id\n runtime\n generated_at\n columns\n __typename\n }"
"\n query_errors(where: {id: {_eq: $error_id}}) {\n id\n job_id\n runtime\n"
" message\n metadata\n type\n generated_at\n __typename\n }\n"
"\n get_result_by_result_id(args: {want_result_id: $result_id}) {\n data\n __typename\n }\n}\n"
}

Expand All @@ -130,8 +143,8 @@ def query_result(self, result_id):
response = self.session.post(GRAPH_URL, json=query_data)
if response.status_code == 200:
data = response.json()
# print(data)
logger.debug(data)
return data
else:
print(response.text)
logger.error(response.text)
return {}
13 changes: 9 additions & 4 deletions tests/test_duneanalytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,21 @@ def test_login(self, dune):

def test_fetch_auth_token(self, dune):
# fetch authentication token
# dune.login()
dune.fetch_auth_token()
assert dune.auth_refresh is not None
assert dune.username is not None
assert dune.password is not None
assert dune.token is not None

@pytest.mark.skip(reason='To be implemented')
# @pytest.mark.skip(reason='To be implemented')
def test_query_result_id(self, dune):
assert False
result_id = dune.query_result_id(query_id=3751)
assert result_id is not None

@pytest.mark.skip(reason='To be implemented')
# @pytest.mark.skip(reason='To be implemented')
def test_query_result(self, dune):
assert False
result_id = dune.query_result_id(query_id=3705)
assert result_id is not None
data = dune.query_result(result_id)
assert data is not None

0 comments on commit 30dd554

Please sign in to comment.