Post

Earned It

Earned It

Overview

An app for impulsive shoppers who want to shop guilt free and less often that incentivizes shopping by completing productive tasks!

Features

  • Ability to add products to wishlist
  • Complete tasks to earn points and redeem them for items on wishlist
  • Persistant Data using SwiftData
  • Multi-Device Cloud Sync using CloudKit
  • Web-Scraper built to scrape item’s titles/images from Amazon
  • Share Extension adding ability to add items from outside of app

Contributors Forks Stargazers Issues LinkedIn


Open GitHub Repo »

TestFlight Link · Report Bug · Request Feature

Built With

  • CSS

Code Snippet

A small snippet of the logic that scraps product details from Amazon.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
  func getProductTitleName (url: URL) async ->  (String,String){
    let metadataProvider = LPMetadataProvider()
    do {
        let metadata = try await metadataProvider.startFetchingMetadata(for: url)
        return (metadata.title!, metadata.originalURL!.absoluteString)

    }catch{
        let metadata: (String,String)
        metadata.0 = "Random"
        metadata.1 = url.absoluteString
        return (metadata.0, metadata.1)

    }
//    return (metadata.title!, metadata.originalURL!.absoluteString)
    
}

func getProductImage(url: URL, completion: @escaping (Result<(String,String,String), Error>) -> Void) {
    URLSession.shared.dataTask(with: url) { data, response, error in
        if let error = error {
            completion(.failure(error))
            return
        }
        guard let data = data, let html = String(data: data, encoding: .utf8) else {
            completion(.failure(NSError(domain: "InvalidData", code: 0, userInfo: nil)))
            return
        }
        do {
            print(url)
            let doc: Document = try SwiftSoup.parseBodyFragment(html)
            let container: Element = try doc.getElementById("dp-container")!
            let titleElement: Element = try container.getElementById("productTitle")!
            guard let title = try? titleElement.text() else { return }
       
            let imageElement: Element = try container.select("#imgTagWrapperId img").first()!
            let imgLink = try imageElement.attr("src")
            completion(.success((imgLink,try String(contentsOf: url),title)))
        } catch {
            completion(.failure(error))
        }
    }.resume()
}


func getImageUrl(_ input: String)->String{
    let detector = try! NSDataDetector(types: NSTextCheckingResult.CheckingType.link.rawValue)
    let matches = detector.matches(in: input, options: [], range: NSRange(location: 0, length: input.utf16.count))
    
    guard let range = Range(matches[0].range, in: input) else { fatalError("Can not get range") }
    let url = input[range]
    
    return String(url).components(separatedBy: "&")[0]
}


This post is licensed under CC BY 4.0 by the author.