From 48b6d16e291cec895bfe47040127a4ad617d13d8 Mon Sep 17 00:00:00 2001 From: Not Keion Date: Mon, 1 May 2023 19:37:43 -0600 Subject: [PATCH] Structure, Readme, Simplified API --- .gitattributes | 2 -- .gitignore | 1 + README.md | 6 +++++ Test.py | 10 +++++++++ api_connectors/Interyak.py | 33 ++++++++++++++++++++++++++++ api_connectors/PositionStackAPI.py | 30 +++++++++++++++++++++++++ api_connectors/YakConnection.py | 27 +++++++++++++++++++++++ connection.json.template | 17 ++++++++++++++ client.py => old/client.py | 0 comment.py => old/comment.py | 0 dm_thread.py => old/dm_thread.py | 0 yak.py => old/yak.py | 0 yak_archive.py => old/yak_archive.py | 0 yak_data.py => old/yak_data.py | 0 14 files changed, 124 insertions(+), 2 deletions(-) delete mode 100644 .gitattributes create mode 100644 .gitignore create mode 100644 README.md create mode 100644 Test.py create mode 100644 api_connectors/Interyak.py create mode 100644 api_connectors/PositionStackAPI.py create mode 100644 api_connectors/YakConnection.py create mode 100644 connection.json.template rename client.py => old/client.py (100%) rename comment.py => old/comment.py (100%) rename dm_thread.py => old/dm_thread.py (100%) rename yak.py => old/yak.py (100%) rename yak_archive.py => old/yak_archive.py (100%) rename yak_data.py => old/yak_data.py (100%) diff --git a/.gitattributes b/.gitattributes deleted file mode 100644 index dfe0770..0000000 --- a/.gitattributes +++ /dev/null @@ -1,2 +0,0 @@ -# Auto detect text files and perform LF normalization -* text=auto diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..21158f8 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +connection.json \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..b0e0cec --- /dev/null +++ b/README.md @@ -0,0 +1,6 @@ +# Old YikYak API +Example to Query all yaks at a single location: `Test.py` + +`connection.json` must be completed for the tests to run (CONTAINS API KEY FOR POSITION STACK AND YIKYAK) + +i'm bad at writing docs -- no one will use this anyways. \ No newline at end of file diff --git a/Test.py b/Test.py new file mode 100644 index 0000000..946e552 --- /dev/null +++ b/Test.py @@ -0,0 +1,10 @@ +from api_connectors.PositionStackAPI import PositionStackAPI #The API that can convert landmark / address to coordinate +from api_connectors.Interyak import QueryYaks # One File to contain all endpoints (simplicity) + +Geocoder = PositionStackAPI.PositionStackAPI() + +point = Geocoder.ForwardGeocode("College of Idaho") +data = QueryYaks(point).data + +for d in data: print(d["voteCount"], d["text"]) + diff --git a/api_connectors/Interyak.py b/api_connectors/Interyak.py new file mode 100644 index 0000000..2596494 --- /dev/null +++ b/api_connectors/Interyak.py @@ -0,0 +1,33 @@ +from YakConnection import AccessToken, Connection +import json, requests + +class QueryYaks(Connection): + def __init__(self, Point=(0,0)): + super().__init__() + self.secret = AccessToken() + self.Point = Point + headers = { + 'content-type': 'application/json', + 'accept': '*/*', + 'apollographql-client-version': '3.0.3-3', + 'authorization': str(AccessToken()), + 'accept-language': 'en-US,en;q=0.9', + 'location': 'POINT(0 0)', + 'x-apollo-operation-type': 'query', + 'user-agent': 'Yik%20Yak/3 CFNetwork/1399 Darwin/22.1.0', + 'apollographql-client-name': 'com.yikyak.2-apollo-ios', + 'x-apollo-operation-name': 'Feed', + } + json_data = { + 'operationName': 'Feed', + 'query': 'query Feed($feedType: FeedType, $feedOrder: FeedOrder, $pageLimit: Int, $cursor: String, $point: FixedPointScalar) {\n feed(\n feedType: $feedType\n feedOrder: $feedOrder\n first: $pageLimit\n after: $cursor\n point: $point\n ) {\n __typename\n edges {\n __typename\n node {\n __typename\n id\n videoId\n videoPlaybackDashUrl\n videoPlaybackHlsUrl\n videoDownloadMp4Url\n videoThumbnailUrl\n videoState\n text\n userEmoji\n userColor\n secondaryUserColor\n distance\n geohash\n interestAreas\n createdAt\n commentCount\n voteCount\n isIncognito\n isMine\n isReported\n myVote\n threadId\n isReplyable\n notificationsEnabled\n }\n }\n pageInfo {\n __typename\n endCursor\n hasNextPage\n }\n }\n}', + 'variables': { + 'cursor': None, + 'feedOrder': "HOT", + 'feedType': 'LOCAL', + 'pageLimit': None, + 'point': f'POINT({Point[1]} {Point[0]})', + }, + } + response = requests.post('https://api.yikyak.com/graphql/', headers=headers, json=json_data) + self.data = [d["node"] for d in json.loads(response.text)["data"]["feed"]["edges"]] diff --git a/api_connectors/PositionStackAPI.py b/api_connectors/PositionStackAPI.py new file mode 100644 index 0000000..87e9cde --- /dev/null +++ b/api_connectors/PositionStackAPI.py @@ -0,0 +1,30 @@ +import requests, json, YakConnection + +class PositionStackAPI(YakConnection.Connection): # POSITION STACK AP + def __init__(self): + super().__init__() + self.api_key = self.get_setting("POSITION_STACK_API_KEY") + + def ReverseGeocode(self,Point): #Get Landmark Name From Coordinate + params = { + 'access_key': self.api_key, + 'query': f'{Point[0]},{Point[1]}', + } + data = requests.get('http://api.positionstack.com/v1/reverse', params=params) + data = json.loads(data.text)["data"] + for p in data: + if p['type'] == "address": + return p['name'] + + return data["data"][0]['name'] + + def ForwardGeocode(self, Address): # Get Landmark Name From Address + params = { + 'access_key': self.api_key, + 'query': Address, + 'reigon':'North America', + 'limit':1 + } + data = requests.get('http://api.positionstack.com/v1/forward', params=params) + data = json.loads(data.text)["data"][0] + return (data["latitude"], data["longitude"]) \ No newline at end of file diff --git a/api_connectors/YakConnection.py b/api_connectors/YakConnection.py new file mode 100644 index 0000000..61af3d3 --- /dev/null +++ b/api_connectors/YakConnection.py @@ -0,0 +1,27 @@ +import requests, json, PositionStackAPI + +class Connection: + def __init__(self, config_path): + self.connection_settings = json.load(open(config_path)) + + def get_setting(self, key): + return self.connection_settings[key] + + +class AccessToken(Connection): #Access / Id Token TODO: Implement better solution to retreiving access token + def __init__(self): + super().__init__() + headers = self.get_setting('headers') + + params = { + 'key': self.get_setting("key"), + } + + json_data = { + 'grantType': 'refresh_token', + 'refreshToken': self.get_setting('refresh_token'), + } + + self.response = requests.post('https://securetoken.googleapis.com/v1/token', params=params, headers=headers, json=json_data) + def __str__(self) -> str: + return json.loads(self.response.text)['access_token'] \ No newline at end of file diff --git a/connection.json.template b/connection.json.template new file mode 100644 index 0000000..b20d37f --- /dev/null +++ b/connection.json.template @@ -0,0 +1,17 @@ +{ + "refresh_token":"[REFRESH TOKEN]", + + "headers":{ + "x-client-version": "iOS/FirebaseSDK/9.0.0/FirebaseCore-iOS", + "content-type": "application/json", + "accept": "*/*", + "x-ios-bundle-identifier": "com.yikyak.2", + "user-agent": "FirebaseAuth.iOS/9.0.0 com.yikyak.2/3.0.3 iPhone/16.1.1 hw/iPhone14_6", + "accept-language": "en" + }, + + "key":"[YIKYAK_KEY]", + + "POSITION_STACK_API_KEY":"[POSITION STACK API KEY]" +} +remove this line and save as connection.json \ No newline at end of file diff --git a/client.py b/old/client.py similarity index 100% rename from client.py rename to old/client.py diff --git a/comment.py b/old/comment.py similarity index 100% rename from comment.py rename to old/comment.py diff --git a/dm_thread.py b/old/dm_thread.py similarity index 100% rename from dm_thread.py rename to old/dm_thread.py diff --git a/yak.py b/old/yak.py similarity index 100% rename from yak.py rename to old/yak.py diff --git a/yak_archive.py b/old/yak_archive.py similarity index 100% rename from yak_archive.py rename to old/yak_archive.py diff --git a/yak_data.py b/old/yak_data.py similarity index 100% rename from yak_data.py rename to old/yak_data.py