Skip to content

Commit

Permalink
add support for videos
Browse files Browse the repository at this point in the history
Signed-off-by: George Lemon <[email protected]>
  • Loading branch information
georgelemon committed Sep 15, 2024
1 parent a201444 commit b7e9c4d
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 2 deletions.
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@
- [x] Direct to Object parser
- [x] Written in Nim language

First, create your API key on https://www.pexels.com/api/
First, create your API key: https://www.pexels.com/api/

## Examples
**Search for Photos**

Search Pexels for any topic that you would like.

```nim
Expand All @@ -34,6 +35,16 @@ for pic in pics:
echo picture.src.tiny
```

**Search for Videos**
```nim
import pkg/pexels
let
px = newPexelsClient(apikey = "123abc")
vids: PexelsVideosResponse =
waitFor px.videos("nature")
for vid in vids:
echo vid
```

### ❤ Contributions & Support
- 🐛 Found a bug? [Create a new Issue](/issues)
Expand Down
2 changes: 1 addition & 1 deletion src/pexels/photo.nim
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ proc `$`*(photoResponse: PexelsPhotoResponse): string =

proc search*(pexels: Pexels, query: string): Future[PexelsPhotosResponse] {.async.} =
pexels.query["query"] = query
let res: AsyncResponse = await pexels.httpGet(PexelsEndpoint.epSearchPhotos, @[])
let res: AsyncResponse = await pexels.httpGet(PexelsEndpoint.epSearchPhotos)
let body = await res.body
result = fromJson(body, PexelsPhotosResponse)
pexels.client.close()
Expand Down
65 changes: 65 additions & 0 deletions src/pexels/video.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# This package provides a Nim API client for the
# Pexels.com API: https://www.pexels.com/api/
#
# (c) 2024 George Lemon | MIT License
# Made by Humans from OpenPeeps
# https://github.com/openpeeps/pexels-nim
import std/[strutils, colors]
import pkg/jsony

import ./metaclient

type
PexelsVideoQuality* = enum
videoQualitySD = "sd"
videoQualityHD = "hd"
videoQualityHLS = "hls"
videoQualityUHD = "uhd"

PexelsVideographer* = object
id*: uint
name*, url*: string

PexelsVideoFile* = object
id*: uint
quality*: PexelsVideoQuality
file_type*: string
width*, height*: uint
fps: float
link*: string

PexelsVideoPicture* = object
id*: uint
picture*: string
nr*: uint

PexelsVideoResponse* = object
id*, width*, height*: uint
url*, image*: string
duration*: uint
user*: PexelsVideographer
video_files*: seq[PexelsVideoFile]
video_pictures*: seq[PexelsVideoPicture]

PexelsVideosResponse* = object of PexelsListingResponse
videos*: seq[PexelsVideoResponse]

proc `$`*(videosResponse: PexelsVideosResponse): string =
## Convert `PexelsVideosResponse` object to JSON
videosResponse.toJson()

proc `$`*(videoResponse: PexelsVideoPicture): string =
## Convert `PexelsVideoPicture` object to JSON
videoResponse.toJson()

proc videos*(pexels: Pexels, query: string): Future[PexelsVideosResponse] {.async.} =
## Search for videos by `query`
pexels.query["query"] = query
let res: AsyncResponse = await pexels.httpGet(PexelsEndpoint.epSearchVideos)
let body = await res.body
result = fromJson(body, PexelsVideosResponse)
pexels.client.close()

iterator items*(videosResponse: PexelsVideosResponse): PexelsVideoResponse =
for v in videosResponse.videos:
yield v

0 comments on commit b7e9c4d

Please sign in to comment.