Skip to content

Instantly share code, notes, and snippets.

@qoli
Last active October 25, 2022 02:56
Show Gist options
  • Save qoli/74f97b385623edea37a323c23b19fdae to your computer and use it in GitHub Desktop.
Save qoli/74f97b385623edea37a323c23b19fdae to your computer and use it in GitHub Desktop.
opensea
//
// ContentView.swift
// testOS
//
// Created by 黃佁媛 on 2022/10/25.
//
import Alamofire
import SwiftUI
struct ContentView: View {
var body: some View {
VStack {
Image(systemName: "globe")
.imageScale(.large)
.foregroundColor(.accentColor)
Text("Hello, world!")
}
.padding()
.onAppear {
getIP()
sendRequest()
}
}
func getIP() {
/* Configure session, choose between:
* defaultSessionConfiguration
* ephemeralSessionConfiguration
* backgroundSessionConfigurationWithIdentifier:
And set session-wide properties, such as: HTTPAdditionalHeaders,
HTTPCookieAcceptPolicy, requestCachePolicy or timeoutIntervalForRequest.
*/
let sessionConfig = URLSessionConfiguration.default
/* Create session, and optionally set a URLSessionDelegate. */
let session = URLSession(configuration: sessionConfig, delegate: nil, delegateQueue: nil)
/* Create the Request:
getIP (GET http://icanhazip.com/)
*/
guard var URL = URL(string: "https://icanhazip.com/") else { return }
var request = URLRequest(url: URL)
request.httpMethod = "GET"
// Headers
request.addValue("__cf_bm=9PDNM0kTY9ZQq.HsbMe6f.FHPE1wKeai5BxX83e8QCs-1666666146-0-AQyMemQSRF3dzwxCDmB+xSSEWDsRLa8a18mqF7gszmcp6IPvk2h/60RMCb3grSEHCn4MhEdQSbbpKiNpu1BrvjE=", forHTTPHeaderField: "Cookie")
/* Start a new Task */
let task = session.dataTask(with: request, completionHandler: { (data: Data?, response: URLResponse?, error: Error?) in
if error == nil {
// Success
let statusCode = (response as! HTTPURLResponse).statusCode
print("URL Session Task Succeeded: HTTP \(statusCode)")
} else {
// Failure
print("URL Session Task Failed: %@", error!.localizedDescription)
}
do {
if let data = data, let string = String(bytes: data, encoding: .utf8) {
print(string)
} else {
print("null")
}
} catch {
print(error)
}
})
task.resume()
session.finishTasksAndInvalidate()
}
func sendRequest() {
/* Configure session, choose between:
* defaultSessionConfiguration
* ephemeralSessionConfiguration
* backgroundSessionConfigurationWithIdentifier:
And set session-wide properties, such as: HTTPAdditionalHeaders,
HTTPCookieAcceptPolicy, requestCachePolicy or timeoutIntervalForRequest.
*/
let sessionConfig = URLSessionConfiguration.default
/* Create session, and optionally set a URLSessionDelegate. */
let session = URLSession(configuration: sessionConfig, delegate: nil, delegateQueue: nil)
/* Create the Request:
Request (5) (POST https://opensea.io/__api/graphql/)
*/
guard let URL = URL(string: "https://opensea.io/__api/graphql/") else { return }
var request = URLRequest(url: URL)
request.httpMethod = "POST"
// Headers
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.0 Safari/605.1.15", forHTTPHeaderField: "User-Agent")
request.addValue("amp_ddd6ec=4XSeVsKbmVVHy_CTWcPGNs.MHg5MzJlYzA0ZTM4MDZiNmJhYzBlNDdiYTk0N2I1NWJjMDIzMTA1YmVj..1gg6g34h0.1gg6g411s.4u.1d.6b; theme={%22theme%22:%22dark%22}; opensea_logged_out=false; __os_session=eyJpZCI6Ijk0NGJmNTVhLTFlYmYtNGUxYS1iOTlhLWJhNzI0MTA1ODM0NiJ9; __os_session.sig=UKmbHBja0yr4DPcqkWHnh3YSs-_5GX4W6L5PodVxy-Q; _ga_9VSBF2K4BX=GS1.1.1666665527.3.1.1666665546.0.0.0; _ga=GA1.1.1260328999.1655461485; dpr=2; _gcl_au=1.1.285233173.1666607231; _gid=GA1.2.483064963.1666607235; __os_sessionId=eyJpZCI6IjRmMGE2YmM4LWU2YzMtNGQxZS1iZTNmLTE5NDcwOGQ3YjYzOSJ9; __os_sessionId.sig=S4lTEZYlcBGqQ-mnW1G3ezd_Cze47OtnpPLNkgb-660; __cf_bm=mMvrzje2iWtimEwin_K2aZFF1fEV3pZKvtZB5GhDtW8-1666665521-0-ASYDCfhktsbAcAojcI2oXhdoWxDLEQQKKCarlazNNr5lLhTahN5QXQPs2wRETw7hfh8JpHohPxo3lj5L1fLGCXk=; _dd_s=rum=0&expire=1666666444351; _gat_gtag_UA_111688253_1=1; _gat_UA-111688253-1=1; _uetsid=6b81c320538611edbaa94d7e29830453; _uetvid=6b8201d0538611eda4de211f117606aa", forHTTPHeaderField: "Cookie")
request.addValue("4c578ad5abb64c3806c94e11edd134d718c0be73d27d82d7dd9b2a1644144d39", forHTTPHeaderField: "x-signed-query")
// JSON Body
let bodyObject: [String: Any] = [
"id": "RankingsPageTopPaginationQuery",
"query": "query RankingsPageTopPaginationQuery(\n $chain: [ChainScalar!]\n $count: Int\n $createdAfter: DateTime\n $cursor: String\n $includeAllTimeStats: Boolean!\n $includeOneDayStats: Boolean!\n $includeOneHourStats: Boolean!\n $includeSevenDayStats: Boolean!\n $includeSixHourStats: Boolean!\n $includeThirtyDayStats: Boolean!\n $parents: [CollectionSlug!]\n $sortBy: CollectionSort\n) {\n ...RankingsPageTop_data\n}\n\nfragment RankingsPageTop_data on Query {\n rankings(after: $cursor, chains: $chain, first: $count, sortBy: $sortBy, parents: $parents, createdAfter: $createdAfter) {\n edges {\n node {\n createdDate\n name\n slug\n logo\n isVerified\n relayId\n ...StatsV2CollectionCell_collection\n ...collection_url\n nativePaymentAsset {\n ...StatsV2TablePrice_logo\n id\n }\n statsV2 {\n floorPrice {\n unit\n eth\n }\n numOwners\n totalSupply\n totalQuantity\n totalListed\n }\n statsV2 @include(if: $includeOneHourStats) {\n oneHourSales\n oneHourChange\n oneHourVolume {\n unit\n eth\n }\n }\n statsV2 @include(if: $includeSixHourStats) {\n sixHourSales\n sixHourChange\n sixHourVolume {\n unit\n eth\n }\n }\n statsV2 @include(if: $includeOneDayStats) {\n oneDaySales\n oneDayChange\n oneDayVolume {\n unit\n eth\n }\n }\n statsV2 @include(if: $includeSevenDayStats) {\n sevenDaySales\n sevenDayChange\n sevenDayVolume {\n unit\n eth\n }\n }\n statsV2 @include(if: $includeThirtyDayStats) {\n thirtyDaySales\n thirtyDayChange\n thirtyDayVolume {\n unit\n eth\n }\n }\n statsV2 @include(if: $includeAllTimeStats) {\n totalSales\n totalVolume {\n unit\n eth\n }\n }\n id\n __typename\n }\n cursor\n }\n pageInfo {\n endCursor\n hasNextPage\n }\n }\n}\n\nfragment StatsV2CollectionCell_collection on CollectionType {\n name\n imageUrl\n isVerified\n slug\n}\n\nfragment StatsV2TablePrice_logo on PaymentAssetType {\n symbol\n}\n\nfragment collection_url on CollectionType {\n slug\n isCategory\n}\n",
"variables": [
"chain": NSNull(),
"parents": NSNull(),
"includeOneDayStats": true,
"count": 900,
"includeAllTimeStats": false,
"cursor": "YXJyYXljb25uZWN0aW9uOjk5",
"includeSevenDayStats": false,
"includeOneHourStats": false,
"includeSixHourStats": false,
"createdAfter": NSNull(),
"includeThirtyDayStats": false,
"sortBy": "ONE_DAY_VOLUME",
],
]
request.httpBody = try! JSONSerialization.data(withJSONObject: bodyObject, options: [])
/* Start a new Task */
let task = session.dataTask(with: request, completionHandler: { (data: Data?, response: URLResponse?, error: Error?) in
if error == nil {
// Success
let statusCode = (response as! HTTPURLResponse).statusCode
print("URL Session Task Succeeded: HTTP \(statusCode)")
} else {
// Failure
print("URL Session Task Failed: %@", error!.localizedDescription)
}
do {
if let data = data {
let json = try JSONSerialization.jsonObject(with: data, options: [.allowFragments])
print(json)
} else {
print("null")
}
} catch {
print(error)
}
})
task.resume()
session.finishTasksAndInvalidate()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment