获取 macOS 菜单栏应用程序的 SO 信誉值
Getting SO Reputation value for macOS Menu Bar App
如何获取 macOS 菜单栏应用程序的 Whosebug 信誉值?
这是我的代码:
import Cocoa
@NSApplicationMain class AppDelegate: NSObject, NSApplicationDelegate {
var statusBarItem: NSStatusItem!
func applicationDidFinishLaunching(_ aNotification: Notification) {
self.statusBarItem = NSStatusBar.system.statusItem(withLength: CGFloat(NSStatusItem.variableLength))
self.statusBarItem.button?.title = "Rep " + String("28,250")
NSApp.activate(ignoringOtherApps: true)
}
}
您可以使用堆栈交换 api。您只需要传递您的 SO 用户 ID 并解析 JSON 响应:
https://api.stackexchange.com/2.3/users/\(userID)?site=Whosebug
游乐场测试
// MARK: - Root
struct Root: Codable {
let items: [User]
let hasMore: Bool
let quotaMax, quotaRemaining: Int
}
// MARK: - Item
struct User: Codable {
let badgeCounts: BadgeCounts
let accountId: Int
let isEmployee: Bool
let lastModifiedDate,
lastAccessDate,
creationDate: Date
let reputationChangeYear,
reputationChangeQuarter,
reputationChangeMonth,
reputationChangeWeek,
reputationChangeDay,
reputation: Int
let userType: String
let userId: Int
let location, websiteUrl: String
let link: URL
let profileImage: URL
let displayName: String
}
// MARK: - BadgeCounts
struct BadgeCounts: Codable {
let bronze, silver, gold: Int
}
let userID = 2303865
let url = URL(string: "https://api.stackexchange.com/2.3/users/\(userID)?site=Whosebug")!
URLSession.shared.dataTask(with: url) { data, response, error in
guard let data = data else {
print("error:", error ?? "")
return
}
do {
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
decoder.dateDecodingStrategy = .secondsSince1970
if let user = try decoder.decode(Root.self, from: data).items.first {
print("reputation:", user.reputation)
print(user)
}
} catch {
print(error)
}
这将打印:
reputation: 200288
User(badgeCounts: BadgeCounts(bronze: 499, silver: 428, gold: 53), accountId: 2665245, isEmployee: false, lastModifiedDate: 2021-07-09 10:05:01 +0000, lastAccessDate: 2021-07-12 19:57:05 +0000, creationDate: 2013-04-21 07:25:19 +0000, reputationChangeYear: 12800, reputationChangeQuarter: 552, reputationChangeMonth: 552, reputationChangeWeek: 107, reputationChangeDay: 74, reputation: 200288, userType: "registered", userId: 2303865, location: "Brazil", websiteUrl: "", link: "https://whosebug.com/users/2303865/leo-dabus", profileImage: "https://i.stack.imgur.com/varL9.jpg?s=128&g=1", displayName: "Leo Dabus")
如何获取 macOS 菜单栏应用程序的 Whosebug 信誉值?
这是我的代码:
import Cocoa
@NSApplicationMain class AppDelegate: NSObject, NSApplicationDelegate {
var statusBarItem: NSStatusItem!
func applicationDidFinishLaunching(_ aNotification: Notification) {
self.statusBarItem = NSStatusBar.system.statusItem(withLength: CGFloat(NSStatusItem.variableLength))
self.statusBarItem.button?.title = "Rep " + String("28,250")
NSApp.activate(ignoringOtherApps: true)
}
}
您可以使用堆栈交换 api。您只需要传递您的 SO 用户 ID 并解析 JSON 响应:
https://api.stackexchange.com/2.3/users/\(userID)?site=Whosebug
游乐场测试
// MARK: - Root
struct Root: Codable {
let items: [User]
let hasMore: Bool
let quotaMax, quotaRemaining: Int
}
// MARK: - Item
struct User: Codable {
let badgeCounts: BadgeCounts
let accountId: Int
let isEmployee: Bool
let lastModifiedDate,
lastAccessDate,
creationDate: Date
let reputationChangeYear,
reputationChangeQuarter,
reputationChangeMonth,
reputationChangeWeek,
reputationChangeDay,
reputation: Int
let userType: String
let userId: Int
let location, websiteUrl: String
let link: URL
let profileImage: URL
let displayName: String
}
// MARK: - BadgeCounts
struct BadgeCounts: Codable {
let bronze, silver, gold: Int
}
let userID = 2303865
let url = URL(string: "https://api.stackexchange.com/2.3/users/\(userID)?site=Whosebug")!
URLSession.shared.dataTask(with: url) { data, response, error in
guard let data = data else {
print("error:", error ?? "")
return
}
do {
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
decoder.dateDecodingStrategy = .secondsSince1970
if let user = try decoder.decode(Root.self, from: data).items.first {
print("reputation:", user.reputation)
print(user)
}
} catch {
print(error)
}
这将打印:
reputation: 200288
User(badgeCounts: BadgeCounts(bronze: 499, silver: 428, gold: 53), accountId: 2665245, isEmployee: false, lastModifiedDate: 2021-07-09 10:05:01 +0000, lastAccessDate: 2021-07-12 19:57:05 +0000, creationDate: 2013-04-21 07:25:19 +0000, reputationChangeYear: 12800, reputationChangeQuarter: 552, reputationChangeMonth: 552, reputationChangeWeek: 107, reputationChangeDay: 74, reputation: 200288, userType: "registered", userId: 2303865, location: "Brazil", websiteUrl: "", link: "https://whosebug.com/users/2303865/leo-dabus", profileImage: "https://i.stack.imgur.com/varL9.jpg?s=128&g=1", displayName: "Leo Dabus")