Skip to content
Extraits de code Groupes Projets
Valider fa176d83 rédigé par Alexandre Morignot's avatar Alexandre Morignot
Parcourir les fichiers

Try to save Post

parent 9469e7d3
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
......@@ -4,6 +4,7 @@ import (
"log"
"regexp"
"sync"
"time"
"github.com/mvdan/xurls"
......@@ -13,7 +14,7 @@ import (
type Bot interface {
Collection
ParseLine(author string, line string, contents chan *site.Content) error
ParseLine(author string, source string, line string, contents chan *site.Content) error
}
type PlayBot struct {
......@@ -47,7 +48,25 @@ func (*PlayBot) ExtractTags(line string) []string {
return tags
}
func (pb *PlayBot) ParseLine(author string, line string, contents chan *site.Content) error {
func (pb *PlayBot) MakePost(author string, source string, url string, tags []string) *Post {
content, err := pb.GetByUrl(url)
if err != nil {
log.Print(err)
return nil
}
content.AddTags(tags)
post := &Post{
Date: time.Now(),
Author: author,
Source: source,
Content: content,
}
return post
}
func (pb *PlayBot) ParseLine(author string, source string, line string, contents chan *site.Content) error {
tags := pb.ExtractTags(line)
urls := xurls.Strict.FindAllString(line, -1)
var wg sync.WaitGroup
......@@ -56,12 +75,10 @@ func (pb *PlayBot) ParseLine(author string, line string, contents chan *site.Con
wg.Add(1)
go func(url string) {
defer wg.Done()
content, err := pb.Add(url, tags)
if err != nil {
log.Print(err)
} else {
contents <- content
post := pb.MakePost(author, source, url, tags)
pb.InsertPost(post)
if post != nil {
contents <- post.Content
}
}(url)
}
......
package bot
import (
"log"
"git.iiens.net/morignot2011/playbot/site"
)
type Collection interface {
Add(url string, tags []string) (*site.Content, error)
Get()
GetByUrl(url string) (*site.Content, error)
InsertPost(*Post) error
}
type PlayBotCollection struct {
......@@ -17,11 +16,9 @@ type PlayBotCollection struct {
Readers []site.Reader
}
func (pb *PlayBotCollection) Add(url string, tags []string) (*site.Content, error) {
log.Printf("%s: Add() %s, %q", pb.Source, url, tags)
func (pbc *PlayBotCollection) GetByUrl(url string) (*site.Content, error) {
var content *site.Content
for _, reader := range pb.Readers {
for _, reader := range pbc.Readers {
var err error
content, err = reader.Read(url)
if err != nil {
......@@ -32,9 +29,10 @@ func (pb *PlayBotCollection) Add(url string, tags []string) (*site.Content, erro
}
}
pb.Db.Create(content)
return content, nil
}
func (*PlayBotCollection) Get() {
func (pbc *PlayBotCollection) InsertPost(post *Post) error {
pbc.Db.Insert(post)
return nil
}
package bot
import (
"fmt"
"log"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/mysql"
)
type DbParams struct {
Host string
Port string
User string
Password string
DbName string
}
type Db interface {
Create(interface{}) Db
Insert(interface{}) error
}
type PlayBotDb struct {
db *gorm.DB
}
func NewPlayBotDb(dbParams DbParams) (Db, error) {
conn := fmt.Sprintf(
"%s:%s@tcp(%s:%s)/%s",
dbParams.User,
dbParams.Password,
dbParams.Host,
dbParams.Port,
dbParams.DbName,
)
log.Printf("About to connect")
db, err := gorm.Open("mysql", conn)
log.Printf("Connected")
if err != nil {
return nil, err
}
db.LogMode(true)
return &PlayBotDb{db}, nil
}
func (pdb *PlayBotDb) Insert(value interface{}) error {
pdb.db.Create(value)
return nil
}
package bot
import (
"log"
"git.iiens.net/morignot2011/playbot/site"
)
......@@ -11,23 +9,22 @@ type Factory interface {
}
type playBotFactory struct {
db Db
readers []site.Reader
}
func NewPlayBotFactory(dbParams DbParams, readers []site.Reader) *playBotFactory {
return &playBotFactory{readers}
func NewPlayBotFactory(dbParams DbParams, readers []site.Reader) (*playBotFactory, error) {
db, err := NewPlayBotDb(dbParams)
if err != nil {
return nil, err
}
func (f *playBotFactory) GetBot(source string) Bot {
// give DB as arg
db := &DummyDb{}
return NewPlayBot(source, db, f.readers)
return &playBotFactory{
db: db,
readers: readers,
}, nil
}
type DummyDb struct{}
func (db *DummyDb) Create(value interface{}) Db {
log.Printf("Create(): %q", value)
return db
func (f *playBotFactory) GetBot(source string) Bot {
return NewPlayBot(source, f.db, f.readers)
}
......@@ -11,8 +11,7 @@ type Post struct {
Date time.Time
Author string `gorm:"column:sender_irc"`
Source string `gorm:"column:chan"`
Content site.Content
Tag Tag
Content *site.Content
}
func (Post) TableName() string {
......
package bot
type Tag struct {
Tag string
ContentId int `gorm:"colmun:id"`
}
......@@ -83,7 +83,10 @@ func main() {
readers = append(readers, reader)
}
factory := bot.NewPlayBotFactory(config.Db, readers)
factory, err := bot.NewPlayBotFactory(config.Db, readers)
if err != nil {
log.Fatal(err)
}
for name, config := range config.Transports {
c := startTransport(name, config, factory)
......
......@@ -9,10 +9,17 @@ type Content struct {
IsCollection bool `gorm:"column:playlist"`
Source string `gorm:"column:type"`
SourceId string `gorm:"column:external_id"`
Tags []Tag
Title string
Url string
}
func (Content) Table() string {
func (Content) TableName() string {
return "playbot"
}
func (c *Content) AddTags(tags []string) {
for _, tag := range tags {
c.Tags = append(c.Tags, Tag{Tag: tag})
}
}
......@@ -3,7 +3,6 @@ package site
import (
"errors"
"fmt"
"log"
"net/http"
"regexp"
......@@ -25,10 +24,8 @@ func NewYoutube(key string) (Reader, error) {
}
func (yt Youtube) Read(url string) (*Content, error) {
log.Print(url)
re := regexp.MustCompile(`(?:^|[^!])https?://(?:www.youtube.com/watch\?[a-zA-Z0-9_=&-]*v=|youtu.be/)([a-zA-Z0-9_-]+)`)
match := re.FindStringSubmatch(url)
log.Print(match)
if len(match) == 0 {
return nil, nil
}
......
......@@ -10,9 +10,9 @@ func (t *IrcTransport) connected(conn *irc.Conn, line *irc.Line) {
for _, channel := range t.channels {
t.Logger.Printf("Join %s", channel)
conn.Join(channel)
bot := t.botFactory.GetBot("irc.iiens.net")
t.bots[channel] = bot
bot.Add("lol.com", []string{})
}
}
......@@ -26,9 +26,10 @@ func (t *IrcTransport) privmsg(conn *irc.Conn, line *irc.Line) {
b := t.bots[channel]
contents := make(chan *site.Content)
go b.ParseLine(line.Nick, msg, contents)
source := t.getSourceName(channel)
go b.ParseLine(line.Nick, source, msg, contents)
for content := range contents {
t.printContent(conn, content, channel)
t.printNewContent(conn, content, channel)
}
}
......@@ -104,3 +104,7 @@ func (t *IrcTransport) Run() error {
return nil
}
func (t *IrcTransport) getSourceName(channel string) string {
return fmt.Sprintf("%s@%s", channel, t.Name)
}
......@@ -2,7 +2,6 @@ package irc
import (
"fmt"
"log"
irc "github.com/fluffle/goirc/client"
......@@ -36,7 +35,7 @@ const (
LIGHT_GREY string = "\x0315"
)
func (t *IrcTransport) printContent(conn *irc.Conn, content *site.Content, channel string) {
func (t *IrcTransport) printNewContent(conn *irc.Conn, content *site.Content, channel string) {
msg := fmt.Sprintf("%s[%d] %s%s", YELLOW, content.Id, GREEN, content.Title)
if content.Author != "" {
......@@ -61,11 +60,15 @@ func (t *IrcTransport) printContent(conn *irc.Conn, content *site.Content, chann
msg += fmt.Sprintf("%02d)%s", s, NORMAL)
}
msg += " => " + content.Url + ORANGE
msg += NORMAL
for _, tag := range content.Tags {
msg += " #" + tag.Tag
}
conn.Privmsg(channel, msg)
}
func (t *IrcTransport) printError(err error) {
log.Printf("Error: %s", err)
t.Logger.Printf("Error: %s", err)
}
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Veuillez vous inscrire ou vous pour commenter