-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 96a185c
Showing
10 changed files
with
600 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# Compiled Object files, Static and Dynamic libs (Shared Objects) | ||
*.o | ||
*.a | ||
*.so | ||
|
||
# Folders | ||
_obj | ||
_test | ||
|
||
# Architecture specific extensions/prefixes | ||
*.[568vq] | ||
[568vq].out | ||
|
||
*.cgo1.go | ||
*.cgo2.c | ||
_cgo_defun.c | ||
_cgo_gotypes.go | ||
_cgo_export.* | ||
|
||
_testmain.go | ||
|
||
*.exe | ||
*.test | ||
*.prof | ||
|
||
# Output of the go coverage tool, specifically when used with LiteIDE | ||
*.out | ||
*.bin | ||
|
||
config.ini | ||
crossbuild.bat | ||
safelinks.txt | ||
|
||
testfolder/* | ||
|
||
deploy.sh |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
FROM golang:1.14 as builder | ||
|
||
WORKDIR /app | ||
|
||
COPY go.* ./ | ||
RUN go mod download | ||
|
||
COPY . ./ | ||
|
||
RUN CGO_ENABLED=0 GOOS=linux go build -mod=readonly -v -o server | ||
|
||
FROM alpine:3 | ||
RUN apk add --no-cache ca-certificates tzdata | ||
|
||
COPY --from=builder /app/server /server | ||
|
||
CMD ["/server"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2016 Sebastian Stefan Winkler | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"net/http" | ||
"os" | ||
|
||
"github.com/Seklfreak/youtube-opml-exporter/pkg" | ||
) | ||
|
||
func main() { | ||
http.HandleFunc("/", pkg.ExportHandler) | ||
http.HandleFunc("/login", pkg.LoginHandler) | ||
http.HandleFunc("/exchange", pkg.ExchangeHandler) | ||
|
||
port := os.Getenv("PORT") | ||
if port == "" { | ||
port = "8080" | ||
} | ||
|
||
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", port), nil)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
module github.com/Seklfreak/youtube-opml-exporter | ||
|
||
go 1.14 | ||
|
||
require ( | ||
cloud.google.com/go v0.54.0 // indirect | ||
github.com/kelseyhightower/envconfig v1.4.0 | ||
go.uber.org/zap v1.14.0 | ||
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d | ||
google.golang.org/api v0.20.0 | ||
google.golang.org/genproto v0.0.0-20200306153348-d950eab6f860 // indirect | ||
) |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package pkg | ||
|
||
import ( | ||
"errors" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/kelseyhightower/envconfig" | ||
"go.uber.org/zap" | ||
"golang.org/x/oauth2" | ||
"google.golang.org/api/option" | ||
"google.golang.org/api/youtube/v3" | ||
) | ||
|
||
type config struct { | ||
GoogleClientID string `envconfig:"GOOGLE_CLIENT_ID" required:"true"` | ||
GoogleClientSecret string `envconfig:"GOOGLE_CLIENT_SECRET" required:"true"` | ||
} | ||
|
||
var ( | ||
logger *zap.Logger | ||
|
||
cfg config | ||
|
||
oauthConfig = &oauth2.Config{ | ||
ClientID: "", // filled by config | ||
ClientSecret: "", // filled by config | ||
Endpoint: oauth2.Endpoint{ | ||
AuthURL: "https://accounts.google.com/o/oauth2/auth", | ||
TokenURL: "https://oauth2.googleapis.com/token", | ||
}, | ||
RedirectURL: "urn:ietf:wg:oauth:2.0:oob", | ||
Scopes: []string{youtube.YoutubeReadonlyScope}, | ||
} | ||
) | ||
|
||
func init() { | ||
var err error | ||
|
||
logger, err = zap.NewProduction() | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
err = envconfig.Process("", &cfg) | ||
if err != nil { | ||
logger.Fatal("failure reading config", zap.Error(err)) | ||
} | ||
|
||
oauthConfig.ClientID = cfg.GoogleClientID | ||
oauthConfig.ClientSecret = cfg.GoogleClientSecret | ||
} | ||
|
||
func ytServiceViaBasicAuth(r *http.Request) (*youtube.Service, error) { | ||
_, password, ok := r.BasicAuth() | ||
if !ok || password == "" { | ||
return nil, errors.New("please provide refresh token") | ||
} | ||
|
||
oauthToken := &oauth2.Token{ | ||
Expiry: time.Now(), | ||
TokenType: "Bearer", | ||
RefreshToken: password, | ||
} | ||
|
||
return youtube.NewService( | ||
r.Context(), | ||
option.WithTokenSource(oauthConfig.TokenSource(r.Context(), oauthToken)), | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package pkg | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
) | ||
|
||
func ExchangeHandler(w http.ResponseWriter, r *http.Request) { | ||
token, err := oauthConfig.Exchange(r.Context(), r.URL.Query().Get("token")) | ||
if err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
w.Header().Set("Content-Type", "text/plain; charset=utf-8") | ||
w.Header().Set("X-Content-Type-Options", "nosniff") | ||
fmt.Fprintf(w, "Refresh Token: %s", token.RefreshToken) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package pkg | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
|
||
"go.uber.org/zap" | ||
"google.golang.org/api/youtube/v3" | ||
) | ||
|
||
const ( | ||
documentFmt string = ` | ||
<opml version="1.1"> | ||
<body> | ||
<outline text="YouTube Subscriptions" title="YouTube Subscriptions"> | ||
%s | ||
</outline> | ||
</body> | ||
</opml> | ||
` | ||
itemFmt string = ` | ||
<outline text="%s" title="%s" type="rss" | ||
xmlUrl="https://www.youtube.com/feeds/videos.xml?channel_id=%s"/> | ||
` | ||
) | ||
|
||
func ExportHandler(w http.ResponseWriter, r *http.Request) { | ||
yt, err := ytServiceViaBasicAuth(r) | ||
if err != nil { | ||
http.Error(w, err.Error(), http.StatusUnauthorized) | ||
return | ||
} | ||
|
||
var items []*youtube.Subscription | ||
|
||
var pageToken string | ||
for { | ||
logger.Info("making YouTube API request", zap.String("page_token", pageToken)) | ||
|
||
resp, err := yt.Subscriptions. | ||
List("snippet"). | ||
MaxResults(50). | ||
Order("alphabetical"). | ||
Mine(true). | ||
PageToken(pageToken). | ||
Do() | ||
if err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
items = append(items, resp.Items...) | ||
|
||
pageToken = resp.NextPageToken | ||
if resp.NextPageToken == "" { | ||
break | ||
} | ||
} | ||
|
||
logger.Info("found items", zap.Int("amount", len(items))) | ||
|
||
var result string | ||
for _, item := range items { | ||
if item == nil || item.Snippet == nil || item.Snippet.ResourceId == nil || | ||
item.Snippet.ResourceId.ChannelId == "" { | ||
logger.Warn("skipping item because it does not have all required fields", zap.Any("item", item)) | ||
continue | ||
} | ||
|
||
result += fmt.Sprintf(itemFmt, item.Snippet.Title, item.Snippet.Title, item.Snippet.ResourceId.ChannelId) | ||
} | ||
|
||
result = fmt.Sprintf(documentFmt, result) | ||
|
||
w.Header().Set("Content-Type", "application/xml") | ||
fmt.Fprint(w, result) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package pkg | ||
|
||
import ( | ||
"net/http" | ||
|
||
"golang.org/x/oauth2" | ||
) | ||
|
||
func LoginHandler(w http.ResponseWriter, r *http.Request) { | ||
authURL := oauthConfig.AuthCodeURL("state-token", oauth2.AccessTypeOffline) | ||
|
||
http.Redirect(w, r, authURL, http.StatusFound) | ||
} |