diff --git a/README.MD b/README.MD new file mode 100644 index 0000000..09144c8 --- /dev/null +++ b/README.MD @@ -0,0 +1,47 @@ +# SteamWishlistScraper + +Scrape your steam wishlist. + +## Table of Contents + +- [SteamWishlistScraper](#steamwishlistscraper) + - [Table of Contents](#table-of-contents) + - [Installing](#installing) + - [Getting Started](#getting-started) + - [Scrape wishlist](#scrape-wishlist) + - [Global parameters](#global-parameters) + - [Contributing](#contributing) + - [License](#license) + +## Installing + +Download the latest binary from [releases](https://github.com/mpostument/SteamWishlistScraper) + +## Getting Started + +### Scrape wishlist + +1. Generate api key on [steam](https://steamcommunity.com/dev/apikey) +2. Run SteamWishlistScraper + `SteamWishlistScraper --username --apikey ` + +Note: Api key can be added to $HOME/.SteamWishlistScraper.yaml as `apikey: ` + +## Global parameters + +`username` - Steam user name. Default `""` +`apikey` - Steam api key. Default `""` + +## Contributing + +1. Fork it +2. Download your fork to your PC ( `git clone https://github.com/mpostument/SteamWishlistScraper && cd SteamWishlistScraper` ) +3. Create your feature branch ( `git checkout -b my-new-feature` ) +4. Make changes and add them ( `git add .` ) +5. Commit your changes ( `git commit -m 'Add some feature'` ) +6. Push to the branch ( `git push origin my-new-feature` ) +7. Create new pull request + +## License + +SteamWishlistScraper is released under the Apache 2.0 license. See [LICENSE.txt](https://github.com/mpostument/SteamWishlistScraper/blob/master/LICENSE) \ No newline at end of file diff --git a/cmd/root.go b/cmd/root.go index 1cd1c47..d0f8c76 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -17,8 +17,10 @@ package cmd import ( "fmt" + "log" "os" - "steamwishlistscraper/pkg" + + "github.com/mpostument/SteamWishlistScraper/steam" homedir "github.com/mitchellh/go-homedir" "github.com/spf13/cobra" @@ -30,29 +32,20 @@ var cfgFile string // rootCmd represents the base command when called without any subcommands var rootCmd = &cobra.Command{ Use: "steamwishlistscraper", - Short: "A brief description of your application", - Long: `A longer description that spans multiple lines and likely contains -examples and usage of using your application. For example: - -Cobra is a CLI library for Go that empowers applications. -This application is a tool to generate the needed files -to quickly create a Cobra application.`, + Short: "Root comand for steam interaction", + Long: `Root comand for steam interaction.`, } var api = &cobra.Command{ Use: "scrape", - Short: "A brief description of your command", - Long: `A longer description that spans multiple lines and likely contains examples -and usage of using your command. For example: -Cobra is a CLI library for Go that empowers applications. -This application is a tool to generate the needed files -to quickly create a Cobra application.`, + Short: "Scrape steam wishlist", + Long: `Scrape steam wishlist.`, Run: func(cmd *cobra.Command, args []string) { - key := viper.GetString("SteamApiKey") + apiKey := viper.GetString("apikey") userName, _ := cmd.Flags().GetString("username") - steamID := pkg.GetSteamId(userName, key) - games := pkg.ScrapeWishlist(steamID) - pkg.SaveToFile(games) + steamID := steam.GetSteamId(userName, apiKey) + games := steam.ScrapeWishlist(steamID) + steam.SaveToFile(games) }, } @@ -71,7 +64,13 @@ func init() { rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.SteamWishlistScraper.yaml)") rootCmd.PersistentFlags().StringP("username", "u", "", "Steam UserName") - rootCmd.MarkPersistentFlagRequired("username") + if err := rootCmd.MarkPersistentFlagRequired("username"); err != nil { + log.Println(err) + } + rootCmd.PersistentFlags().StringP("apikey", "a", "", "Steam api key") + if err := viper.BindPFlag("apikey", rootCmd.PersistentFlags().Lookup("apikey")); err != nil { + log.Println(err) + } } // initConfig reads in config file and ENV variables if set. diff --git a/main.go b/main.go index b77f524..ee56194 100644 --- a/main.go +++ b/main.go @@ -15,7 +15,7 @@ limitations under the License. */ package main -import "steamwishlistscraper/cmd" +import "github.com/mpostument/SteamWishlistScraper/cmd" func main() { cmd.Execute() diff --git a/pkg/filemanager.go b/steam/filemanager.go similarity index 96% rename from pkg/filemanager.go rename to steam/filemanager.go index 76ca11b..a2062af 100644 --- a/pkg/filemanager.go +++ b/steam/filemanager.go @@ -1,4 +1,4 @@ -package pkg +package steam import ( "bufio" diff --git a/pkg/steamid.go b/steam/steamid.go similarity index 72% rename from pkg/steamid.go rename to steam/steamid.go index 9b20de5..819fc85 100644 --- a/pkg/steamid.go +++ b/steam/steamid.go @@ -1,4 +1,4 @@ -package pkg +package steam import ( "encoding/json" @@ -17,10 +17,10 @@ type Response struct { } func GetSteamId(userName string, apiKey string) string { - baseUrl := "http://api.steampowered.com/ISteamUser/ResolveVanityURL/v0001/" - u, err := url.Parse(baseUrl) + baseURL := "http://api.steampowered.com/ISteamUser/ResolveVanityURL/v0001/" + u, err := url.Parse(baseURL) if err != nil { - log.Fatalln(err) + log.Fatalln("Not able to parse url", err) } queryString := u.Query() queryString.Set("key", apiKey) @@ -29,15 +29,14 @@ func GetSteamId(userName string, apiKey string) string { resp, err := http.Get(u.String()) if err != nil { - log.Fatalln(err) + log.Fatalln("Didn't get response from steam", err) } - defer resp.Body.Close() var result User err = json.NewDecoder(resp.Body).Decode(&result) if err != nil { - log.Fatalln(err) + log.Fatalln("Decoding failed", err) } return result.Response.Steamid } diff --git a/pkg/wishlist.go b/steam/wishlist.go similarity index 70% rename from pkg/wishlist.go rename to steam/wishlist.go index 2e1a827..1f87a2c 100644 --- a/pkg/wishlist.go +++ b/steam/wishlist.go @@ -1,4 +1,4 @@ -package pkg +package steam import ( "encoding/json" @@ -15,22 +15,22 @@ type Game struct { Name string `json:"name"` } -func ScrapeWishlist(steamId string) []string{ +func ScrapeWishlist(steamId string) []string { pageNumber := 0 var gameList []string for { - baseUrl := fmt.Sprintf("https://store.steampowered.com/wishlist/profiles/%s/wishlistdata/", steamId) - u, err := url.Parse(baseUrl) + baseURL := fmt.Sprintf("https://store.steampowered.com/wishlist/profiles/%s/wishlistdata/", steamId) + u, err := url.Parse(baseURL) if err != nil { - log.Fatalln(err) + log.Fatalln("Not able to pars url", err) } queryString := u.Query() queryString.Set("p", strconv.Itoa(pageNumber)) u.RawQuery = queryString.Encode() page, err := http.Get(u.String()) if err != nil { - log.Fatalln(err) + log.Fatalln("Didn't get responce from wishlist page", err) } var result map[string]Game @@ -41,7 +41,7 @@ func ScrapeWishlist(steamId string) []string{ if len(res) <= 2 { break } - log.Fatalln(err) + log.Fatalln("Wishlist decoding failed", err) } page.Body.Close() for _, v := range result { @@ -51,4 +51,4 @@ func ScrapeWishlist(steamId string) []string{ } sort.Strings(gameList) return gameList -} \ No newline at end of file +}