Add flag to auto-open page in web browser
This commit is contained in:
parent
f3f2550d85
commit
795b7250ed
@ -15,16 +15,11 @@ import (
|
|||||||
"gitlab.com/offline-twitter/twitter_offline_engine/pkg/scraper"
|
"gitlab.com/offline-twitter/twitter_offline_engine/pkg/scraper"
|
||||||
)
|
)
|
||||||
|
|
||||||
/**
|
// Global variable referencing the open data profile
|
||||||
* Global variable referencing the open data profile
|
|
||||||
*/
|
|
||||||
var profile persistence.Profile
|
var profile persistence.Profile
|
||||||
|
|
||||||
var version_string string
|
var version_string string
|
||||||
|
|
||||||
/**
|
|
||||||
* Main method
|
|
||||||
*/
|
|
||||||
func main() {
|
func main() {
|
||||||
profile_dir := flag.String("profile", ".", "")
|
profile_dir := flag.String("profile", ".", "")
|
||||||
flag.StringVar(profile_dir, "p", ".", "")
|
flag.StringVar(profile_dir, "p", ".", "")
|
||||||
@ -159,7 +154,10 @@ func main() {
|
|||||||
case "unlike_tweet":
|
case "unlike_tweet":
|
||||||
unlike_tweet(target)
|
unlike_tweet(target)
|
||||||
case "webserver":
|
case "webserver":
|
||||||
start_webserver(*addr)
|
fs := flag.NewFlagSet("", flag.ExitOnError)
|
||||||
|
should_auto_open := fs.Bool("auto-open", false, "")
|
||||||
|
fs.Parse(args[1:])
|
||||||
|
start_webserver(*addr, *should_auto_open)
|
||||||
case "fetch_inbox":
|
case "fetch_inbox":
|
||||||
fetch_inbox(*how_many)
|
fetch_inbox(*how_many)
|
||||||
case "fetch_dm":
|
case "fetch_dm":
|
||||||
@ -184,7 +182,6 @@ func main() {
|
|||||||
// args:
|
// args:
|
||||||
// - username: twitter username or email address
|
// - username: twitter username or email address
|
||||||
// - password: twitter account password
|
// - password: twitter account password
|
||||||
|
|
||||||
func login(username string, password string) {
|
func login(username string, password string) {
|
||||||
// Skip the scraper.the_api variable, just use a local one since no scraping is happening
|
// Skip the scraper.the_api variable, just use a local one since no scraping is happening
|
||||||
api := scraper.NewGuestSession()
|
api := scraper.NewGuestSession()
|
||||||
@ -406,9 +403,9 @@ func list_followed() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func start_webserver(addr string) {
|
func start_webserver(addr string, should_auto_open bool) {
|
||||||
app := webserver.NewApp(profile)
|
app := webserver.NewApp(profile)
|
||||||
app.Run(addr)
|
app.Run(addr, should_auto_open)
|
||||||
}
|
}
|
||||||
|
|
||||||
func fetch_inbox(how_many int) {
|
func fetch_inbox(how_many int) {
|
||||||
|
@ -8,6 +8,8 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
"runtime"
|
||||||
"path"
|
"path"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
@ -130,7 +132,7 @@ func (app *Application) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (app *Application) Run(address string) {
|
func (app *Application) Run(address string, should_auto_open bool) {
|
||||||
srv := &http.Server{
|
srv := &http.Server{
|
||||||
Addr: address,
|
Addr: address,
|
||||||
ErrorLog: app.ErrorLog,
|
ErrorLog: app.ErrorLog,
|
||||||
@ -147,10 +149,24 @@ func (app *Application) Run(address string) {
|
|||||||
|
|
||||||
app.start_background()
|
app.start_background()
|
||||||
|
|
||||||
|
if should_auto_open {
|
||||||
|
go openWebPage("http://" + address)
|
||||||
|
}
|
||||||
err := srv.ListenAndServe()
|
err := srv.ListenAndServe()
|
||||||
app.ErrorLog.Fatal(err)
|
app.ErrorLog.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func openWebPage(url string) error {
|
||||||
|
switch runtime.GOOS {
|
||||||
|
case "darwin": // macOS
|
||||||
|
return exec.Command("open", url).Run()
|
||||||
|
case "windows":
|
||||||
|
return exec.Command("cmd", "/c", "start", url).Run()
|
||||||
|
default: // Linux and others
|
||||||
|
return exec.Command("xdg-open", url).Run()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func parse_cursor_value(c *persistence.Cursor, r *http.Request) error {
|
func parse_cursor_value(c *persistence.Cursor, r *http.Request) error {
|
||||||
cursor_param := r.URL.Query().Get("cursor")
|
cursor_param := r.URL.Query().Get("cursor")
|
||||||
if cursor_param != "" {
|
if cursor_param != "" {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user