Skip to content

Commit

Permalink
Feat: Add option to ignore cache
Browse files Browse the repository at this point in the history
  • Loading branch information
MeNsaaH committed Jan 24, 2021
1 parent e885321 commit 926c344
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 19 deletions.
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ Usage:

Available Commands:
api host gophie as an API on a PORT env variable, fallback to set argument
clear-cache Clears the Gophie Cache
engines Show summary and list of available engines
help Help about any command
list lists the recent movies by page number
Expand All @@ -75,14 +76,15 @@ Available Commands:
version Get Gophie Version

Flags:
-e, --engine string The Engine to use for querying and downloading (default "netnaija")
-h, --help help for gophie
-o, --output-dir string Path to download files to
-v, --verbose Display Verbose logs
-c, --cache-dir string The directory to store/lookup cache
-e, --engine string The Engine to use for querying and downloading (default "netnaija")
-h, --help help for gophie
-o, --output-dir string Path to download files to
-s, --selenium-url string The URL of selenium instance to use
-v, --verbose Display Verbose logs

Use "gophie [command] --help" for more information about a command.


Gophie - Bisoncorp (2020) (https://github.com/go-phie/gophie)
```

Expand Down
8 changes: 1 addition & 7 deletions cmd/clearCache.go → cmd/clear-cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,7 @@ import (
// clearCacheCmd represents the clearCache command
var clearCacheCmd = &cobra.Command{
Use: "clear-cache",
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: "Clears the Gophie Cache",
Run: func(cmd *cobra.Command, args []string) {
err := os.RemoveAll(viper.GetString("cache-dir"))
if err != nil {
Expand Down
6 changes: 4 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ var (
seleniumURL string
// CacheDir: The Directory to store all colly files
cacheDir string
// Should Cache requests or not
ignoreCache bool
)

// rootCmd represents the base command when called without any subcommands
Expand Down Expand Up @@ -86,13 +88,15 @@ func init() {
&seleniumURL, "selenium-url", "s", "", "The URL of selenium instance to use")
rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "Display Verbose logs")
rootCmd.PersistentFlags().StringVarP(&outputPath, "output-dir", "o", "", "Path to download files to")
rootCmd.PersistentFlags().BoolVar(&ignoreCache, "ignore-cache", false, "Ignore Cache and makes new requests")

viper.BindPFlag("config", rootCmd.PersistentFlags().Lookup("config"))
viper.BindPFlag("verbose", rootCmd.PersistentFlags().Lookup("verbose"))
viper.BindPFlag("engine", rootCmd.PersistentFlags().Lookup("engine"))
viper.BindPFlag("selenium-url", rootCmd.PersistentFlags().Lookup("selenium-url"))
viper.BindPFlag("output-dir", rootCmd.PersistentFlags().Lookup("output-dir"))
viper.BindPFlag("cache-dir", rootCmd.PersistentFlags().Lookup("cache-dir"))
viper.BindPFlag("ignore-cache", rootCmd.PersistentFlags().Lookup("ignore-cache"))

}

Expand All @@ -114,10 +118,8 @@ func initConfig() {
}

// Configs From Env

replacer := strings.NewReplacer("-", "_")
viper.SetEnvKeyReplacer(replacer)
viper.SetEnvPrefix("gophie") // will be uppercased automatically
viper.AutomaticEnv() // read in environment variables that match

}
17 changes: 12 additions & 5 deletions engine/engines.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,23 @@ func Scrape(engine Engine) ([]Movie, error) {
// Config Vars
// seleniumURL := fmt.Sprintf("%s/wd/hub", viper.GetString("selenium-url"))
cacheDir := viper.GetString("cache-dir")
ignoreCache := viper.GetBool("ignore-cache")
var (
t *transport.ChromeDpTransport
err error
c *colly.Collector
)

c := colly.NewCollector(
// Cache responses to prevent multiple download of pages
// even if the collector is restarted
colly.CacheDir(cacheDir),
)
if ignoreCache {
c = colly.NewCollector()

} else {
c = colly.NewCollector(
// Cache responses to prevent multiple download of pages
// even if the collector is restarted
colly.CacheDir(cacheDir),
)
}

// Add Cloud Flare scraper bypasser
if engine.getName() == "NetNaija" {
Expand Down

0 comments on commit 926c344

Please sign in to comment.