Skip to content

Commit 63bf008

Browse files
authored
Merge pull request #24 from mbaraa/fix/songs-meta-from-search
Fix: Download meta data in search
2 parents 53bdbc2 + 14a577c commit 63bf008

File tree

14 files changed

+111
-83
lines changed

14 files changed

+111
-83
lines changed

README.md

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ _Note: this is a fling side-project that will die in a while so don't get your h
2121

2222
IDK, it would be really nice of you to contribute, check the poorly written [CONTRIBUTING.md](/CONTRIBUTING.md) for more info.
2323

24-
### Roadmap
24+
## Roadmap
2525

2626
- [x] Search YouTube for music
2727
- [x] Web UI
@@ -33,6 +33,17 @@ IDK, it would be really nice of you to contribute, check the poorly written [CON
3333
- [ ] Write a better YouTube scraper (or try to fix the quota thing)
3434
- [ ] Refactor the code (never gonna happen)
3535

36+
## Technologies used
37+
38+
- **[Go](https://golang.org)**: Main programming language.
39+
- **[templ](https://templ.guide)**: The better [html/template](https://pkg.go.dev/html/template).
40+
- **[htmx](https://htmx.org)**: The front-end library of peace.
41+
- **[GORM](https://gorm.io)**: The fantastic ORM library for Golang.
42+
- **[MariaDB](https://mariadb.org)**: Relational database.
43+
- **[Python](https://python.org)**: Used for the YouTube download service.
44+
- **[yt-dlp](https://github.com/yt-dlp/yt-dlp)**: YouTube download helper.
45+
- **[minify](https://github.com/tdewolff/minify)**: Minify static text files.
46+
3647
## Run locally
3748

3849
1. Clone the repo.
@@ -58,9 +69,12 @@ docker compose up
5869

5970
## Acknowledgements
6071

61-
- The background was taken from dankpods.net
62-
- Frank’s original image was taken from dankpods.biz
72+
- **This project is not affiliated with YouTube or Google, or anyone to that matter in any sort of ways.**
73+
- The background was taken from [dankpods.net](https://dankpods.net)
74+
- Frank’s original image was taken from [dingusland.biz](https://dingusland.biz)
6375
- Colorscheme is inspired from [Dankpods](https://www.youtube.com/@DankPods)
64-
- templ was used to make this project’s views: MIT licensed by [Adrian Hesketh](https://github.com/a-h)
65-
- htmx was used to make this project’s client more dynamic: No licence but just wanted to say that I used htmx BTW
6676
- youtube-scrape was used to search videos without using the actual YouTube API (small quota): MIT licenses by [Herman Fassett](https://github.com/HermanFassett)
77+
78+
---
79+
80+
Made with 🧉 by [Baraa Al-Masri](https://mbaraa.com)

cmd/server/server.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func StartServer(staticFS embed.FS) error {
6767
})
6868
pagesHandler.Handle("/music/", http.StripPrefix("/music", http.FileServer(http.Dir(config.Env().YouTube.MusicDir))))
6969

70-
pagesRouter := pages.NewPagesHandler(profileRepo, playlistsService, jwtUtil)
70+
pagesRouter := pages.NewPagesHandler(profileRepo, playlistsService, jwtUtil, &search.ScraperSearch{})
7171
pagesHandler.HandleFunc("/", gHandler.OptionalAuthPage(pagesRouter.HandleHomePage))
7272
pagesHandler.HandleFunc("/signup", gHandler.AuthPage(pagesRouter.HandleSignupPage))
7373
pagesHandler.HandleFunc("/login", gHandler.AuthPage(pagesRouter.HandleLoginPage))
@@ -76,7 +76,7 @@ func StartServer(staticFS embed.FS) error {
7676
pagesHandler.HandleFunc("/playlists", gHandler.AuthPage(pagesRouter.HandlePlaylistsPage))
7777
pagesHandler.HandleFunc("/playlist/{playlist_id}", gHandler.AuthPage(pagesRouter.HandleSinglePlaylistPage))
7878
pagesHandler.HandleFunc("/privacy", gHandler.NoAuthPage(pagesRouter.HandlePrivacyPage))
79-
pagesHandler.HandleFunc("/search", gHandler.OptionalAuthPage(pagesRouter.HandleSearchResultsPage(&search.ScraperSearch{})))
79+
pagesHandler.HandleFunc("/search", gHandler.OptionalAuthPage(pagesRouter.HandleSearchResultsPage))
8080

8181
///////////// APIs /////////////
8282

handlers/pages/pages.go

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,21 @@ type pagesHandler struct {
2525
profileRepo db.GetterRepo[models.Profile]
2626
playlistsService *playlists.Service
2727
jwtUtil jwt.Manager[any]
28+
ytSearch search.Service
2829
}
2930

3031
func NewPagesHandler(
3132
profileRepo db.GetterRepo[models.Profile],
3233
playlistsService *playlists.Service,
3334
jwtUtil jwt.Manager[any],
35+
ytSearch search.Service,
3436
) *pagesHandler {
35-
return &pagesHandler{profileRepo, playlistsService, jwtUtil}
37+
return &pagesHandler{
38+
profileRepo: profileRepo,
39+
playlistsService: playlistsService,
40+
jwtUtil: jwtUtil,
41+
ytSearch: ytSearch,
42+
}
3643
}
3744

3845
func (p *pagesHandler) HandleHomePage(w http.ResponseWriter, r *http.Request) {
@@ -134,31 +141,29 @@ func (p *pagesHandler) HandleProfilePage(w http.ResponseWriter, r *http.Request)
134141
layouts.Default(pages.Profile(profile)).Render(r.Context(), w)
135142
}
136143

137-
func (p *pagesHandler) HandleSearchResultsPage(ytSearch search.Service) http.HandlerFunc {
138-
return func(w http.ResponseWriter, r *http.Request) {
139-
query := r.URL.Query().Get("query")
140-
results, err := ytSearch.Search(query)
141-
if err != nil {
142-
w.WriteHeader(http.StatusNotFound)
143-
w.Write([]byte("not found"))
144-
log.Errorln(err)
145-
return
146-
}
144+
func (p *pagesHandler) HandleSearchResultsPage(w http.ResponseWriter, r *http.Request) {
145+
query := r.URL.Query().Get("query")
146+
results, err := p.ytSearch.Search(query)
147+
if err != nil {
148+
w.WriteHeader(http.StatusNotFound)
149+
w.Write([]byte("not found"))
150+
log.Errorln(err)
151+
return
152+
}
147153

148-
var songsInPlaylists map[string]string
149-
var playlists []entities.Playlist
150-
profileId, profileIdCorrect := r.Context().Value(handlers.ProfileIdKey).(uint)
151-
if profileIdCorrect {
152-
log.Info("downloading songs from search")
153-
playlists, songsInPlaylists, _ = p.playlistsService.GetAllMappedForAddPopover(results, profileId)
154-
}
154+
var songsInPlaylists map[string]string
155+
var playlists []entities.Playlist
156+
profileId, profileIdCorrect := r.Context().Value(handlers.ProfileIdKey).(uint)
157+
if profileIdCorrect {
158+
log.Info("downloading songs' meta data from search")
159+
playlists, songsInPlaylists, _ = p.playlistsService.GetAllMappedForAddPopover(results, profileId)
160+
}
155161

156-
if handlers.IsNoLayoutPage(r) {
157-
pages.SearchResults(results, playlists, songsInPlaylists).Render(r.Context(), w)
158-
return
159-
}
160-
layouts.Default(pages.SearchResults(results, playlists, songsInPlaylists)).Render(r.Context(), w)
162+
if handlers.IsNoLayoutPage(r) {
163+
pages.SearchResults(results, playlists, songsInPlaylists).Render(r.Context(), w)
164+
return
161165
}
166+
layouts.Default(pages.SearchResults(results, playlists, songsInPlaylists)).Render(r.Context(), w)
162167
}
163168

164169
func (p *pagesHandler) HandleSignupPage(w http.ResponseWriter, r *http.Request) {

services/playlists/playlists.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,8 @@ func (p *Service) GetAll(ownerId uint) ([]entities.Playlist, error) {
231231

232232
// TODO: fix this weird ass 3 return values
233233
func (p *Service) GetAllMappedForAddPopover(songs []entities.Song, ownerId uint) ([]entities.Playlist, map[string]string, error) {
234+
_ = p.downloadService.DownloadYoutubeSongsMetadata(songs)
235+
234236
var dbPlaylists []models.Playlist
235237
err := p.
236238
repo.
@@ -273,10 +275,5 @@ func (p *Service) GetAllMappedForAddPopover(songs []entities.Song, ownerId uint)
273275
}
274276
}
275277

276-
err = p.downloadService.DownloadYoutubeSongsMetadata(songs)
277-
if err != nil {
278-
return nil, nil, err
279-
}
280-
281278
return playlists, mappedPlaylists, nil
282279
}

services/youtube/download/download.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,6 @@ func (d *Service) DownloadYoutubeSong(req entities.Song) error {
3535
log.Infof("The song with id %s is already downloaded\n", req.YtId)
3636
return nil
3737
}
38-
if err != nil {
39-
return err
40-
}
4138

4239
resp, err := http.Get(fmt.Sprintf("%s/download/%s", config.Env().YouTube.DownloaderUrl, req.YtId))
4340
if err != nil {

static/css/style.css

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,6 @@
4545
}
4646
}
4747

48-
.btn {
49-
}
50-
5148
span,
5249
p,
5350
h1,
@@ -58,3 +55,11 @@ h5,
5855
h6 {
5956
font-family: "Ubuntu";
6057
}
58+
59+
a {
60+
text-decoration: underline;
61+
}
62+
63+
a:hover {
64+
text-decoration: none;
65+
}

static/images/logo-big.webp

7.5 KB
Binary file not shown.

views/components/header/header.templ

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
)
99

1010
templ homeLinkContainer() {
11-
<div class={ "flex", "justify-center", "items-center", "flex-col", "md:flex-row", "gap-3", "h-min" }>
11+
<div class={ "flex", "justify-center", "items-center", "flex-col", "md:flex-row", "gap-1", "h-min" }>
1212
<img
1313
width="55"
1414
height="55"
@@ -21,27 +21,29 @@ templ homeLinkContainer() {
2121
}
2222

2323
templ mobileHeader() {
24-
<div class={ "md:hidden", "h-[250px]", "flex", "flex-col", "justify-between" }>
25-
@navlink.LinkContainer("/about", "About", icons.About())
26-
<div class={ "absolute", "top-[15px]", "right-[15px]" }>
24+
<div class={ "md:hidden" }>
25+
<div class={ "w-full", "flex", "justify-between", "items-center" }>
26+
@navlink.LinkContainer("/about", "About", icons.About())
2727
@themeswitch.ThemeSwitch()
2828
</div>
29-
<div class={ "font-Ubuntu", "text-secondary" }>
30-
@navlink.LinkContainer("/", "Home", homeLinkContainer())
31-
</div>
32-
<div class={ }>
33-
<p class={ "text-secondary", "font-Ubuntu", "text-xl" }>
34-
if fullName, ok := ctx.Value("full-name").(string); ok {
35-
Hi { fullName } 👋
36-
} else {
37-
Hiyo 👋
38-
}
39-
<br/>
40-
Wanna play something?
41-
</p>
42-
</div>
43-
<div class={ }>
44-
@search.Search()
29+
<div class={ "flex", "flex-col", "justify-between", "gap-y-3" }>
30+
<div class={ "font-Ubuntu", "text-secondary" }>
31+
@navlink.LinkContainer("/", "Home", homeLinkContainer())
32+
</div>
33+
<div>
34+
<p class={ "text-secondary", "font-Ubuntu", "text-xl" }>
35+
if fullName, ok := ctx.Value("full-name").(string); ok {
36+
Hi { fullName } 👋
37+
} else {
38+
Hiyo 👋
39+
}
40+
<br/>
41+
Wanna play something?
42+
</p>
43+
</div>
44+
<div>
45+
@search.Search()
46+
</div>
4547
</div>
4648
</div>
4749
}
@@ -78,7 +80,7 @@ templ desktopHeader() {
7880
}
7981

8082
templ Header() {
81-
<header id="dank-header" class={ "bg-primary", "p-[20px]", "md:p-[10px]" }>
83+
<header id="dank-header" class={ "bg-primary", "p-[15px]", "md:p-[10px]" }>
8284
// mobiles are usually shitty at rendering, so this prevents mobiles from rendering two blocks and choosing one using CSS.
8385
if ctx.Value("is-mobile").(bool) {
8486
@mobileHeader()

views/components/navlink/navlink.templ

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ templ NavLink(title, imageUrl, path string, showTilte ...bool) {
44
<a
55
href={ templ.SafeURL(path) }
66
title={ title }
7-
class={ "cursor-pointer", "underline", "hover:no-underline" , "inline-block" }
7+
class={ "inline-block" }
88
hx-get={ path + "?no_layout=true" }
99
hx-target="#main-contents"
1010
hx-swap="innerHTML"
@@ -33,7 +33,6 @@ templ LinkContainer(path, title string, child templ.Component) {
3333
<a
3434
href={ templ.SafeURL(path) }
3535
title={ title }
36-
class={ "cursor-pointer" }
3736
hx-get={ path + "?no_layout=true" }
3837
hx-target="#main-contents"
3938
hx-swap="innerHTML"

views/components/search/search.templ

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package search
22

33
templ Search() {
4-
<div class={ "!text-primary", "!font-Ubuntu", "w-[92vw]", "md:w-[300px]", "xl:w-[500px]" }>
4+
<div class={ "!text-primary", "!font-Ubuntu", "w-full", "md:w-[300px]", "xl:w-[500px]" }>
55
<form
66
id="search-form"
77
class={ "w-full" }

views/pages/about.templ

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ templ About() {
2020
width="200"
2121
height="200"
2222
class={ "w-[200px]", "h-[200px]", "rounded-[100px]" }
23-
src="/static/images/logo.webp"
23+
src="/static/images/logo-big.webp"
2424
alt="DrankMuzikk Logo"
2525
/>
2626
<h2 class={ "text-3xl" }>DankMuzikk</h2>
@@ -37,29 +37,40 @@ templ About() {
3737
<p>
3838
This application is licensed under the GNU General Public License version 3 (GPLv3), that is, this application is free to use for any purpose, it’s source-code is available on GitHub here for any purpose needed to look at the code, any redestribution of the application has to be under GPLv3 including license and copyright notices.
3939
<br/>
40-
Full license is available <a class={ "underline" } href="https://github.com/mbaraa/dankmuzikk/blob/main/LICENSE" target="_blank">here</a>.
40+
Full license is available <a href="https://github.com/mbaraa/dankmuzikk/blob/main/LICENSE" target="_blank">here</a>.
4141
</p>
4242
</section>
4343
<section id="privacy" class={ "flex", "flex-col", "gap-y-1" }>
4444
<h2 class={ "text-xl" }>Privacy</h2>
4545
<p>
46-
You can find details about privacy <a class={ "underline" } href="/privacy">here</a>.
46+
You can find details about privacy <a href="/privacy">here</a>.
4747
<br/>
48-
TLDR; I don’t collect any data, not sure install uBlock and check trackers yourself or check the code on <a class={ "underline" } href="https://github.com/mbaraa/dankmuzikk" target="_blank">GitHub</a>.
48+
TLDR; I don’t collect any data, not sure install uBlock and check trackers yourself or check the code on <a href="https://github.com/mbaraa/dankmuzikk" target="_blank">GitHub</a>.
4949
</p>
5050
</section>
51+
<section id="technologies" class={ "flex", "flex-col", "gap-y-1" }>
52+
<h2 class={ "text-xl" }>Technologies used</h2>
53+
<ul class={ "list-disc", "ms-5" }>
54+
<li><a target="_blank" href="https://golang.org">Go</a>: Main programming language.</li>
55+
<li><a harget="_blank" href="https://templ.guide">templ</a>: The better <a harget="_blank" href="https://pkg.go.dev/html/template">html/template</a>.</li>
56+
<li><a harget="_blank" href="https://htmx.org">htmx</a>: The front-end library of peace.</li>
57+
<li><a harget="_blank" href="https://gorm.io">GORM</a>: The fantastic ORM library for Golang.</li>
58+
<li><a harget="_blank" href="https://mariadb.org">MariaDB</a>: Relational database.</li>
59+
<li><a harget="_blank" href="https://python.org">Python</a>: Used for the YouTube download service.</li>
60+
<li><a harget="_blank" href="https://github.com/yt-dlp/yt-dlp">yt-dlp]</a>: YouTube download helper.</li>
61+
<li><a harget="_blank" href="https://github.com/tdewolff/minify">minify</a>: Minify static text files.</li>
62+
</ul>
63+
</section>
5164
<section id="acknoledgements" class={ "flex", "flex-col", "gap-y-1" }>
5265
<h2 class={ "text-xl" }>Acknoledgements</h2>
5366
<ul class={ "list-disc", "ms-5" }>
5467
<li>
55-
<p class={ "font-bold" }>This product is not affiliated with YouTube or Google, or anyone to that matter in any sort of ways.</p>
68+
<p class={ "font-bold" }>This project is not affiliated with YouTube or Google, or anyone to that matter in any sort of ways.</p>
5669
</li>
57-
<li>The background was taken from <a class={ "underline" } href="https://dankpods.net" target="_blank">dankpods.net</a></li>
58-
<li>Frank’s original image was taken from <a class={ "underline" } href="https://dingusland.biz" target="_blank">dingusland.biz</a></li>
59-
<li>Colorscheme is inspired from <a class={ "underline" } href="https://youtube.com/@Dankpods" target="_blank">&commat;Dankpods</a> </li>
60-
<li>templ was used to make this project’s views: MIT licensed by <a class={ "underline" } href="https://github.com/a-h" target="_blank">Adrian Hesketh</a>.</li>
61-
<li>htmx was used to make this project’s client more dynamic: No licence but just wanted to say that I used <a class={ "underline" } href="https://htmx.org" target="_blank">htmx</a> BTW.</li>
62-
<li>youtube-scrape was used to search videos without using the actual YouTube API (small quota): MIT licenses by <a class={ "underline" } href="https://github.com/HermanFassett" target="_blank">Herman Fassett</a>.</li>
70+
<li>The background was taken from <a href="https://dankpods.net" target="_blank">dankpods.net</a></li>
71+
<li>Frank’s original image was taken from <a href="https://dingusland.biz" target="_blank">dingusland.biz</a></li>
72+
<li>Colorscheme is inspired from <a href="https://youtube.com/@Dankpods" target="_blank">&commat;Dankpods</a> </li>
73+
<li>youtube-scrape was used to search videos without using the actual YouTube API (small quota): MIT licenses by <a href="https://github.com/HermanFassett" target="_blank">Herman Fassett</a>.</li>
6374
</ul>
6475
</section>
6576
</section>

views/pages/index.templ

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ templ Index() {
2222
@navlink.NavLink("in about page", "", "/about")
2323
<br/>
2424
<br/>
25-
And you can check the beta features <a class={ "cursor-pointer", "underline", "hover:no-underline" } href="https://beta.dankmuzikk.com">here</a>
25+
And you can check the beta features <a href="https://beta.dankmuzikk.com">here</a>
2626
<br/>
2727
<br/>
2828
Happy danking 🎉✨

views/pages/privacy.templ

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ templ Privacy() {
5252
</ul>
5353
<p>Contact Us</p>
5454
<p>
55-
If you have any questions about this Privacy Policy, please contact me at: <a class={ "underline" } href="mailto:dankmuzikk@mbaraa.com">dankmuzikk@mbaraa.com</a>
55+
If you have any questions about this Privacy Policy, please contact me at: <a href="mailto:dankmuzikk@mbaraa.com">dankmuzikk@mbaraa.com</a>
5656
</p>
5757
</section>
5858
</main>

ytdl/main.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,21 +116,19 @@ def download_song(id: str) -> int:
116116

117117
## wait list
118118
while to_be_downloaded.exists(id):
119-
print(f"the song with the yt id {id} is being downloaded in the background...")
120119
time.sleep(1)
121120
pass
122121

123-
124-
print("preparing to download")
125-
126122
## download the stuff
127123
if song_exists(id):
128124
to_be_downloaded.remove(id)
129125
return 0
126+
130127
to_be_downloaded.append(id)
131128
ytdl.download(f"https://www.youtube.com/watch?v={id}")
132-
update_song_status(id)
133129
to_be_downloaded.remove(id)
130+
update_song_status(id)
131+
134132
return 0
135133
except DownloadError:
136134
return 1

0 commit comments

Comments
 (0)