Skip to content
This repository has been archived by the owner on Jan 18, 2025. It is now read-only.

Commit

Permalink
Merge pull request #147 from LeonardoCardoso/release-candidate/3.4.0
Browse files Browse the repository at this point in the history
Release candidate/3.4.0
  • Loading branch information
LeonardoCardoso authored Jul 27, 2021
2 parents 6c195f6 + c5deedc commit 589d073
Show file tree
Hide file tree
Showing 19 changed files with 155 additions and 113 deletions.
15 changes: 15 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.4.x` Releases - [3.4.0](#340)
- `3.3.x` Releases - [3.3.0](#330)
- `3.2.x` Releases - [3.2.0](#320)
- `3.1.x` Releases - [3.1.0](#310)
Expand All @@ -21,6 +22,20 @@

---

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

#### Added
- Added support for m3u8 lists
- Added by [jeffhodsdon](https://github.com/jeffhodsdon)

#### Changed
- Resolve relative image URLs against the request URL.
- Changed by [lhunath](https://github.com/lhunath)
- Video parsing fix
- Changed by [jeffhodsdon](https://github.com/jeffhodsdon)
- Fixed github link image for `og:image` property.
- Changed by [MuhtasimTanmoy](https://github.com/MuhtasimTanmoy/)

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

#### Changed
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.3.0</string>
<string>3.4.0</string>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>NSAppTransportSecurity</key>
Expand Down
8 changes: 4 additions & 4 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.3.0-red.svg)](https://github.com/LeonardoCardoso/SwiftLinkPreview#cocoapods)
[![CocoaPods](https://img.shields.io/badge/pod-v3.4.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.3.0'
pod 'SwiftLinkPreview', '~> 3.4.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.3.0
github "LeonardoCardoso/SwiftLinkPreview" ~> 3.4.0
// ...
```

Expand All @@ -85,7 +85,7 @@ let package = Package(
name: "Your Target Name",
dependencies: [
// ...
.Package(url: "https://github.com/LeonardoCardoso/SwiftLinkPreview.git", "3.3.0")
.Package(url: "https://github.com/LeonardoCardoso/SwiftLinkPreview.git", "3.4.0")
// ...
]
)
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.3.0</string>
<string>3.4.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.3.0</string>
<string>3.4.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.3.0</string>
<string>3.4.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.3.0</string>
<string>3.4.0</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
Expand Down
10 changes: 6 additions & 4 deletions Sources/Regex.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ import Foundation

// MARK: - Regular expressions
class Regex {

static let imagePattern = "(.+?)\\.(gif|jpg|jpeg|png|bmp)$"

static let imagePattern = "(.+?)\\.(gif|jpg|jpeg|png|bmp)\\?.*$"
static let videoPattern = "(.+?)\\.(mp4|mpeg|avi|mov|m3u8)\\?.*$"
static let openGraphImagePattern = "(.+?)\\.(gif||jpg|jpeg|png|bmp)$"
static let videoTagPattern = "<video[^>]+src=\"([^\"]+)"
static let secondaryVideoTagPattern = "og:video\" content=\"([^\"](.+?))\"(.+?)[/]?>"
static let secondaryVideoTagPattern = "og:video\"(.+?)content=\"([^\"](.+?))\"(.+?)[/]?>"
static let imageTagPattern = "<img(.+?)src=\"([^\"](.+?))\"(.+?)[/]?>"
static let secondaryImageTagPattern = "og:image\" content=\"([^\"](.+?))\"(.+?)[/]?>"
static let secondaryImageTagPattern = "og:image\"(.+?)content=\"([^\"](.+?))\"(.+?)[/]?>"
static let titlePattern = "<title(.*?)>(.*?)</title>"
static let metatagPattern = "<meta(.*?)>"
static let metatagContentPattern = "content=(\"(.*?)\")|('(.*?)')"
Expand Down
21 changes: 18 additions & 3 deletions Sources/StringExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,27 @@ extension String {
// Check if url is an image
func isImage() -> Bool {

return Regex.test(self, regex: Regex.imagePattern)
let possible = ["gif", "jpg", "jpeg", "png", "bmp"]
if let url = URL(string: self),
possible.contains(url.pathExtension) {
return true
}

return false
}

func isOpenGraphImage() -> Bool {
return Regex.test(self, regex: Regex.openGraphImagePattern)
}

func isVideo() -> Bool {
return Regex.test(self, regex: Regex.videoTagPattern)
let possible = ["mp4", "mov", "mpeg", "avi", "m3u8"]
if let url = URL(string: self),
possible.contains(url.pathExtension) {
return true
}

return false
}

// Split into substring of equal length
Expand Down
52 changes: 30 additions & 22 deletions Sources/SwiftLinkPreview.swift
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,7 @@ extension SwiftLinkPreview {
let value = value.decoded.extendedTrim
if tag == "image" {
let value = addImagePrefixIfNeeded(value, result: result)
if value.isImage() { result.set(value, for: key) }
if value.isOpenGraphImage(){ result.set(value, for: key) }
} else if tag == "video" {
let value = addImagePrefixIfNeeded(value, result: result)
if value.isVideo() { result.set(value, for: key) }
Expand Down Expand Up @@ -621,23 +621,25 @@ extension SwiftLinkPreview {
let images = result.images

if images == nil || images?.isEmpty ?? true {
let values = Regex.pregMatchAll(htmlCode, regex: Regex.imageTagPattern, index: 2)
if !values.isEmpty {
let imgs = values.map { self.addImagePrefixIfNeeded($0, result: result) }

result.images = imgs
result.image = imgs.first
}
else{
let values = Regex.pregMatchAll(htmlCode, regex: Regex.secondaryImageTagPattern, index: 1)
// Should look for <meta property="og:image" content=""/> first instead of <img/> tag.
let values = Regex.pregMatchAll(htmlCode, regex: Regex.secondaryImageTagPattern, index: 2)
if !values.isEmpty {
result.images = values
result.image = values.first
} else {
// If no OpenGraph image found pick any from <img/> tag to show.
let values = Regex.pregMatchAll(htmlCode, regex: Regex.imageTagPattern, index: 2)
if !values.isEmpty {
result.images = values
result.image = values.first
let imgs = values.map { self.addImagePrefixIfNeeded($0, result: result) }
result.images = imgs
result.image = imgs.first
}
}

}
} else {
let values = Regex.pregMatchAll(htmlCode, regex: Regex.secondaryImageTagPattern, index: 1)
let values = Regex.pregMatchAll(htmlCode, regex: Regex.secondaryImageTagPattern, index: 2)
if !values.isEmpty {
result.images = values
result.image = values.first
Expand Down Expand Up @@ -678,21 +680,27 @@ extension SwiftLinkPreview {
var image = image

// TODO: account for HTML <base>
if let canonicalUrl = canonicalUrl, let finalUrl = finalUrl {
if finalUrl.hasPrefix("https:") {
if let canonicalUrl = canonicalUrl, let finalUrl = finalUrl, let proto = finalUrl.split(separator: ":").first {
if image.hasPrefix("/") {
if image.hasPrefix("//") {
image = "https:" + image
} else if image.hasPrefix("/") {
image = "https://" + canonicalUrl + image
// image url is //domain/path
image = proto + ":" + image
} else {
// image url is /path relative to base url
image = proto + "://" + canonicalUrl + image
}
} else if !image.contains("://") {
// image is relative to request url
let requestUrl = removeSuffixIfNeeded(finalUrl)
if requestUrl.hasSuffix("/") {
image = requestUrl + image
} else {
image = (requestUrl as NSString).deletingLastPathComponent + "/" + image
}
} else if image.hasPrefix("//") {
image = "http:" + image
} else if image.hasPrefix("/") {
image = "http://" + canonicalUrl + image
}
}

return removeSuffixIfNeeded(image)
return image

}

Expand Down
2 changes: 1 addition & 1 deletion SwiftLinkPreview.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ 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.3.0"
s.version = "3.4.0"
s.license = { :type => "MIT", :file => "LICENSE" }
s.author = { "Leonardo Cardoso" => "[email protected]" }
s.homepage = "https://github.com/LeonardoCardoso/SwiftLinkPreview"
Expand Down
12 changes: 4 additions & 8 deletions SwiftLinkPreview.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
1F8164ED26287866000F2905 /* VideoTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1F8164EC26287866000F2905 /* VideoTests.swift */; };
68074FFA1F23B6C900649DE6 /* head-meta-icon.html in Resources */ = {isa = PBXBuildFile; fileRef = 68074FF91F23B6C900649DE6 /* head-meta-icon.html */; };
68074FFB1F23BB1100649DE6 /* head-meta-icon.html in Resources */ = {isa = PBXBuildFile; fileRef = 68074FF91F23B6C900649DE6 /* head-meta-icon.html */; };
68074FFC1F23BB1400649DE6 /* head-meta-icon.html in Resources */ = {isa = PBXBuildFile; fileRef = 68074FF91F23B6C900649DE6 /* head-meta-icon.html */; };
Expand All @@ -32,9 +33,6 @@
9272A10D1E2EF0E600F9F17E /* Regex.swift in Sources */ = {isa = PBXBuildFile; fileRef = 98348AC71D0A00DF003FA2B3 /* Regex.swift */; };
9272A10E1E2EF0E700F9F17E /* Regex.swift in Sources */ = {isa = PBXBuildFile; fileRef = 98348AC71D0A00DF003FA2B3 /* Regex.swift */; };
9272A10F1E2EF0E800F9F17E /* Regex.swift in Sources */ = {isa = PBXBuildFile; fileRef = 98348AC71D0A00DF003FA2B3 /* Regex.swift */; };
9272A1111E2F07AF00F9F17E /* MemoryLeaks.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9272A1101E2F07AF00F9F17E /* MemoryLeaks.swift */; };
9272A1121E2F07AF00F9F17E /* MemoryLeaks.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9272A1101E2F07AF00F9F17E /* MemoryLeaks.swift */; };
9272A1131E2F07AF00F9F17E /* MemoryLeaks.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9272A1101E2F07AF00F9F17E /* MemoryLeaks.swift */; };
982812901D3A9281000D3ABB /* URLs.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9828128F1D3A9281000D3ABB /* URLs.swift */; };
982812921D3A9293000D3ABB /* RegexTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 982812911D3A9293000D3ABB /* RegexTests.swift */; };
982813231D3AD9D4000D3ABB /* SwiftLinkPreview.h in Headers */ = {isa = PBXBuildFile; fileRef = 98C545951D2B30AD0003A74E /* SwiftLinkPreview.h */; settings = {ATTRIBUTES = (Public, ); }; };
Expand Down Expand Up @@ -151,12 +149,12 @@
/* End PBXContainerItemProxy section */

/* Begin PBXFileReference section */
1F8164EC26287866000F2905 /* VideoTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = VideoTests.swift; sourceTree = "<group>"; };
68074FF91F23B6C900649DE6 /* head-meta-icon.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = "head-meta-icon.html"; sourceTree = "<group>"; };
686E58DE1F22416D000C2A33 /* IconTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = IconTests.swift; sourceTree = "<group>"; };
7A552DE121A460910019E8B1 /* Response.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Response.swift; sourceTree = "<group>"; };
7A552DE621A466EB0019E8B1 /* ResponseExtension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ResponseExtension.swift; sourceTree = "<group>"; };
9272A1041E2EC11000F9F17E /* Cache.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Cache.swift; sourceTree = "<group>"; };
9272A1101E2F07AF00F9F17E /* MemoryLeaks.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MemoryLeaks.swift; sourceTree = "<group>"; };
9828128F1D3A9281000D3ABB /* URLs.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = URLs.swift; sourceTree = "<group>"; };
982812911D3A9293000D3ABB /* RegexTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RegexTests.swift; sourceTree = "<group>"; };
9828136C1D3AE2EA000D3ABB /* IntExtension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = IntExtension.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -360,10 +358,10 @@
98DC53391D1D73DB001134E3 /* SwiftLinkPreviewTests */ = {
isa = PBXGroup;
children = (
9272A1101E2F07AF00F9F17E /* MemoryLeaks.swift */,
98B5ED491D3E7DC600AEBD54 /* HugeTests.swift */,
985DCEC81D2C029700B40D76 /* BodyTests.swift */,
985DCEC61D2C026000B40D76 /* ImageTests.swift */,
1F8164EC26287866000F2905 /* VideoTests.swift */,
985DCEC41D2C022E00B40D76 /* MetaTests.swift */,
982812911D3A9293000D3ABB /* RegexTests.swift */,
986D5BE41D33DFE50025555F /* TitleTests.swift */,
Expand Down Expand Up @@ -733,14 +731,14 @@
982812901D3A9281000D3ABB /* URLs.swift in Sources */,
982812921D3A9293000D3ABB /* RegexTests.swift in Sources */,
686E58DF1F22416D000C2A33 /* IconTests.swift in Sources */,
1F8164ED26287866000F2905 /* VideoTests.swift in Sources */,
9828136D1D3AE2EA000D3ABB /* IntExtension.swift in Sources */,
985DCECF1D2C03D900B40D76 /* StringTestExtension.swift in Sources */,
98B5ED4A1D3E7DC600AEBD54 /* HugeTests.swift in Sources */,
985DCEC91D2C029700B40D76 /* BodyTests.swift in Sources */,
9272A10B1E2EEE1700F9F17E /* StringExtension.swift in Sources */,
986D5BE51D33DFE60025555F /* TitleTests.swift in Sources */,
985DCEC71D2C026000B40D76 /* ImageTests.swift in Sources */,
9272A1111E2F07AF00F9F17E /* MemoryLeaks.swift in Sources */,
988B48D81D2C3C3D0040A4AD /* Constants.swift in Sources */,
9272A10D1E2EF0E600F9F17E /* Regex.swift in Sources */,
);
Expand Down Expand Up @@ -777,7 +775,6 @@
9272A10C1E2EEE1800F9F17E /* StringExtension.swift in Sources */,
98E7C3271D3B24C6009E5F6D /* StringTestExtension.swift in Sources */,
98E7C3281D3B24C6009E5F6D /* File.swift in Sources */,
9272A1131E2F07AF00F9F17E /* MemoryLeaks.swift in Sources */,
98E7C3291D3B24C6009E5F6D /* IntExtension.swift in Sources */,
9272A10F1E2EF0E800F9F17E /* Regex.swift in Sources */,
);
Expand Down Expand Up @@ -814,7 +811,6 @@
9272A10A1E2EEE1600F9F17E /* StringExtension.swift in Sources */,
98F76D111D3AF78600E9B10E /* StringTestExtension.swift in Sources */,
98F76D121D3AF78600E9B10E /* File.swift in Sources */,
9272A1121E2F07AF00F9F17E /* MemoryLeaks.swift in Sources */,
98F76D131D3AF78600E9B10E /* IntExtension.swift in Sources */,
9272A10E1E2EF0E700F9F17E /* Regex.swift in Sources */,
);
Expand Down
4 changes: 2 additions & 2 deletions SwiftLinkPreviewTests/HugeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ class HugeTests: XCTestCase {

do {

// Get youtube.com becaus it contains a huge HTML
let source = try String(contentsOf: URL(string: "https://youtube.com")!).extendedTrim
// Get reddit.com because it contains a huge HTML
let source = try String(contentsOf: URL(string: "https://reddit.com")!).extendedTrim

let title = self.slp.crawlCode(source, minimum: SwiftLinkPreview.titleMinimumRelevant)
let description = self.slp.crawlCode(source, minimum: SwiftLinkPreview.decriptionMinimumRelevant)
Expand Down
17 changes: 17 additions & 0 deletions SwiftLinkPreviewTests/ImageTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -95,5 +95,22 @@ class ImageTests: XCTestCase {
}

}

func testImgur() {

do {

let source = try String(contentsOf: URL(string: "https://imgur.com/GoAkW6w")!).extendedTrim

let result = self.slp.crawlMetaTags(source, result: Response())

print(result)

} catch let err as NSError {

print("\(err)")

}
}

}
2 changes: 1 addition & 1 deletion SwiftLinkPreviewTests/Info-macOS.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<key>CFBundlePackageType</key>
<string>BNDL</string>
<key>CFBundleShortVersionString</key>
<string>3.3.0</string>
<string>3.4.0</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
Expand Down
2 changes: 1 addition & 1 deletion SwiftLinkPreviewTests/Info-tvOS.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<key>CFBundlePackageType</key>
<string>BNDL</string>
<key>CFBundleShortVersionString</key>
<string>3.3.0</string>
<string>3.4.0</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
Expand Down
2 changes: 1 addition & 1 deletion SwiftLinkPreviewTests/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<key>CFBundlePackageType</key>
<string>BNDL</string>
<key>CFBundleShortVersionString</key>
<string>3.3.0</string>
<string>3.4.0</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
Expand Down
Loading

0 comments on commit 589d073

Please sign in to comment.