Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
mpostument committed Dec 22, 2020
1 parent f6a139a commit 50626c9
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 36 deletions.
47 changes: 47 additions & 0 deletions README.MD
Original file line number Diff line number Diff line change
@@ -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 <SteamUserName> --apikey <ApiKey>`

Note: Api key can be added to $HOME/.SteamWishlistScraper.yaml as `apikey: <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)
37 changes: 18 additions & 19 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
},
}

Expand All @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ limitations under the License.
*/
package main

import "steamwishlistscraper/cmd"
import "github.com/mpostument/SteamWishlistScraper/cmd"

func main() {
cmd.Execute()
Expand Down
2 changes: 1 addition & 1 deletion pkg/filemanager.go → steam/filemanager.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package pkg
package steam

import (
"bufio"
Expand Down
13 changes: 6 additions & 7 deletions pkg/steamid.go → steam/steamid.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package pkg
package steam

import (
"encoding/json"
Expand All @@ -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)
Expand All @@ -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
}
16 changes: 8 additions & 8 deletions pkg/wishlist.go → steam/wishlist.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package pkg
package steam

import (
"encoding/json"
Expand All @@ -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
Expand All @@ -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 {
Expand All @@ -51,4 +51,4 @@ func ScrapeWishlist(steamId string) []string{
}
sort.Strings(gameList)
return gameList
}
}

0 comments on commit 50626c9

Please sign in to comment.