Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release Candidate 3.5.0 #153

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Change Log

#### 3.x Releases
- `3.5.x` Releases - [3.5.0](#350)
- `3.4.x` Releases - [3.4.0](#340)
- `3.3.x` Releases - [3.3.0](#330)
- `3.2.x` Releases - [3.2.0](#320)
Expand All @@ -22,6 +23,21 @@

---

## [3.5.0](https://github.com/LeonardoCardoso/Swift-Link-Preview/releases/tag/3.5.0)

#### Added
- Annotated `Cancelable.cancel()` as `@objc` to make it compatibale with Objective-C [#135](https://github.com/LeonardoCardoso/SwiftLinkPreview/issues/135)
- Added removal of duplicates from the images array [#45](https://github.com/LeonardoCardoso/SwiftLinkPreview/issues/45)
- Added capture of base URL [#45](https://github.com/LeonardoCardoso/SwiftLinkPreview/issues/45)
- Changed by [LeonardoCardoso](https://github.com/LeonardoCardoso)

#### Changed
- Updated README.md [#150](https://github.com/LeonardoCardoso/SwiftLinkPreview/issues/150)
- Changed by [benlmyers](https://github.com/benlmyers)
- Updated regex limit [#148](https://github.com/LeonardoCardoso/SwiftLinkPreview/issues/148)
- Changed by [kinhvodoi92](https://github.com/kinhvodoi92)


## [3.4.0](https://github.com/LeonardoCardoso/Swift-Link-Preview/releases/tag/3.4.0)

#### Added
Expand Down
7 changes: 3 additions & 4 deletions Example/SwiftLinkPreviewExample.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,9 @@
};
buildConfigurationList = 98846C721D09AA1B00846726 /* Build configuration list for PBXProject "SwiftLinkPreviewExample" */;
compatibilityVersion = "Xcode 8.0";
developmentRegion = English;
developmentRegion = en;
hasScannedForEncodings = 0;
knownRegions = (
English,
en,
Base,
);
Expand Down Expand Up @@ -334,7 +333,7 @@
ONLY_ACTIVE_ARCH = YES;
SDKROOT = iphoneos;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 4.0;
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2";
};
name = Debug;
Expand Down Expand Up @@ -386,7 +385,7 @@
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = iphoneos;
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
SWIFT_VERSION = 4.0;
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2";
VALIDATE_PRODUCT = YES;
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1240"
LastUpgradeVersion = "1300"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ class ViewController: UIViewController {
"www.youtube.com",
"www.google.com",
"facebook.com",

"https://github.com/LeonardoCardoso/SwiftLinkPreview",
"https://www.jbhifi.com.au/products/playstation-4-biomutant",

"https://leocardz.com/swift-link-preview-5a9860c7756f",
"NASA! 🖖🏽 https://www.nasa.gov/",
Expand Down Expand Up @@ -268,6 +269,7 @@ class ViewController: UIViewController {
print("video: ", result.video ?? "no video")
print("icon: ", result.icon ?? "no icon")
print("description: ", result.description ?? "no description")
print("baseURL: ", result.baseURL ?? "no baseURL")
}

guard textField?.text?.isEmpty == false else {
Expand Down
2 changes: 1 addition & 1 deletion Example/SwiftLinkPreviewExample/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>3.4.0</string>
<string>3.5.0</string>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>NSAppTransportSecurity</key>
Expand Down
29 changes: 15 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
> It makes a preview from an URL, grabbing all the information such as title, relevant texts and images.

[![Platform](https://img.shields.io/badge/platform-iOS%20|%20macOS%20|%20watchOS%20|%20tvOS-orange.svg)](https://github.com/LeonardoCardoso/SwiftLinkPreview#requirements-and-details)
[![CocoaPods](https://img.shields.io/badge/pod-v3.4.0-red.svg)](https://github.com/LeonardoCardoso/SwiftLinkPreview#cocoapods)
[![CocoaPods](https://img.shields.io/badge/pod-v3.5.0-red.svg)](https://github.com/LeonardoCardoso/SwiftLinkPreview#cocoapods)
[![Carthage Compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg)](https://github.com/LeonardoCardoso/SwiftLinkPreview#carthage)
[![Swift Package Manager](https://img.shields.io/badge/SPM-compatible-orange.svg)](https://github.com/LeonardoCardoso/SwiftLinkPreview#swift-package-manager)
[![Build Status](https://travis-ci.org/LeonardoCardoso/SwiftLinkPreview.svg?branch=master)](https://travis-ci.org/LeonardoCardoso/SwiftLinkPreview)
Expand Down Expand Up @@ -59,7 +59,7 @@ To use **SwiftLinkPreview** as a pod package just add the following in your **Po
target 'Your Target Name' do
use_frameworks!
// ...
pod 'SwiftLinkPreview', '~> 3.4.0'
pod 'SwiftLinkPreview', '~> 3.5.0'
// ...
end
```
Expand All @@ -70,7 +70,7 @@ To use **SwiftLinkPreview** as a Carthage module package just add the following

```ruby
// ...
github "LeonardoCardoso/SwiftLinkPreview" ~> 3.4.0
github "LeonardoCardoso/SwiftLinkPreview" ~> 3.5.0
// ...
```

Expand All @@ -85,7 +85,7 @@ let package = Package(
name: "Your Target Name",
dependencies: [
// ...
.Package(url: "https://github.com/LeonardoCardoso/SwiftLinkPreview.git", "3.4.0")
.Package(url: "https://github.com/LeonardoCardoso/SwiftLinkPreview.git", "3.5.0")
// ...
]
)
Expand Down Expand Up @@ -121,16 +121,17 @@ let preview = slp.preview("Text containing URL",

```swift
Response {
let url: URL // URL
let finalUrl: URL // unshortened URL
let canonicalUrl: String // canonical URL
let title: String // title
let description: String // page description or relevant text
let images: [String] // array of URLs of the images
let image: String // main image
let icon: String // favicon
let video: String // video
let price: String // price
let baseURL: String? // base
let url: URL? // URL
let finalUrl: URL? // unshortened URL
let canonicalUrl: String? // canonical URL
let title: String? // title
let description: String? // page description or relevant text
let images: [String]? // array of URLs of the images
let image: String? // main image
let icon: String? // favicon
let video: String? // video
let price: String? // price
}
```

Expand Down
2 changes: 1 addition & 1 deletion Sources/Info-macOS.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>3.4.0</string>
<string>3.5.0</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
Expand Down
2 changes: 1 addition & 1 deletion Sources/Info-tvOS.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>3.4.0</string>
<string>3.5.0</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
Expand Down
2 changes: 1 addition & 1 deletion Sources/Info-watchOS.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>3.4.0</string>
<string>3.5.0</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
Expand Down
2 changes: 1 addition & 1 deletion Sources/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>3.4.0</string>
<string>3.5.0</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
Expand Down
5 changes: 3 additions & 2 deletions Sources/Regex.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ class Regex {
static let imageTagPattern = "<img(.+?)src=\"([^\"](.+?))\"(.+?)[/]?>"
static let secondaryImageTagPattern = "og:image\"(.+?)content=\"([^\"](.+?))\"(.+?)[/]?>"
static let titlePattern = "<title(.*?)>(.*?)</title>"
static let metatagPattern = "<meta(.*?)>"
static let metatagContentPattern = "content=(\"(.*?)\")|('(.*?)')"
static let metaTagPattern = "<meta(.*?)>"
static let baseTagPattern = "<base(.+?)href=\"(.*?)\"(.+?)[/]?>"
static let metaTagContentPattern = "content=(\"(.*?)\")|('(.*?)')"
static let cannonicalUrlPattern = "([^\\+&#@%\\?=~_\\|!:,;]+)"
static let rawTagPattern = "<[^>]+>"
static let inlineStylePattern = "<style(.*?)>(.*?)</style>"
Expand Down
3 changes: 2 additions & 1 deletion Sources/Response.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
import Foundation

public struct Response {


public internal(set) var baseURL: String?
public internal(set) var url: URL?
public internal(set) var finalUrl: URL?
public internal(set) var canonicalUrl: String?
Expand Down
6 changes: 6 additions & 0 deletions Sources/ResponseExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ internal extension Response {

var dictionary: [String: Any] {
var responseData:[String: Any] = [:]
responseData["baseURL"] = baseURL
responseData["url"] = url
responseData["finalUrl"] = finalUrl
responseData["canonicalUrl"] = canonicalUrl
Expand All @@ -35,11 +36,14 @@ internal extension Response {
case images
case icon
case video
case baseURL
case price
}

mutating func set(_ value: Any, for key: Key) {
switch key {
case Key.baseURL:
if let value = value as? String { self.baseURL = value }
case Key.url:
if let value = value as? URL { self.url = value }
case Key.finalUrl:
Expand All @@ -65,6 +69,8 @@ internal extension Response {

func value(for key: Key) -> Any? {
switch key {
case Key.baseURL:
return self.baseURL
case Key.url:
return self.url
case Key.finalUrl:
Expand Down
57 changes: 47 additions & 10 deletions Sources/SwiftLinkPreview.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public enum SwiftLinkResponseKey: String {
open class Cancellable: NSObject {
public private(set) var isCancelled: Bool = false

open func cancel() {
@objc open func cancel() {
isCancelled = true
}
}
Expand Down Expand Up @@ -127,15 +127,17 @@ open class SwiftLinkPreview: NSObject {
result.url = url
result.finalUrl = self.extractInURLRedirectionIfNeeded(unshortened)
result.canonicalUrl = self.extractCanonicalURL(unshortened)
result.baseURL = result.baseURL ?? (result.canonicalUrl?.starts(with: "http") == false ? "https://\(result.canonicalUrl!)" : result.canonicalUrl)

self.extractInfo(response: result, cancellable: cancellable, completion: {

result.title = $0.title
result.description = $0.description
result.image = $0.image
result.images = $0.images
result.icon = $0.icon
result.video = $0.video

result.image = self.formatImageURL($0.image, base: $0.baseURL)
result.images = self.formatImageURLs($0.images, base: $0.baseURL)
result.icon = self.formatImageURL($0.icon, base: $0.baseURL)
result.video = self.formatImageURL($0.video, base: $0.baseURL)
result.price = $0.price

self.cache.slp_setCachedResponse(url: unshortened.absoluteString, response: result)
Expand All @@ -154,6 +156,28 @@ open class SwiftLinkPreview: NSObject {
return cancellable
}

private func formatImageURL(_ url: String?, base: String?) -> String? {
guard var url = url else { return nil }

if !url.starts(with: "http"), let base = base {
url = "\(base)\(url)"
}

return url
}

func formatImageURLs(_ array: [String]?, base: String?) -> [String]? {
guard var array = array else { return nil }

for i in 0 ..< array.count {
if let formatted = formatImageURL(array[0], base: base) {
array[i] = formatted
}
}

return Array(Set(array))
}

/*
Extract url redirection inside the GET query.
Like https://www.dji.com/404?url=http%3A%2F%2Fwww.dji.com%2Fmatrice600-pro%2Finfo#specs -> http://www.dji.com/de/matrice600-pro/info#specs
Expand Down Expand Up @@ -287,9 +311,9 @@ extension SwiftLinkPreview {
CFStringConvertIANACharSetNameToEncoding( $0 as CFString ) ) )
} ?? .utf8
if let html = String( data: data, encoding: encoding ) {
for meta in Regex.pregMatchAll( html, regex: Regex.metatagPattern, index: 1 ) {
for meta in Regex.pregMatchAll( html, regex: Regex.metaTagPattern, index: 1 ) {
if (meta.contains( "http-equiv=\"refresh\"" ) || meta.contains( "http-equiv='refresh'" )),
let value = Regex.pregMatchFirst( meta, regex: Regex.metatagContentPattern, index: 2 )?.decoded.extendedTrim,
let value = Regex.pregMatchFirst( meta, regex: Regex.metaTagContentPattern, index: 2 )?.decoded.extendedTrim,
let redirectString = value.split( separator: ";" )
.first( where: { $0.lowercased().starts( with: "url=" ) } )?
.split( separator: "=", maxSplits: 1 ).last,
Expand Down Expand Up @@ -444,6 +468,8 @@ extension SwiftLinkPreview {

result = self.crawlMetaTags(sanitizedHtmlCode, result: result)

result = self.crawlMetaBase(sanitizedHtmlCode, result: result)

var otherResponse = self.crawlTitle(sanitizedHtmlCode, result: result)

otherResponse = self.crawlDescription(otherResponse.htmlCode, result: otherResponse.result)
Expand Down Expand Up @@ -534,10 +560,10 @@ extension SwiftLinkPreview {
Response.Key.title.rawValue,
Response.Key.description.rawValue,
Response.Key.image.rawValue,
Response.Key.video.rawValue,
Response.Key.video.rawValue
]

let metatags = Regex.pregMatchAll(htmlCode, regex: Regex.metatagPattern, index: 1)
let metatags = Regex.pregMatchAll(htmlCode, regex: Regex.metaTagPattern, index: 1)

for metatag in metatags {
for tag in possibleTags {
Expand All @@ -552,7 +578,7 @@ extension SwiftLinkPreview {

if let key = Response.Key(rawValue: tag),
result.value(for: key) == nil {
if let value = Regex.pregMatchFirst(metatag, regex: Regex.metatagContentPattern, index: 2) {
if let value = Regex.pregMatchFirst(metatag, regex: Regex.metaTagContentPattern, index: 2) {
let value = value.decoded.extendedTrim
if tag == "image" {
let value = addImagePrefixIfNeeded(value, result: result)
Expand All @@ -572,6 +598,17 @@ extension SwiftLinkPreview {
return result
}

internal func crawlMetaBase(_ htmlCode: String, result: Response) -> Response {

var result = result

if let base = Regex.pregMatchAll(htmlCode, regex: Regex.baseTagPattern, index: 2).first {
result.set(base, for: .baseURL)
}

return result
}

// Crawl for title if needed
internal func crawlTitle(_ htmlCode: String, result: Response) -> (htmlCode: String, result: Response) {
var result = result
Expand Down
4 changes: 2 additions & 2 deletions SwiftLinkPreview.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ Pod::Spec.new do |s|
s.name = "SwiftLinkPreview"
s.summary = "It makes a preview from an url, grabbing all the information such as title, relevant texts and images."
s.requires_arc = true
s.version = "3.4.0"
s.version = "3.5.0"
s.license = { :type => "MIT", :file => "LICENSE" }
s.author = { "Leonardo Cardoso" => "contact@leocardz.com" }
s.homepage = "https://github.com/LeonardoCardoso/SwiftLinkPreview"
s.source = { :git => "https://github.com/LeonardoCardoso/SwiftLinkPreview.git", :tag => s.version }
s.source_files = "Sources/**/*.swift"
s.swift_version = '4.2'
s.swift_version = '5'

end
Loading