Skip to content

Commit

Permalink
initial import
Browse files Browse the repository at this point in the history
  • Loading branch information
jondot committed Mar 6, 2016
0 parents commit 6f16186
Show file tree
Hide file tree
Showing 40 changed files with 12,421 additions and 0 deletions.
27 changes: 27 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
.DS_Store
.AppleDouble
.LSOverride

# Icon must end with two \r
Icon


# Thumbnails
._*

# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns

# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk
10bisbar*
*.tar.gz
22 changes: 22 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
VERSION=`date -u +v%Y%m%d.%H%M%S`

clean:
rm -f 10bisbar*
rm -f *.tar.gz

test:
@gom test

build: clean
go build -ldflags "-X main.version=$(VERSION) -s"
upx -9 10bisbar

dist: build
mv 10bisbar 10bisbar.15m.sh

release: build
tar czvf 10bisbar.$(VERSION).tar.gz 10bisbar
git tag -a $(VERSION) -m "release $(VERSION)"


.PHONY: test build dist release clean
50 changes: 50 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# 10bisbar

![](shot.png)

Quit your [10Bis](10bis.co.il) guesswork and never get stuck with an empty account.


## Quick Start

Download a binary from the [releases]() section. Then unzip it to your bitbar
plugins folder, and continue to the configuration section.


Or, if you prefer to build on your own and produce a binary. You'll need Go and
UPX (`brew install upx`).

`$ make dist`

Then point bitbar to this folder, or simply copy the `10bisbar` binary to your plugins folder.
There are no dependencies so you should be good to go immediately.

### Configuration

Place a file called `.10bis.json` at your home folder, so that it will be found in `~/.10bis.json`.

Copy this sample:

```json
{
"username":"your-10bis-user",
"password":"your-10bis-password",
"prices":[30, 42, 69, 80],
"no_food_days_per_week": 2
}
```

Put your username and password in, and then put your favorite meal price options in `prices`. Finally,
indicate how many days per week you don't use 10bis at all (typically weekends).


# Contributing
Fork, implement, add tests, pull request, get my everlasting thanks and a respectable place here :).


# Copyright

Copyright (c) 2014 [Dotan Nahum](http://gplus.to/dotan) [@jondot](http://twitter.com/jondot). See MIT-LICENSE for further details.



120 changes: 120 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package main

import(
"path/filepath"
"os/user"
"io/ioutil"
"encoding/json"
"time"
"strings"
"github.com/PuerkitoBio/goquery"
"net/http/cookiejar"
"net/http"
"log"
"fmt"
"bytes"
"strconv"
)

var version = "master"

type Settings struct {
Username string `json:"username"`
Password string `json:"password"`
Prices []int `json:"prices"`
NoFoodDaysPerWeek int `json:"no_food_days_per_week"`
}

func daysLeft(date time.Time) int {
year, m, d := date.Date()
daysInMonth := time.Date(year, m+1, 0, 0, 0, 0, 0, time.UTC).Day()

return daysInMonth - d
}

func buildPredictions(budget float64, settings *Settings) string {
if budget <= 0 {
return ""
}
var buffer bytes.Buffer

// food days left.
// compute number of days left in this month, reduce the
// no-food days per week
daysLeftThisMonth := daysLeft(time.Now())
mealdays := daysLeftThisMonth - (daysLeftThisMonth / 7) * settings.NoFoodDaysPerWeek

buffer.WriteString(fmt.Sprintf("🍔 you have to eat for %v more days.\n---\n", mealdays))
for _,price := range(settings.Prices){
// how much will be off our current budget for this price?
prediction := budget - float64(mealdays * price)
gooddays := int(budget/float64(price))

if mealdays <= gooddays {
buffer.WriteString(fmt.Sprintf("₪%v: %v (₪+%v left)", price, gooddays, prediction))
}else {
buffer.WriteString(fmt.Sprintf("₪%v: %v + %v for ₪%v", price, gooddays, mealdays - gooddays, prediction*-1))
}
if prediction < 0 {
buffer.WriteString("|color=red")
}
buffer.WriteString("\n")
}
return buffer.String()
}

func main(){
u, err := user.Current()
if err != nil{
log.Fatal(err)
}

dot10bis := filepath.Join(u.HomeDir, ".10bis.json")
data, err := ioutil.ReadFile(dot10bis)
if err != nil{
log.Fatal(err)
}

settings := &Settings{}
json.Unmarshal(data, settings)

options := &cookiejar.Options{}
jar, err := cookiejar.New(options)
if err != nil {
log.Fatal(err)
}
client := http.Client{Jar: jar}
resp, err := client.Post(
"https://www.10bis.co.il/Account/LogonAjax",
"application/json",
strings.NewReader(
fmt.Sprintf("{\"timestamp\":%d,\"model\":{\"UserName\":\"%s\",\"Password\":\"%s\",\"SocialLoginUID\":\"\",\"FacebookUserId\":\"undefined\"},\"returnUrl\":\"\"}",
time.Now().Unix(),
settings.Username,
settings.Password,),
),
)
if err != nil {
log.Fatal(err)
}
resp, err = client.Get("https://www.10bis.co.il/Account/UserReport")
if err != nil {
log.Fatal(err)
}

doc,err := goquery.NewDocumentFromReader(resp.Body)
if err != nil {
log.Fatal(err)
}

res := doc.Find(".userReportDataTbl th.currency").First()
prettyAmount := strings.TrimSpace(res.Text())

budget, err := strconv.ParseFloat(strings.Replace(prettyAmount, "₪", "", -1), 64)
if err != nil {
log.Fatal(err)
}
submenu := buildPredictions(budget, settings)
fmt.Printf("%s\n---\n%s---\n%s", prettyAmount, submenu, version)
}

14 changes: 14 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package main
import (
. "gopkg.in/check.v1"
)

type CLISuite struct{}
var _ = Suite(&CLISuite{})

func (s *CLISuite) TestFoobar(c *C) {
//should fail - fix me!
c.Check(42, Equals, 0)
}


Binary file added shot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions test_helper_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package main
import (
"testing"
. "gopkg.in/check.v1"
)

func Test(t *testing.T){ TestingT(t) }



12 changes: 12 additions & 0 deletions vendor/github.com/PuerkitoBio/goquery/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

116 changes: 116 additions & 0 deletions vendor/github.com/PuerkitoBio/goquery/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 6f16186

Please sign in to comment.