Skip to content

Commit

Permalink
Merge pull request #208 from TheM4hd1/development
Browse files Browse the repository at this point in the history
updated like function
  • Loading branch information
TheM4hd1 authored Aug 8, 2020
2 parents 51de980 + e5b1f0e commit 46ac60a
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 3 deletions.
2 changes: 1 addition & 1 deletion SwiftyInsta.podspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Pod::Spec.new do |s|

s.name = "SwiftyInsta"
s.version = "2.3.4"
s.version = "2.3.5"
s.summary = "Private and Tokenless Instagram RESTful API."

s.homepage = "https://github.com/TheM4hd1/SwiftyInsta"
Expand Down
2 changes: 1 addition & 1 deletion SwiftyInsta/API/Endpoints/Endpoint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public struct Endpoint {
/// Media info.
case info = "https://i.instagram.com/api/v1/media/{mediaId}/info/"
/// Like.
case like = "https://i.instagram.com/api/v1/media/{mediaId}/like/"
case like = "https://i.instagram.com/api/v1/media/{mediaId}/like/?d=1"
/// Unlike.
case unlike = "https://i.instagram.com/api/v1/media/{mediaId}/unlike/"
/// Likers.
Expand Down
8 changes: 7 additions & 1 deletion SwiftyInsta/API/Handlers/MediaHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,20 @@ public final class MediaHandler: Handler {
return completionHandler(.failure(GenericError.custom("Invalid `Authentication.Response` in `APIHandler.respone`. Log in again.")))
}
let body = ["_uuid": handler.settings.device.deviceGuid.uuidString,
"device_id": handler.settings.device.deviceGuid.uuidString,
"_uid": storage.dsUserId,
"_csrftoken": storage.csrfToken,
"delivery_class": "organic",
"inventory_source": "media_or_ad",
"is_carousel_bumped_post": "false",
"container_module": "feed_timeline",
"carousel_index": "0",
"media_id": mediaId]

requests.request(Status.self,
method: .post,
endpoint: Endpoint.Media.like.media(mediaId),
body: .parameters(body),
body: .payload(body),
completion: { completionHandler($0.map { $0.state == .ok }) })
}

Expand Down
13 changes: 13 additions & 0 deletions SwiftyInsta/Helpers/HTTPHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class HTTPHelper {
/// A body for the `URLRequest`.
enum Body {
case parameters([String: Any])
case payload([String: Any])
case data(Data)
// case gzip([String: Any])
}
Expand Down Expand Up @@ -207,6 +208,7 @@ class HTTPHelper {
me.addHeaders(to: &request, header: headers)
switch body {
case .parameters(let parameters)?: me.addBody(to: &request, body: parameters)
case .payload(let parameters)?: me.setPayload(to: &request, body: parameters)
case .data(let data)?: request.httpBody = data
default: break
}
Expand Down Expand Up @@ -252,4 +254,15 @@ class HTTPHelper {
request.httpBody = data.data(using: String.Encoding.utf8)
}
}

fileprivate func setPayload(to request: inout URLRequest, body: [String: Any]) {
if !body.isEmpty {
guard let jsonData = try? JSONSerialization.data(withJSONObject: body, options: []) else { return }
guard let json = String(data: jsonData, encoding: String.Encoding.utf8)?
.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) else { return }
let payload = "SIGNATURE." + json
let signedBody = "signed_body=" + payload
request.httpBody = signedBody.data(using: String.Encoding.utf8)
}
}
}

0 comments on commit 46ac60a

Please sign in to comment.