Skip to content

Commit

Permalink
switched to using Twitter's API & added ጰ subscriber symbol
Browse files Browse the repository at this point in the history
  • Loading branch information
Darragh Rogan committed Jul 18, 2020
1 parent e5ede34 commit 925fe29
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 44 deletions.
48 changes: 31 additions & 17 deletions Per mille/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ class AppDelegate: NSObject, NSApplicationDelegate {
return NSMenuItem(title: "Username: Please Refresh", action: nil, keyEquivalent: "")
}()
lazy var instagramFollowers : NSMenuItem = {
return NSMenuItem(title: "Followers: Please Refresh", action: nil, keyEquivalent: "")
return NSMenuItem(title: "Followers: Please Refresh", action: nil, keyEquivalent: "")
}()
lazy var instagramAverageLikes : NSMenuItem = {
return NSMenuItem(title: "Average Likes: Please Refresh", action: nil, keyEquivalent: "")
return NSMenuItem(title: "Average : Please Refresh", action: nil, keyEquivalent: "")
}()
lazy var instagramLastPost : NSMenuItem = {
return NSMenuItem(title: "Last Post: Please Refresh", action: nil, keyEquivalent: "")
Expand All @@ -43,7 +43,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
}()

lazy var tikTokFollowers : NSMenuItem = {
return NSMenuItem(title: "Followers: Please Refresh", action: nil, keyEquivalent: "")
return NSMenuItem(title: "Followers: Please Refresh", action: nil, keyEquivalent: "")
}()

lazy var tikTokHearts : NSMenuItem = {
Expand All @@ -55,7 +55,11 @@ class AppDelegate: NSObject, NSApplicationDelegate {
}()

lazy var twitterFollowers : NSMenuItem = {
return NSMenuItem(title: "Followers: Please Refresh", action: nil, keyEquivalent: "")
return NSMenuItem(title: "Followers ጰ: Please Refresh", action: nil, keyEquivalent: "")
}()

lazy var twitterListed : NSMenuItem = {
return NSMenuItem(title: "Listed: Please Refresh", action: nil, keyEquivalent: "")
}()

lazy var youTubeTitle : NSMenuItem = {
Expand All @@ -66,7 +70,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
return NSMenuItem(title: "Lifetime ▶: Please Refresh", action: nil, keyEquivalent: "")
}()
lazy var youTubeSubscribers : NSMenuItem = {
return NSMenuItem(title: "Subscribers: Please Refresh", action: nil, keyEquivalent: "")
return NSMenuItem(title: "Subscribers: Please Refresh", action: nil, keyEquivalent: "")
}()
lazy var youTubeLastVideo : NSMenuItem = {
return NSMenuItem(title: "Last Video: Please Refresh", action: nil, keyEquivalent: "")
Expand Down Expand Up @@ -146,6 +150,8 @@ class AppDelegate: NSObject, NSApplicationDelegate {
menu.addItem(twitterScreenName)

menu.addItem(twitterFollowers)

menu.addItem(twitterListed)

menu.addItem(
NSMenuItem.separator()
Expand Down Expand Up @@ -235,8 +241,8 @@ class AppDelegate: NSObject, NSApplicationDelegate {
if defaults.integer(forKey: "InstagramInUse") == 1{
DataLoaderInstagram().loadInstagramData()
self.instagramUsername.title = "Username: Loading, please wait"
self.instagramFollowers.title = "Followers: Loading, please wait"
self.instagramAverageLikes.title = "Average Likes: Loading, please wait"
self.instagramFollowers.title = "Followers: Loading, please wait"
self.instagramAverageLikes.title = "Average : Loading, please wait"
self.instagramLastPost.title = "Last Post: Loading, please wait"
}
else{
Expand All @@ -245,7 +251,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
if defaults.integer(forKey: "TikTokInUse") == 1{
DataLoaderTikTok().loadTikTokData()
self.tikTokUniqueID.title = "Username: Loading, please wait (20s)"
self.tikTokFollowers.title = "Followers: Loading, please wait (20s)"
self.tikTokFollowers.title = "Followers: Loading, please wait (20s)"
self.tikTokHearts.title = "Lifetime ♥: Loading, please wait (20s)"
}
else{
Expand All @@ -254,7 +260,8 @@ class AppDelegate: NSObject, NSApplicationDelegate {
if defaults.integer(forKey: "TwitterInUse") == 1{
DataLoaderTwitter().loadTwitterData()
self.twitterScreenName.title = "Screen Name: Loading, please wait"
self.twitterFollowers.title = "Followers: Loading, please wait"
self.twitterFollowers.title = "Followers ጰ: Loading, please wait"
self.twitterListed.title = "Listed: Loading, please wait"
}
else{
}
Expand All @@ -271,7 +278,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
})

self.youTubeTitle.title = "Channel: Loading, please wait"
self.youTubeSubscribers.title = "Subscribers: Loading, please wait"
self.youTubeSubscribers.title = "Subscribers: Loading, please wait"
self.youTubeViews.title = "Lifetime ▶: Loading, please wait"
self.youTubeLastVideo.title = "Last Video: Loading, please wait"
}
Expand All @@ -282,8 +289,8 @@ class AppDelegate: NSObject, NSApplicationDelegate {

if self.defaults.integer(forKey: "InstagramInUse") == 1{
self.instagramUsername.title = "Username: \(String(instagramData.username))"
self.instagramFollowers.title = "Followers: \(String(format: "%U", locale: Locale.current, instagramData.follower))"
self.instagramAverageLikes.title = "Average Likes: \(String(format: "%U", locale: Locale.current, instagramData.average_like))"
self.instagramFollowers.title = "Followers: \(String(format: "%U", locale: Locale.current, instagramData.follower))"
self.instagramAverageLikes.title = "Average : \(String(format: "%U", locale: Locale.current, instagramData.average_like))"

let instagramLastPostLikes = instagramData.last_post?[0].like
let instagramLastPostComment = instagramData.last_post?[0].comment
Expand All @@ -299,11 +306,18 @@ class AppDelegate: NSObject, NSApplicationDelegate {
})


DispatchQueue.main.asyncAfter(deadline: .now() + 5.0, execute: {
DispatchQueue.main.asyncAfter(deadline: .now() + 2.0, execute: {

if self.defaults.integer(forKey: "TwitterInUse") == 1{
self.twitterScreenName.title = "Screen Name: \(twitterData.screen_name)"
self.twitterFollowers.title = "Followers: \(twitterData.followers)"
if twitterData.data?[0].username != nil {
self.twitterScreenName.title = "Screen Name: \(twitterData.data?[0].username as! String)"
self.twitterFollowers.title = "Followers ጰ: \(String(format: "%U", locale: Locale.current, (twitterData.data?[0].publicMetrics.followersCount as! Int)))"
self.twitterListed.title = "Listed: On \(String(format: "%U", locale: Locale.current, (twitterData.data?[0].publicMetrics.listedCount as! Int))) list(s)"
}
else {
self.twitterScreenName.title = "Error; if internet connectivity & username okay, problem is with Twitter API. Try later"
self.twitterFollowers.title = "Error; if internet connectivity & username okay, problem is with Twitter API. Try later"
}
}
else{
}
Expand All @@ -317,7 +331,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
}

if let youTubeSubscriberCount = youTubeData.items?[0].statistics.subscriberCount {
self.youTubeSubscribers.title = "Subscribers: \(youTubeSubscriberCount)"
self.youTubeSubscribers.title = "Subscribers: \(youTubeSubscriberCount)"
} else {
self.youTubeSubscribers.title = "Error; if internet connectivity & Channel ID okay, problem is with YouTube API. Try later"
}
Expand Down Expand Up @@ -357,7 +371,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
self.tikTokUniqueID.title = "Error; if internet connectivity & Username okay, problem is with RapidAPI. Try later"
}
if let tikTokFollowers = tikTokData.data?.userInfo?.stats?.followerCount {
self.tikTokFollowers.title = "Followers: \(String(format: "%U", locale: Locale.current, tikTokFollowers))"
self.tikTokFollowers.title = "Followers: \(String(format: "%U", locale: Locale.current, tikTokFollowers))"
} else {
self.tikTokFollowers.title = "Error; if internet connectivity & Username okay, problem is with RapidAPI. Try later"
}
Expand Down
14 changes: 7 additions & 7 deletions Per mille/DataLoaderTikTok.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,13 @@ var tikTokData = TikTokDataStructure()
print(error)
} else {
let httpResponse = response as? HTTPURLResponse
print("Received from the TikTok API")
if let data = data,
let urlContent = NSString(data: data, encoding: String.Encoding.ascii.rawValue) {
print(urlContent)
} else {
print("error with printing string encoded data")
}
// print("Received from the TikTok API")
// if let data = data,
// let urlContent = NSString(data: data, encoding: String.Encoding.ascii.rawValue) {
// print(urlContent)
// } else {
// print("error with printing string encoded data")
// }

//Parse JSON
let decoder = JSONDecoder()
Expand Down
112 changes: 92 additions & 20 deletions Per mille/DataLoaderTwitter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,88 @@ import Foundation

// define the strucutre of the JSON that will be decoded - came from https://app.quicktype.io

//struct TwitterDataStructure: Codable {
// var screen_name: String = ""
//// var userID: String
//// var lastupdate: String
// var followers: String = ""
//// var country, gender: String
//// var profiles: [String]
//// var peergroups: [Peergroup]
//
// // enum CodingKeys: String, CodingKey {
// // case screenName
// // case userID
// // case lastupdate, followers, country, gender, profiles, peergroups
// // }
//}
//
//struct Peergroup: Codable {
// var topic, region, rank: String
//}


// User's Twitter v2 API
struct TwitterDataStructure: Codable {
var screen_name: String = ""
// var userID: String
// var lastupdate: String
var followers: String = ""
// var country, gender: String
// var profiles: [String]
// var peergroups: [Peergroup]

// enum CodingKeys: String, CodingKey {
// case screenName
// case userID
// case lastupdate, followers, country, gender, profiles, peergroups
// }
var data: [Datum]?
}

struct Peergroup: Codable {
var topic, region, rank: String

struct Datum: Codable {
var id: String
var entities: Entities
var username, name: String
var publicMetrics: PublicMetrics

enum CodingKeys: String, CodingKey {
case id, entities, username, name
case publicMetrics = "public_metrics"
}
}


struct Entities: Codable {
var url, entitiesDescription: Description

enum CodingKeys: String, CodingKey {
case url
case entitiesDescription = "description"
}
}

// MARK: - Description
struct Description: Codable {
var urls: [URLElement]?
}

// MARK: - URLElement
struct URLElement: Codable {
var start, end: Int
var url: String
var expandedURL: String
var displayURL: String

enum CodingKeys: String, CodingKey {
case start, end, url
case expandedURL = "expanded_url"
case displayURL = "display_url"
}
}

// MARK: - PublicMetrics
struct PublicMetrics: Codable {
var followersCount, followingCount, tweetCount, listedCount: Int

enum CodingKeys: String, CodingKey {
case followersCount = "followers_count"
case followingCount = "following_count"
case tweetCount = "tweet_count"
case listedCount = "listed_count"
}
}



// define an instance of the twwitter data that can be filled by the data loader and read by the menu
var twitterData = TwitterDataStructure()

Expand All @@ -41,14 +103,24 @@ var twitterData = TwitterDataStructure()

func loadTwitterData() {

// let headers = [
// "x-rapidapi-host": "peerreach-peerreach-subscription.p.rapidapi.com",
// "x-rapidapi-key": "\(APIKeyRapidAPI)"
// ]
//
// let request = NSMutableURLRequest(url: NSURL(string: "https://peerreach-peerreach-subscription.p.rapidapi.com/user/lookup.json?screen_name=\(AppDelegate().defaults.object(forKey:"TwitterHandle") as? String ?? String())")! as URL,
// cachePolicy: .useProtocolCachePolicy,
// timeoutInterval: 10.0)

let headers = [
"x-rapidapi-host": "peerreach-peerreach-subscription.p.rapidapi.com",
"x-rapidapi-key": "\(APIKeyRapidAPI)"
"Authorization": "Bearer \(APIKeyTwitter)",
]

let request = NSMutableURLRequest(url: NSURL(string: "https://peerreach-peerreach-subscription.p.rapidapi.com/user/lookup.json?screen_name=\(AppDelegate().defaults.object(forKey:"TwitterHandle") as? String ?? String())")! as URL,
let request = NSMutableURLRequest(url: NSURL(string: "https://api.twitter.com/labs/2/users/by?usernames=\(AppDelegate().defaults.object(forKey:"TwitterHandle") as? String ?? String())&user.fields=entities,public_metrics&tweet.fields=created_at,public_metrics")! as URL,
cachePolicy: .useProtocolCachePolicy,
timeoutInterval: 10.0)


request.httpMethod = "GET"
request.allHTTPHeaderFields = headers

Expand All @@ -70,8 +142,8 @@ var twitterData = TwitterDataStructure()
do {
let dataFromTwitter = try decoder.decode(TwitterDataStructure.self, from: data!)
twitterData = dataFromTwitter
// print(twitterData.screen_name)
// print(twitterData.followers)
// print(twitterData.data?[0].username)
// print(twitterData.data?[0].publicMetrics.followersCount)

}
catch {
Expand Down

0 comments on commit 925fe29

Please sign in to comment.