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

Stakwork Youtube Download #583

Merged
merged 7 commits into from
Jul 10, 2023
Merged
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
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