Преглед на файлове

go-music: moved to a proper repository

Douglas A преди 3 години
родител
ревизия
08f12d5010
променени са 10 файла, в които са добавени 116 реда и са изтрити 256 реда
  1. 0 26
      go-music/.gitignore
  2. 0 8
      go-music/LICENSE
  3. 0 3
      go-music/README.md
  4. 0 5
      go-music/go.mod
  5. 0 10
      go-music/go.sum
  6. 0 171
      go-music/mb.go
  7. 0 33
      go-music/mb_test.go
  8. 11 0
      wander-ball/Makefile
  9. BIN
      wander-ball/wander
  10. 105 0
      wander-ball/wander.c

+ 0 - 26
go-music/.gitignore

@@ -1,26 +0,0 @@
-# ---> Go
-# 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
-

+ 0 - 8
go-music/LICENSE

@@ -1,8 +0,0 @@
-MIT License
-Copyright (c) <year> <copyright holders>
-
-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.

+ 0 - 3
go-music/README.md

@@ -1,3 +0,0 @@
-# music-brainz-go
-
-Go implementation of the API to access music-brainz

+ 0 - 5
go-music/go.mod

@@ -1,5 +0,0 @@
-module go-music
-
-go 1.14
-
-require github.com/stretchr/testify v1.5.1

+ 0 - 10
go-music/go.sum

@@ -1,10 +0,0 @@
-github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
-github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
-github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
-github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
-github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
-github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
-github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
-gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
-gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
-gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

+ 0 - 171
go-music/mb.go

@@ -1,171 +0,0 @@
-package mb
-
-import (
-	"encoding/json"
-	"fmt"
-	"net/http"
-	"net/url"
-	"sort"
-	"time"
-)
-
-const (
-	apiURI               = "https://musicbrainz.org/ws/2"
-	artistSearchEndpoint = "artist?query"
-)
-
-//MB holds the http client
-type MB struct {
-	Client http.Client
-}
-
-//Search holds the search returns
-type Search struct {
-	artist artistSearch
-}
-
-//NewMusicBrainz returns a new Search object
-func NewMusicBrainz() MB {
-
-	var mb MB
-	timeout := time.Duration(10 * time.Second)
-
-	mb.Client = http.Client{Timeout: timeout}
-	return mb
-}
-
-//SearchForArtist return a list of artists using `query` as parameter if none found returns error
-func (m MB) SearchForArtist(query string) (*Search, error) {
-	var ret Search
-	uri := fmt.Sprintf("%s/%s=%s", apiURI, artistSearchEndpoint, url.QueryEscape(query))
-
-	req, err := http.NewRequest(
-		"GET",
-		uri,
-		nil)
-
-	req.Header.Set(
-		"User-Agent",
-		"Go Application Development: Create spotify playlist based on artist")
-	req.Header.Set("Content-Type", "application/json")
-	req.Header.Set("Accept", "application/json")
-
-	if err != nil {
-		fmt.Printf("error creating request\n%v", err)
-		return nil, err
-	}
-
-	resp, err := m.Client.Do(req)
-	if err != nil {
-		fmt.Printf("error opening request\n%v", err)
-		return nil, err
-	}
-	defer resp.Body.Close()
-	if err != nil {
-		fmt.Printf("could not read body\n%v", err)
-		return nil, err
-	}
-	err = json.NewDecoder(resp.Body).Decode(&ret.artist)
-	if err != nil {
-		fmt.Printf("could not decode body\n%v", err)
-		return nil, err
-	}
-
-	if ret.artist.Count == 0 {
-		return nil, fmt.Errorf("cound not found artist: %s", query)
-	}
-
-	return &ret, nil
-}
-
-//GetArtistName get the artist with the highest score on the search
-func (s Search) GetArtistName() (string, error) {
-
-	if len(s.artist.Artists) == 0 {
-		return "", fmt.Errorf("could not find any artist")
-	}
-	return s.artist.Artists[0].Name, nil
-}
-
-//GetArtistTags returns the tags in order of relevance of the current artist
-func (s Search) GetArtistTags() []Tags {
-	var t []Tags
-	for _, elem := range s.artist.Artists[0].Tags {
-		if int(elem.Count) > 0 {
-			t = append(t, elem)
-		}
-	}
-	sort.Sort(ByInverseCount(t))
-	return t
-}
-
-type artistSearch struct {
-	Created time.Time `json:"created"`
-	Count   int       `json:"count"`
-	Offset  int       `json:"offset"`
-	Artists []artists `json:"artists"`
-}
-
-type area struct {
-	ID       string `json:"id"`
-	Type     string `json:"type"`
-	TypeID   string `json:"type-id"`
-	Name     string `json:"name"`
-	SortName string `json:"sort-name"`
-	LifeSpan struct {
-		Ended interface{} `json:"ended"`
-	} `json:"life-span"`
-}
-
-type alias struct {
-	SortName  string      `json:"sort-name"`
-	TypeID    string      `json:"type-id,omitempty"`
-	Name      string      `json:"name"`
-	Locale    interface{} `json:"locale"`
-	Type      string      `json:"type"`
-	Primary   interface{} `json:"primary"`
-	BeginDate interface{} `json:"begin-date"`
-	EndDate   interface{} `json:"end-date"`
-}
-
-type artists struct {
-	ID        string `json:"id"`
-	Type      string `json:"type,omitempty"`
-	TypeID    string `json:"type-id,omitempty"`
-	Score     int    `json:"score"`
-	Name      string `json:"name"`
-	SortName  string `json:"sort-name"`
-	Country   string `json:"country,omitempty"`
-	Area      area   `json:"area,omitempty"`
-	BeginArea struct {
-		ID       string `json:"id"`
-		Type     string `json:"type"`
-		TypeID   string `json:"type-id"`
-		Name     string `json:"name"`
-		SortName string `json:"sort-name"`
-		LifeSpan struct {
-			Ended interface{} `json:"ended"`
-		} `json:"life-span"`
-	} `json:"begin-area,omitempty"`
-	Isnis    []string `json:"isnis,omitempty"`
-	LifeSpan struct {
-		Begin string      `json:"begin"`
-		Ended interface{} `json:"ended"`
-	} `json:"life-span,omitempty"`
-	Aliases        []alias `json:"aliases,omitempty"`
-	Tags           []Tags  `json:"tags,omitempty"`
-	Disambiguation string  `json:"disambiguation,omitempty"`
-}
-
-//Tags represent Artist tags (eg. Rock, Punk, Late 2000s)
-type Tags struct {
-	Count int    `json:"count"`
-	Name  string `json:"name"`
-}
-
-//ByInverseCount return the tags in relevance order
-type ByInverseCount []Tags
-
-func (t ByInverseCount) Len() int           { return len(t) }
-func (t ByInverseCount) Less(i, j int) bool { return t[i].Count >= t[j].Count }
-func (t ByInverseCount) Swap(i, j int)      { t[i], t[j] = t[j], t[i] }

+ 0 - 33
go-music/mb_test.go

@@ -1,33 +0,0 @@
-package mb
-
-import (
-	"testing"
-
-	"github.com/stretchr/testify/assert"
-)
-
-func TestShouldBeEqualToArtist(t *testing.T) {
-	m := NewMusicBrainz()
-	s, _ := m.SearchForArtist("Metallica")
-	name, _ := s.GetArtistName()
-
-	assert.Equal(t, name, "Metallica")
-}
-
-func TestErrShouldBeNilIfArtistIsValid(t *testing.T) {
-	m := NewMusicBrainz()
-	_, err := m.SearchForArtist("Iron Maiden")
-	assert.Nil(t, err, "err should be nil for valid artist")
-}
-
-func TestIfArtistIsValidThenErrIsNil(t *testing.T) {
-	m := NewMusicBrainz()
-	_, err := m.SearchForArtist("Metallica")
-	assert.Nil(t, err, "err should be nil if found artist")
-}
-
-func TestErrShouldContainErrorIfArtistIsInvalid(t *testing.T) {
-	m := NewMusicBrainz()
-	_, err := m.SearchForArtist("difjapodsifjapdsif adspofijdas flkdsajf pakdsfad")
-	assert.NotNil(t, err, "err should not be nil if found artist")
-}

+ 11 - 0
wander-ball/Makefile

@@ -0,0 +1,11 @@
+PROGRAM = wander
+CFLAGS =-Wall -Werror -pedantic -pedantic-errors -fno-fast-math -fno-builtin -m64 -std=iso9899:1999 -I${PWD}
+
+
+all: $(PROGRAM)
+
+$(PROGRAM): $(PROGRAM).o
+	$(CC) $(CFLAGS) `pkg-config --cflags --libs sdl2` -o $@ $<
+
+$(PROGRAM).o: $(PROGRAM).c
+	$(CC) $(CFLAGS) -c `pkg-config --cflags sdl2` -o $@ $<

BIN
wander-ball/wander


+ 105 - 0
wander-ball/wander.c

@@ -0,0 +1,105 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdbool.h>
+
+#include <SDL2/SDL.h>
+
+typedef struct {
+        SDL_Window *win;
+        SDL_Renderer *ren;
+        SDL_Event *ev;
+        bool is_initialized;
+        int flags;
+        bool running;
+} env;
+
+typedef struct {
+        int x;
+        int y;
+} vec2d;
+
+bool init_env(
+  env *env,
+  int sdl_flags,
+  char* win_name,
+  vec2d win_size,
+  int renderer_flags)
+{
+        if (env->is_initialized)
+                return false;
+        SDL_Init(sdl_flags);
+        env->flags = sdl_flags;
+        env->win = SDL_CreateWindow(win_name,
+                                    SDL_WINDOWPOS_UNDEFINED,
+                                    SDL_WINDOWPOS_UNDEFINED,
+                                    win_size.x,
+                                    win_size.y,
+                                    SDL_WINDOW_OPENGL);
+        if (!env->win) {
+                fprintf(stderr, "could not create window: %s\n", SDL_GetError());
+                return false;
+        }
+
+        env->ren = SDL_CreateRenderer(env->win,
+                                      -1,
+                                      renderer_flags);
+        
+        if (!env->win) {
+                fprintf(stderr, "could not create renderer: %s\n", SDL_GetError());                                                                                                                     
+                return false;
+        }
+        env->running = false;
+
+        return true;
+}
+
+void process_event(env *env){
+        switch(env->ev->type)
+        {
+                case SDL_QUIT:
+                        env->running = false;
+                        break;
+        }
+
+}
+
+void run(env *env) 
+{
+        if (!env->is_initialized) {
+                fprintf(stderr, "event not initialized.");
+                return;
+        }
+        env->running = true;
+        while (env->running) {
+                while(SDL_PollEvent(env->ev))
+                        process_event(env);
+        }
+}
+
+void destroy_env(env *env)
+{
+        env->is_initialized = false;
+        SDL_DestroyRenderer(env->ren);
+        SDL_DestroyWindow(env->win);
+        env->flags = 0;
+}
+
+int main(int argc, char** argv)
+{
+        env e;
+        e.is_initialized = false;
+        vec2d win_size = { .x = 800, .y = 600 };
+        if (init_env(&e, 
+                     SDL_INIT_VIDEO,
+                     "Wander",
+                     win_size,
+                     SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC)){
+                fprintf(stdout, "cannot initialize, exiting\n");
+                return 1;
+        }
+        
+
+
+        run(&e);
+        destroy_env(&e);
+}