Skip to content

Instantly share code, notes, and snippets.

@skeptycal
Forked from mxcl/detweet.swift
Created January 12, 2022 05:02
Show Gist options
  • Save skeptycal/5f1b8bae2e88ecad4ba6ea92d9a7ab15 to your computer and use it in GitHub Desktop.
Save skeptycal/5f1b8bae2e88ecad4ba6ea92d9a7ab15 to your computer and use it in GitHub Desktop.

Revisions

  1. @mxcl mxcl revised this gist Jan 18, 2020. 1 changed file with 6 additions and 18 deletions.
    24 changes: 6 additions & 18 deletions detweet.swift
    Original file line number Diff line number Diff line change
    @@ -1,19 +1,18 @@
    #!/usr/bin/swift sh
    import Foundation
    import PromiseKit // @mxcl ~> 6.5
    import PromiseKit // @mxcl ~> 6.5
    import Swifter // @mattdonnelly == b27a89

    let swifter = Swifter(
    consumerKey: "FILL",
    consumerSecret: "ME",
    oauthToken: "IN",
    oauthTokenSecret: "https://developer.twitter.com/en/docs/basics/apps/overview.html"
    consumerKey: "FILL",
    consumerSecret: "ME",
    oauthToken: "IN",
    oauthTokenSecret: "https://developer.twitter.com/en/docs/basics/apps/overview.html"
    )

    extension JSON {
    var date: Date? {
    guard let string = string else { return nil }

    let formatter = DateFormatter()
    formatter.dateFormat = "EEE MMM dd HH:mm:ss Z yyyy"
    return formatter.date(from: string)
    @@ -27,49 +26,40 @@ print("Deleting qualifying tweets before:", twoMonthsAgo)
    func deleteTweets(maxID: String? = nil) -> Promise<Void> {
    return Promise { seal in
    swifter.getTimeline(for: "mxcl", count: 200, maxID: maxID, success: { json in

    if json.array!.count <= 1 {
    // if we get one result for a requested maxID, we're done
    return seal.fulfill(())
    }

    for item in json.array! {
    let date = item["created_at"].date!
    let id = item["id_str"].string!
    let id = item["id_str"].string!
    guard date < twoMonthsAgo, item["favorite_count"].integer! < 2 else {
    continue
    }
    swifter.destroyTweet(forID: id, success: { _ in
    print("D:", item["text"].string!)
    }, failure: seal.reject)
    }

    let next = json.array!.last!["id_str"].string!
    deleteTweets(maxID: next).pipe(to: seal.resolve)

    }, failure: seal.reject)
    }
    }

    func deleteFavorites(maxID: String? = nil) -> Promise<Void> {
    return Promise { seal in
    swifter.getRecentlyFavoritedTweets(count: 200, maxID: maxID, success: { json in

    if json.array!.count <= 1 {
    return seal.fulfill(())
    }

    for item in json.array! {
    guard item["created_at"].date! < twoMonthsAgo else { continue }

    swifter.unfavoriteTweet(forID: item["id_str"].string!, success: { _ in
    print("D❤️:", item["text"].string!)
    }, failure: seal.reject)
    }

    let next = json.array!.last!["id_str"].string!
    deleteFavorites(maxID: next).pipe(to: seal.resolve)

    }, failure: seal.reject)
    }
    }
    @@ -81,13 +71,11 @@ func unblockPeople(cursor: String? = nil) -> Promise<Void> {
    print("Unblocking:", id)
    swifter.unblockUser(for: .id(id.string!))
    }

    if let next = next, !next.isEmpty, next != prev, next != "0" {
    unblockPeople(cursor: next).pipe(to: seal.resolve)
    } else {
    seal.fulfill(())
    }

    }, failure: seal.reject)
    }
    }
  2. @mxcl mxcl revised this gist Jan 27, 2019. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions detweet.swift
    Original file line number Diff line number Diff line change
    @@ -35,6 +35,7 @@ func deleteTweets(maxID: String? = nil) -> Promise<Void> {

    for item in json.array! {
    let date = item["created_at"].date!
    let id = item["id_str"].string!
    guard date < twoMonthsAgo, item["favorite_count"].integer! < 2 else {
    continue
    }
  3. @mxcl mxcl revised this gist Jan 27, 2019. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions detweet.swift
    Original file line number Diff line number Diff line change
    @@ -30,7 +30,7 @@ func deleteTweets(maxID: String? = nil) -> Promise<Void> {

    if json.array!.count <= 1 {
    // if we get one result for a requested maxID, we're done
    return seal.fulfill()
    return seal.fulfill(())
    }

    for item in json.array! {
    @@ -55,7 +55,7 @@ func deleteFavorites(maxID: String? = nil) -> Promise<Void> {
    swifter.getRecentlyFavoritedTweets(count: 200, maxID: maxID, success: { json in

    if json.array!.count <= 1 {
    return seal.fulfill()
    return seal.fulfill(())
    }

    for item in json.array! {
    @@ -84,7 +84,7 @@ func unblockPeople(cursor: String? = nil) -> Promise<Void> {
    if let next = next, !next.isEmpty, next != prev, next != "0" {
    unblockPeople(cursor: next).pipe(to: seal.resolve)
    } else {
    seal.fulfill()
    seal.fulfill(())
    }

    }, failure: seal.reject)
  4. @mxcl mxcl created this gist Jan 15, 2019.
    101 changes: 101 additions & 0 deletions detweet.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,101 @@
    #!/usr/bin/swift sh
    import Foundation
    import PromiseKit // @mxcl ~> 6.5
    import Swifter // @mattdonnelly == b27a89

    let swifter = Swifter(
    consumerKey: "FILL",
    consumerSecret: "ME",
    oauthToken: "IN",
    oauthTokenSecret: "https://developer.twitter.com/en/docs/basics/apps/overview.html"
    )

    extension JSON {
    var date: Date? {
    guard let string = string else { return nil }

    let formatter = DateFormatter()
    formatter.dateFormat = "EEE MMM dd HH:mm:ss Z yyyy"
    return formatter.date(from: string)
    }
    }

    let twoMonthsAgo = Date() - 24*60*60*30*2

    print("Deleting qualifying tweets before:", twoMonthsAgo)

    func deleteTweets(maxID: String? = nil) -> Promise<Void> {
    return Promise { seal in
    swifter.getTimeline(for: "mxcl", count: 200, maxID: maxID, success: { json in

    if json.array!.count <= 1 {
    // if we get one result for a requested maxID, we're done
    return seal.fulfill()
    }

    for item in json.array! {
    let date = item["created_at"].date!
    guard date < twoMonthsAgo, item["favorite_count"].integer! < 2 else {
    continue
    }
    swifter.destroyTweet(forID: id, success: { _ in
    print("D:", item["text"].string!)
    }, failure: seal.reject)
    }

    let next = json.array!.last!["id_str"].string!
    deleteTweets(maxID: next).pipe(to: seal.resolve)

    }, failure: seal.reject)
    }
    }

    func deleteFavorites(maxID: String? = nil) -> Promise<Void> {
    return Promise { seal in
    swifter.getRecentlyFavoritedTweets(count: 200, maxID: maxID, success: { json in

    if json.array!.count <= 1 {
    return seal.fulfill()
    }

    for item in json.array! {
    guard item["created_at"].date! < twoMonthsAgo else { continue }

    swifter.unfavoriteTweet(forID: item["id_str"].string!, success: { _ in
    print("D❤️:", item["text"].string!)
    }, failure: seal.reject)
    }

    let next = json.array!.last!["id_str"].string!
    deleteFavorites(maxID: next).pipe(to: seal.resolve)

    }, failure: seal.reject)
    }
    }

    func unblockPeople(cursor: String? = nil) -> Promise<Void> {
    return Promise { seal in
    swifter.getBlockedUsersIDs(stringifyIDs: "true", cursor: cursor, success: { json, prev, next in
    for id in json.array! {
    print("Unblocking:", id)
    swifter.unblockUser(for: .id(id.string!))
    }

    if let next = next, !next.isEmpty, next != prev, next != "0" {
    unblockPeople(cursor: next).pipe(to: seal.resolve)
    } else {
    seal.fulfill()
    }

    }, failure: seal.reject)
    }
    }

    when(fulfilled: deleteTweets(), deleteFavorites(), unblockPeople()).done {
    exit(0)
    }.catch {
    print("error:", $0)
    exit(1)
    }

    RunLoop.main.run()