-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit f6b2d5e
Showing
6 changed files
with
141 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package firebase | ||
|
||
import ( | ||
"capAble-scrapper/pkg/objects" | ||
"context" | ||
"log" | ||
|
||
"cloud.google.com/go/firestore" | ||
firebase "firebase.google.com/go" | ||
|
||
"google.golang.org/api/option" | ||
) | ||
|
||
type FireBaseSvcImpl struct { | ||
FirestoreClient *firestore.Client | ||
} | ||
|
||
type FireBaseSvc interface { | ||
WriteToFirebase(data []objects.Opportunity) error | ||
ReadFromFirebase() error | ||
} | ||
|
||
func NewService() FireBaseSvc { | ||
opt := option.WithCredentialsFile(".firebase-cred.json") | ||
app, err := firebase.NewApp(context.Background(), nil, opt) | ||
if err != nil { | ||
log.Fatalf("error initializing app: %v", err) | ||
return nil | ||
} | ||
firestoreClient, err := app.Firestore(context.Background()) | ||
if err != nil { | ||
log.Fatalf("error initializing firestore: %v", err) | ||
return nil | ||
} | ||
|
||
return &FireBaseSvcImpl{ | ||
FirestoreClient: firestoreClient, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package firebase | ||
|
||
func (fb *FireBaseSvcImpl) ReadFromFirebase() error { | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package firebase | ||
|
||
import ( | ||
"capAble-scrapper/pkg/crypto" | ||
"capAble-scrapper/pkg/objects" | ||
"context" | ||
"fmt" | ||
) | ||
|
||
func (fb *FireBaseSvcImpl) WriteToFirebase(data []objects.Opportunity) error { | ||
collectionName := "opportunities" | ||
|
||
batch := fb.FirestoreClient.BulkWriter(context.Background()) | ||
opportunitiesCollection := fb.FirestoreClient.Collection(collectionName) | ||
|
||
fmt.Printf("Received %d\n", len(data)) | ||
for count, obj := range data { | ||
if obj.Company == "" { | ||
continue | ||
} | ||
docID, err := crypto.HashObject(obj) | ||
if err != nil { | ||
return err | ||
} | ||
docRef := opportunitiesCollection.Doc(docID) | ||
_, err = batch.Create(docRef, obj) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
fmt.Printf("Writing Object: %d\n", count) | ||
} | ||
|
||
batch.End() | ||
fmt.Println("Objects written to Firestore.") | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package database | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"go.mongodb.org/mongo-driver/mongo" | ||
"go.mongodb.org/mongo-driver/mongo/options" | ||
"go.mongodb.org/mongo-driver/mongo/readpref" | ||
) | ||
|
||
func Connection() *mongo.Client { | ||
options := options.Client().ApplyURI("mongodb://user:password@localhost:27017") | ||
client, err := mongo.Connect(context.TODO(), options) | ||
if err != nil { | ||
fmt.Println(err) | ||
} | ||
if err := client.Ping(context.TODO(), readpref.Primary()); err != nil { | ||
panic(err) | ||
} | ||
return client | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package crypto | ||
|
||
import ( | ||
"crypto/sha256" | ||
"encoding/hex" | ||
"encoding/json" | ||
) | ||
|
||
func HashObject(object interface{}) (string, error) { | ||
hasher := sha256.New() | ||
serializedObject, err := json.Marshal(object) | ||
if err != nil { | ||
return "", err | ||
} | ||
hasher.Write(serializedObject) | ||
return hex.EncodeToString(hasher.Sum(nil)), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package objects | ||
|
||
import "time" | ||
|
||
type Opportunity struct { | ||
Company string | ||
CompanyLogo string | ||
CompanyURL string | ||
JobPostingUrl string | ||
Location string | ||
Industry string | ||
Role string | ||
Description string | ||
Experience string | ||
SeniorityLevel string | ||
EmploymentType string | ||
Job string | ||
Pay string | ||
Posted time.Time | ||
} |