Skip to content

Commit

Permalink
Stakwork Youtube Download (#583)
Browse files Browse the repository at this point in the history
* successfully formatted data

* added stakwork request

* commented out feed http

* finished the starkwork youtube download feature

* added Stakwork download process to readme

* reformmated Youtube structs

* removed stringified data
  • Loading branch information
elraphty authored Jul 10, 2023
1 parent 1ef010c commit f8c0c52
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 1 deletion.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,11 @@ If you would like to enable Relay for invoice creation and keysend payment add t
RELAY_URL=
RELAY_AUTH_KEY=
```

### Stakwork Youtube vidoes download for tribes feed

If you would like to enable Stakwork jobs for Youtube videos download add the Stakwork env key and values to the .env file

```
STAKWORK_KEY=
```
2 changes: 1 addition & 1 deletion frontend/app/src/people/auth/signIn.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { Divider } from '../../components/common';
import IconButton from '../../components/common/icon_button';
import { useIsMobile } from '../../hooks';
import QR from '../utils/QR';
import AuthQR from './authQR';
import AuthQR from './AuthQR';
import SphinxAppLoginDeepLink from './SphinxAppLoginDeepLink';

interface ImageProps {
Expand Down
74 changes: 74 additions & 0 deletions handlers/feed.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
package handlers

import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"os"
"strings"

"github.com/stakwork/sphinx-tribes/db"
"github.com/stakwork/sphinx-tribes/feeds"
)

func GetGenericFeed(w http.ResponseWriter, r *http.Request) {
url := r.URL.Query().Get("url")

feed, err := feeds.ParseFeed(url, false)
if err != nil {
w.WriteHeader(http.StatusNotFound)
Expand All @@ -29,10 +33,80 @@ func GetGenericFeed(w http.ResponseWriter, r *http.Request) {

feed.Value = feeds.AddedValue(feed.Value, tribe.OwnerPubKey)

processYoutubeDownload(url, *feed)

w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(feed)
}

func processYoutubeDownload(url string, feed feeds.Feed) {
stakworkKey := fmt.Sprintf("Token token=%s", os.Getenv("STAKWORK_KEY"))
if stakworkKey == "" {
fmt.Println("Youtube Download Error: Stakwork key not found")
} else {
if strings.Contains(url, "youtube") {
var data []string
for z := 0; z < len(feed.Items); z++ {
i := feed.Items[z]
data = append(data, i.Link)
}

type Vars struct {
YoutubeContent []string `json:"youtube_content"`
}

type Attributes struct {
Vars Vars `json:"vars"`
}

type SetVar struct {
Attributes Attributes `json:"attributes"`
}

type WorkflowParams struct {
SetVar SetVar `json:"set_var"`
}

workflows := WorkflowParams{
SetVar: SetVar{
Attributes: Attributes{
Vars: Vars{YoutubeContent: data},
},
},
}

body := map[string]interface{}{
"name": "Sphinx Youtube Content Storage",
"workflow_id": "11848",
"workflow_params": workflows,
}

buf, err := json.Marshal(body)
if err != nil {
fmt.Println("Youtube error: Unable to parse message into byte buffer", err)
return
}

requestUrl := "https://jobs.stakwork.com/api/v1/projects"
request, err := http.NewRequest(http.MethodPost, requestUrl, bytes.NewBuffer(buf))
request.Header.Set("Content-Type", "application/json")
request.Header.Set("Authorization", stakworkKey)

client := &http.Client{}
response, err := client.Do(request)
if err != nil {
fmt.Println("Youtube Download Request Error ===", err)
}
defer response.Body.Close()
res, err := ioutil.ReadAll(response.Body)
if err != nil {
fmt.Println("Youtube Download Request Error ==", err)
}
fmt.Println("Youtube Download Succces ==", string(res))
}
}
}

func GetPodcast(w http.ResponseWriter, r *http.Request) {
url := r.URL.Query().Get("url")
feedid := r.URL.Query().Get("id")
Expand Down

0 comments on commit f8c0c52

Please sign in to comment.