about summary refs log tree commit diff
path: root/tools/convertdb/convertdb.go
blob: adef31910bf117c0b7f1c50c16170a5950ef6cb7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package main

import (
	"strconv"
	"fmt"
	"io/ioutil"
	"json"
	"./couch"
	"os"
	"time"
)

//old
type OldComment struct {
	Author string
	Text   string
	Date   string
}

type OldEntry struct {
	Id       string
	Title    string
	Author   string
	Text     string
	Mtext    string
	Comments []OldComment
}

//new
type Comment struct {
	Author string 	`json:"cauthor"`
	Text   string 	`json:"ctext"`
	Date   int64 	`json:"cdate"`
}

type Entry struct {
	Id       string `json:"_id"`
	Year     int    `json:"year"`
	Month    int    `json:"month"`
	Day		 int
	Lang     string `json:"lang"`
	Title    string `json:"title"`
	Author   string `json:"author"`
	Text     string `json:"text"`
	Mtext    string `json:"mtext"`
	Comments []Comment 	`json:"comments"`
}

func main() {
	getAllByYear("2011", 8, 12)
	getAllByYear("2012", 1, 2)
}

func getAllByYear(year string, minm, maxm int){
	db, _ := couch.NewDatabase("127.0.0.1", "5984", "tazblog")
	for i:=minm;i<=maxm;i++{
		dirList, err := ioutil.ReadDir(fmt.Sprintf("data/%s/%02d/", year, i))
		if err != nil {
			fmt.Println(err.String())
			os.Exit(1)
		}
		for d:=len(dirList)-1; d>-1; d--{
			content, cErr := ioutil.ReadFile(fmt.Sprintf("data/%s/%02d/%s", year, i, dirList[d].Name))
			if cErr != nil {
				fmt.Println(cErr)
				os.Exit(1)
			}
			var oEntry OldEntry
			jErr := json.Unmarshal(content, &oEntry)
			if jErr != nil {
				fmt.Println(jErr.String())
				os.Exit(1)
			}
			nEntry := convertEntry(oEntry, fmt.Sprintf("data/%s/%02d/%s", year, i, dirList[d].Name))
			eId, _, err := db.Insert(nEntry)
			if err != nil {
				fmt.Println(err.String())
				os.Exit(1)
			}
			fmt.Println("Inserted " + eId)
		}		
	}
}

func convertEntry(oEntry OldEntry, p string) Entry{
	var nEntry Entry
	nComments := make([]Comment, len(oEntry.Comments))
	for i:=0;i<len(oEntry.Comments);i++{
		nComments[i].Author = oEntry.Comments[i].Author
		nComments[i].Text = oEntry.Comments[i].Text
		nComments[i].Date = parseDumbTime(oEntry.Comments[i].Date)
	}

	nEntry.Id 		= oEntry.Id[3:]
	nEntry.Year, _  = strconv.Atoi(p[5:9])
	nEntry.Month, _ = strconv.Atoi(p[10:12])
	nEntry.Day, _	= strconv.Atoi(p[13:15])
	nEntry.Title  	= oEntry.Title
	nEntry.Author 	= oEntry.Author
	nEntry.Mtext 	= oEntry.Mtext
	nEntry.Text 	= oEntry.Text
	nEntry.Comments = nComments
	nEntry.Lang 	= "de"

	return nEntry
}

//http://tazj.in/2012/02/10.155234
func parseEntryTime(year, month, day int, ids string) string {
	x := fmt.Sprintf()
}

func parseDumbTime(Year, Month, Day int, ) int64 {
	x, err := time.Parse("[Am 02.01.2006 um 15:04 Uhr]", ct)
	if err != nil {
		fmt.Println(err.String())
		os.Exit(1)
	}

	return x.Seconds()
}