remove unnecessary CBox prefix
rewrite sraper/dumper examples load should not nake a new file simply the NewScraper() funciton
This commit is contained in:
6 files changed
+80
-60
No files matched your search
+13
-7
@@ -1,20 +1,26 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
cboxscraper "github.com/Neo-Desktop/go-cbox-scraper"
|
|
||||||
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
"os"
|
||||||
"sort"
|
"sort"
|
||||||
|
|
||||||
|
cbox "github.com/Neo-Desktop/go-cbox-scraper"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
info := cboxscraper.CBoxServerInfo{}
|
if len(os.Args) != 2 {
|
||||||
scraper := cboxscraper.NewScraper(info, -1, -1)
|
log.Fatalln("syntax:", os.Args[0], "<filename>")
|
||||||
|
}
|
||||||
|
|
||||||
err := scraper.Load("../test.gob")
|
scraper := cbox.NewScraper()
|
||||||
if err != nil {
|
err := scraper.Load(os.Args[1])
|
||||||
log.Println("Load failed:", err)
|
|
||||||
|
if err != nil && os.IsNotExist(err) {
|
||||||
|
log.Fatalln("error, unable to open file", os.Args[1])
|
||||||
|
} else if err != nil {
|
||||||
|
log.Fatalln("error reading file", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
keys := make([]int, 0, len(scraper.Messages))
|
keys := make([]int, 0, len(scraper.Messages))
|
||||||
|
|||||||
+24
-16
@@ -2,37 +2,45 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
"log"
|
||||||
|
"os"
|
||||||
|
|
||||||
cboxscraper "github.com/Neo-Desktop/go-cbox-scraper"
|
cbox "github.com/Neo-Desktop/go-cbox-scraper"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// this example configuration is for a "new" style cbox
|
if len(os.Args) != 2 {
|
||||||
// this will not work with the program as-is
|
log.Fatalln("syntax:", os.Args[0], "[filename]")
|
||||||
info := cboxscraper.CBoxServerInfo{
|
|
||||||
WebHostID: 6,
|
|
||||||
BoxID: 850801,
|
|
||||||
BoxTag: "hD3VIj",
|
|
||||||
Debug: false,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
scraper := cboxscraper.NewScraper(info, -1, -1)
|
scraper := cbox.NewScraper()
|
||||||
|
err := scraper.Load(os.Args[1])
|
||||||
|
|
||||||
err := scraper.Load("../test.gob")
|
if err != nil && os.IsNotExist(err) {
|
||||||
if err != nil {
|
log.Println("Unable to open file, attempting to create...", err)
|
||||||
log.Println("Load failed:", err)
|
scraper.Configure(cbox.ServerInfo{
|
||||||
|
WebHostID: 7,
|
||||||
|
BoxID: 41422,
|
||||||
|
BoxTag: "7wcls1",
|
||||||
|
Debug: false,
|
||||||
|
})
|
||||||
|
err = scraper.Save(os.Args[1])
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalln("Unable to save file:", err)
|
||||||
|
}
|
||||||
|
} else if err != nil {
|
||||||
|
log.Fatalln("Loading file failed:", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = scraper.Scrape(true)
|
err = scraper.Scrape(true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Scraper failed:", err)
|
log.Println("Scraping did not finish successfully: ", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// change this value before saving
|
// all settings and messages are saved when calling Save()
|
||||||
scraper.Debug = true
|
scraper.Debug = true
|
||||||
|
|
||||||
err = scraper.Save("../test.gob")
|
err = scraper.Save(os.Args[1])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Save failed:", err)
|
log.Fatalln("Unable to save file:", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+3
-3
@@ -5,13 +5,13 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type CBoxMessage struct {
|
type Message struct {
|
||||||
MessageID int
|
MessageID int
|
||||||
DateTime time.Time
|
DateTime time.Time
|
||||||
Username string
|
Username string
|
||||||
Message string
|
Message string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *CBoxMessage) String() string {
|
func (m *Message) String() string {
|
||||||
return fmt.Sprintf("#%d [%s] <%s> %s", m.MessageID, m.DateTime.Format(CboxDatetimeFormat), m.Username, m.Message)
|
return fmt.Sprintf("#%d [%s] <%s> %s", m.MessageID, m.DateTime.Format(DatetimeFormat), m.Username, m.Message)
|
||||||
}
|
}
|
||||||
@@ -21,7 +21,7 @@ func init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type CBoxPage struct {
|
type CBoxPage struct {
|
||||||
Messages map[int]*CBoxMessage
|
Messages map[int]*Message
|
||||||
CanPaginate bool
|
CanPaginate bool
|
||||||
|
|
||||||
index int
|
index int
|
||||||
@@ -40,12 +40,12 @@ type CBoxPage struct {
|
|||||||
|
|
||||||
section string
|
section string
|
||||||
|
|
||||||
CBoxServerInfo
|
ServerInfo
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewCBoxPage(cbxInfo CBoxServerInfo) *CBoxPage {
|
func NewCBoxPage(cbxInfo ServerInfo) *CBoxPage {
|
||||||
return &CBoxPage{
|
return &CBoxPage{
|
||||||
Messages: make(map[int]*CBoxMessage),
|
Messages: make(map[int]*Message),
|
||||||
CanPaginate: false,
|
CanPaginate: false,
|
||||||
index: -1,
|
index: -1,
|
||||||
previousIndex: -1,
|
previousIndex: -1,
|
||||||
@@ -58,7 +58,7 @@ func NewCBoxPage(cbxInfo CBoxServerInfo) *CBoxPage {
|
|||||||
smallestID: -1,
|
smallestID: -1,
|
||||||
largestID: -1,
|
largestID: -1,
|
||||||
section: "",
|
section: "",
|
||||||
CBoxServerInfo: cbxInfo,
|
ServerInfo: cbxInfo,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -210,14 +210,14 @@ func (p *CBoxPage) parsePage(document *goquery.Document) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *CBoxPage) parseMessage(messageID int, element *goquery.Selection) *CBoxMessage {
|
func (p *CBoxPage) parseMessage(messageID int, element *goquery.Selection) *Message {
|
||||||
message := CBoxMessage{
|
message := Message{
|
||||||
MessageID: messageID,
|
MessageID: messageID,
|
||||||
}
|
}
|
||||||
|
|
||||||
datetimeElement := element.Find("div").Text()
|
datetimeElement := element.Find("div").Text()
|
||||||
if datetimeElement != "" {
|
if datetimeElement != "" {
|
||||||
message.DateTime, _ = time.Parse(CboxDatetimeFormat, datetimeElement)
|
message.DateTime, _ = time.Parse(DatetimeFormat, datetimeElement)
|
||||||
}
|
}
|
||||||
element.Find("div").Remove()
|
element.Find("div").Remove()
|
||||||
|
|
||||||
|
|||||||
+28
-22
@@ -6,33 +6,39 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
const CboxDatetimeFormat = "2006-01-02 03:04PM"
|
const DatetimeFormat = "2006-01-02 03:04PM"
|
||||||
|
|
||||||
type CBoxScraper struct {
|
type Scraper struct {
|
||||||
SmallestMessageID int
|
SmallestMessageID int
|
||||||
LargestMessageID int
|
LargestMessageID int
|
||||||
Messages map[int]*CBoxMessage
|
Messages map[int]*Message
|
||||||
CBoxServerInfo
|
ServerInfo
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewScraper(cboxServerInfo CBoxServerInfo, smallestID int, largestID int) *CBoxScraper {
|
func NewScraper() *Scraper {
|
||||||
return &CBoxScraper{
|
return &Scraper{
|
||||||
SmallestMessageID: smallestID,
|
Messages: make(map[int]*Message),
|
||||||
LargestMessageID: largestID,
|
|
||||||
CBoxServerInfo: cboxServerInfo,
|
|
||||||
Messages: make(map[int]*CBoxMessage),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *CBoxScraper) sleep() {
|
func NewScraperFromFile(filePath string) (*Scraper, error) {
|
||||||
|
output := NewScraper()
|
||||||
|
err := output.Load(filePath)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return output, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Scraper) sleep() {
|
||||||
s.debugPrintln("Sleeping 10 seconds...")
|
s.debugPrintln("Sleeping 10 seconds...")
|
||||||
time.Sleep(10 * time.Second)
|
time.Sleep(10 * time.Second)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *CBoxScraper) Scrape(updatesOnly bool) error {
|
func (s *Scraper) Scrape(updatesOnly bool) error {
|
||||||
s.debugPrintln("Scraper Started...")
|
s.debugPrintln("Scraper Started...")
|
||||||
|
|
||||||
page := NewCBoxPage(s.CBoxServerInfo)
|
page := NewCBoxPage(s.ServerInfo)
|
||||||
|
|
||||||
err := page.FetchMain()
|
err := page.FetchMain()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -78,14 +84,14 @@ func (s *CBoxScraper) Scrape(updatesOnly bool) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *CBoxScraper) merge(input map[int]*CBoxMessage) {
|
func (s *Scraper) merge(input map[int]*Message) {
|
||||||
for k,v := range input {
|
for k,v := range input {
|
||||||
s.Messages[k] = v
|
s.Messages[k] = v
|
||||||
}
|
}
|
||||||
s.updateIndices()
|
s.updateIndices()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *CBoxScraper) updateIndices() {
|
func (s *Scraper) updateIndices() {
|
||||||
s.SmallestMessageID = -1
|
s.SmallestMessageID = -1
|
||||||
s.LargestMessageID = -1
|
s.LargestMessageID = -1
|
||||||
for k, _ := range s.Messages {
|
for k, _ := range s.Messages {
|
||||||
@@ -98,7 +104,11 @@ func (s *CBoxScraper) updateIndices() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *CBoxScraper) Save(path string) error {
|
func (s *Scraper) Configure(config ServerInfo) {
|
||||||
|
s.ServerInfo = config
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Scraper) Save(path string) error {
|
||||||
flags := os.O_TRUNC | os.O_RDWR | os.O_EXCL
|
flags := os.O_TRUNC | os.O_RDWR | os.O_EXCL
|
||||||
file, err := os.Stat(path)
|
file, err := os.Stat(path)
|
||||||
if file == nil {
|
if file == nil {
|
||||||
@@ -122,12 +132,8 @@ func (s *CBoxScraper) Save(path string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *CBoxScraper) Load(path string) error {
|
func (s *Scraper) Load(path string) error {
|
||||||
file, err := os.Stat(path)
|
if _, err := os.Stat(os.Args[1]); os.IsNotExist(err) {
|
||||||
if file == nil {
|
|
||||||
err = s.Save(path)
|
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+4
-4
@@ -4,26 +4,26 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
)
|
)
|
||||||
|
|
||||||
type CBoxServerInfo struct {
|
type ServerInfo struct {
|
||||||
WebHostID int
|
WebHostID int
|
||||||
BoxID int
|
BoxID int
|
||||||
BoxTag string
|
BoxTag string
|
||||||
Debug bool
|
Debug bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *CBoxServerInfo) debugPrint(args ... interface{}) {
|
func (c *ServerInfo) debugPrint(args ... interface{}) {
|
||||||
if c.Debug {
|
if c.Debug {
|
||||||
log.Print(args...)
|
log.Print(args...)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *CBoxServerInfo) debugPrintf(format string, args ... interface{}) {
|
func (c *ServerInfo) debugPrintf(format string, args ... interface{}) {
|
||||||
if c.Debug {
|
if c.Debug {
|
||||||
log.Printf(format, args...)
|
log.Printf(format, args...)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *CBoxServerInfo) debugPrintln(args ... interface{}) {
|
func (c *ServerInfo) debugPrintln(args ... interface{}) {
|
||||||
if c.Debug {
|
if c.Debug {
|
||||||
log.Println(args...)
|
log.Println(args...)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user