Skip to content

Commit 2bbdc64

Browse files
committed
Implemented manifest reader
1 parent 470740d commit 2bbdc64

File tree

8 files changed

+190
-63
lines changed

8 files changed

+190
-63
lines changed

.gitignore

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
dist
2-
test
1+
dist/
2+
test/
3+
.vscode

src/constants.go

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package main
2+
3+
const userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36"

src/main.go

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
package main
22

3+
import (
4+
"fmt"
5+
)
6+
37
func main() {
4-
watch("./test", GetSession())
8+
// session := GetSessionWithCredentials(TEST_USERNAME, TEST_PASSWORD)
9+
manifest, err := RetreiveManifestFromReddit("77346c3e708a")
10+
11+
if err != nil {
12+
fmt.Println("error: ", err)
13+
} else {
14+
fmt.Printf("%+v\n", manifest)
15+
}
516
}

src/manifest.go

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"errors"
6+
"fmt"
7+
"io/ioutil"
8+
"net/http"
9+
)
10+
11+
// Manifest data structure describing the location of a file
12+
// {
13+
// "files": [
14+
// {
15+
// "name": "test.txt",
16+
// "path": [
17+
// "7zp1o6"
18+
// ]
19+
// }
20+
// ]
21+
// }
22+
type Manifest struct {
23+
Files []struct {
24+
Name string `json:"name"`
25+
Path []string `json:"path"`
26+
} `json:"files"`
27+
}
28+
29+
// CreateManifestFromByteArray creates a Manifest object from a byte array
30+
func CreateManifestFromByteArray(JSON []byte) (Manifest, error) {
31+
var m Manifest
32+
err := json.Unmarshal(JSON, &m)
33+
34+
return m, err
35+
}
36+
37+
// CreateManifestFromString creates a manifest object from a JSON string
38+
func CreateManifestFromString(JSON string) (Manifest, error) {
39+
var m Manifest
40+
err := json.Unmarshal([]byte(JSON), &m)
41+
42+
return m, err
43+
}
44+
45+
// RetreiveManifestFromReddit will download a manifest from a specified subreddit
46+
func RetreiveManifestFromReddit(subreddit string) (Manifest, error) {
47+
// https://www.reddit.com/r/[repo]/search.json?q=manifest.json&restrict_sr=on&sort=relevance&t=all
48+
var m Manifest
49+
url := fmt.Sprintf(`https://www.reddit.com/r/%v/search.json?q=manifest&restrict_sr=on&sort=relevance&t=all`, subreddit)
50+
51+
// response, err := client.Get(request)
52+
request, err := http.NewRequest("GET", url, nil)
53+
if err != nil {
54+
return m, err
55+
}
56+
request.Header.Set("User-Agent", userAgent)
57+
client := &http.Client{}
58+
59+
response, err := client.Do(request)
60+
if err != nil {
61+
return m, err
62+
}
63+
defer response.Body.Close()
64+
65+
if response.StatusCode != http.StatusOK {
66+
fmt.Println(request)
67+
return m, fmt.Errorf(`bad status code, [%v]`, response.StatusCode)
68+
}
69+
70+
JSON, err := ioutil.ReadAll(response.Body)
71+
if err != nil {
72+
return m, err
73+
}
74+
75+
search, err := CreateSearchFromByteArray(JSON)
76+
if len(search.Data.Children) == 0 {
77+
return m, errors.New("could not locate manifest.json")
78+
}
79+
80+
for _, listing := range search.Data.Children {
81+
if listing.Data.Subreddit == subreddit && listing.Data.Title == "manifest.json" {
82+
return CreateManifestFromString(listing.Data.Text)
83+
}
84+
}
85+
86+
return m, errors.New("could not locate manifest.json")
87+
}

src/search.go

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package main
2+
3+
import "encoding/json"
4+
5+
// Search data structure describing the results of a subreddit search
6+
// {
7+
// "data": {
8+
// "children": [
9+
// {
10+
// "kind": "t3",
11+
// "data": {
12+
// "subreddit": "77346c3e708a",
13+
// "selftext": "test",
14+
// "id": "7zpvqn",
15+
// "author": "Senior-Jesticle",
16+
// "score": 1,
17+
// "name": "t3_7zpvqn",
18+
// "url": "https://www.reddit.com/r/77346c3e708a/comments/7zpvqn/manifestjson/",
19+
// "title": "manifest.json"
20+
// }
21+
// }
22+
// ]
23+
// }
24+
// }
25+
type Search struct {
26+
Data struct {
27+
Children []struct {
28+
Kind string `json:"kind"`
29+
Data struct {
30+
Subreddit string `json:"subreddit"`
31+
Text string `json:"selftext"`
32+
ID string `json:"id"`
33+
Author string `json:"author"`
34+
Score int `json:"score"`
35+
Name string `json:"name"`
36+
URL string `json:"url"`
37+
Title string `json:"title"`
38+
} `json:"data"`
39+
} `json:"children"`
40+
} `json:"data"`
41+
}
42+
43+
// CreateSearchFromByteArray creates a Search object from a byte array
44+
func CreateSearchFromByteArray(JSON []byte) (Search, error) {
45+
var s Search
46+
err := json.Unmarshal(JSON, &s)
47+
48+
return s, err
49+
}
50+
51+
// CreateSearchFromJSON creates a Search object from a JSON string
52+
func CreateSearchFromJSON(JSON string) (Search, error) {
53+
var s Search
54+
err := json.Unmarshal([]byte(JSON), &s)
55+
56+
return s, err
57+
}

src/session.go

+22
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,25 @@ func GetSession() *geddit.OAuthSession {
2929

3030
return session
3131
}
32+
33+
// GetSessionWithCredentials creates geddit session with provided credentials
34+
func GetSessionWithCredentials(username string, password string) *geddit.OAuthSession {
35+
session, err := geddit.NewOAuthSession(
36+
"gNl1rziyJUjwNQ",
37+
"TdeaiSX6FaxwBsfpi9L6FxFu288",
38+
"redditfs",
39+
"http://maxchehab.com",
40+
)
41+
42+
if err != nil {
43+
log.Fatal(err)
44+
}
45+
46+
// Create new auth token for confidential clients (personal scripts/apps).
47+
err = session.LoginAuth(username, password)
48+
if err != nil {
49+
log.Fatal(err)
50+
}
51+
52+
return session
53+
}

src/upload.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,18 @@ package main
22

33
import "github.com/maxchehab/geddit"
44

5-
func remove(file string, session *geddit.OAuthSession) {
5+
// Remove deletes a file from reddit
6+
func Remove(file string, session *geddit.OAuthSession) {
67

78
}
89

9-
func create(file string, session *geddit.OAuthSession) {
10+
// Create uploads a file from reddit
11+
func Create(file string, session *geddit.OAuthSession) {
1012
session.Submit(geddit.NewTextSubmission("77346c3e708a", "title", "hello world", false, nil))
1113

1214
}
1315

14-
func change(file string, session *geddit.OAuthSession) {
16+
// Change edits a file from reddit
17+
func Change(file string, session *geddit.OAuthSession) {
1518
session.EditUserText(geddit.NewEdit("this is an edit", "t3_7pni8t"))
1619
}

src/watch.go

-57
This file was deleted.

0 commit comments

Comments
 (0)