-
Notifications
You must be signed in to change notification settings - Fork 0
/
manifest.go
171 lines (150 loc) · 4.18 KB
/
manifest.go
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package main
import (
"encoding/json"
"fmt"
"os"
"strconv"
"github.com/maxchehab/geddit"
)
// Manifest data structure describing the location of a file
// {
// "repositories":[
// {
// "name":"My dankeroni repository",
// "files":[
// {
// "name":"test.txt",
// "path":"/",
// "location":[
// "7zp1o6"
// ]
// }
// ]
// }
// ]
// }
type Manifest struct {
Repositories []Repository `json:"repositories"`
Location string
}
// Repository structure
type Repository struct {
Name string `json:"name"`
Files []File `json:"files"`
}
// File structure
type File struct {
Name string `json:"name"`
Path string `json:"path"`
Location []string `json:"location"`
}
// Contains checks if a manifest contians selected repository
func (m *Manifest) Contains(repository Repository) bool {
for _, repo := range m.Repositories {
if repo.Name == repository.Name {
return true
}
}
return false
}
// Update updeates a manifest's repository given a provided repository
func (m *Manifest) Update(repository Repository) {
for index := range m.Repositories {
if m.Repositories[index].Name == repository.Name {
m.Repositories[index] = repository
}
}
}
// ToString converts a selected manifest to a JSON string
func (m Manifest) ToString() string {
b, _ := json.Marshal(m)
return string(b)
}
// UploadBuffer uploads a buffer of data and modifies the file object
func (f *File) UploadBuffer(buffer []byte, session *geddit.OAuthSession, subreddit string) (err error) {
text := ""
for _, b := range buffer {
o := strconv.Itoa(int(b))
text += o + " "
}
submission, err := session.Submit(geddit.NewTextSubmission(subreddit, subreddit, text, false, nil))
if err != nil {
return
}
f.Location = append(f.Location, submission.ID)
return
}
// CreateManifestFromByteArray creates a Manifest object from a byte array
func CreateManifestFromByteArray(JSON []byte) (Manifest, error) {
var m Manifest
err := json.Unmarshal(JSON, &m)
return m, err
}
// CreateManifestFromString creates a manifest object from a JSON string
func CreateManifestFromString(JSON string) (Manifest, error) {
var m Manifest
err := json.Unmarshal([]byte(JSON), &m)
return m, err
}
// FilterReposByNames filters repositories with a collection of names.
func (m Manifest) FilterReposByNames(responses []string) (repositories []Repository) {
for _, repo := range m.Repositories {
for _, response := range responses {
if repo.Name == response {
repositories = append(repositories, repo)
}
}
}
return
}
// Download a file
func (f File) Download(path string, session *geddit.OAuthSession) (err error) {
if _, err := os.Stat(path); err == nil {
os.Remove(path)
}
for _, location := range f.Location {
url := fmt.Sprintf(`https://oauth.reddit.com/r/77346c3e708a/comments/%v.json`, location)
JSON, err := session.GetRawRequest(url)
if err != nil {
return err
}
listing, err := CreateListingFromByteArray(JSON)
err = WriteByteStringToFile(listing.Text, path)
if err != nil {
return err
}
}
return
}
// Download a repository
func (r Repository) Download(path string, session *geddit.OAuthSession) (err error) {
for _, file := range r.Files {
Prompt(fmt.Sprintf("Downloading /%v%v%v", r.Name, file.Path, file.Name))
file.Download(path+"/"+r.Name+file.Path+file.Name, session)
}
return
}
// RetrieveManifestFromReddit will download a manifest from a specified subreddit
func RetrieveManifestFromReddit(subreddit string, session *geddit.OAuthSession) (m Manifest, err error) {
// https://oauth.reddit.com/r/[repo]/search.json?q=manifest.json&restrict_sr=on&sort=relevance&t=all
url := fmt.Sprintf(`https://oauth.reddit.com/r/%v/search.json?q=manifest&restrict_sr=on&sort=relevance&t=all`, subreddit)
JSON, err := session.GetRawRequest(url)
if err != nil {
return
}
search, err := CreateSearchFromByteArray(JSON)
if len(search.Data.Children) == 0 {
return m, nil
}
for _, listing := range search.Data.Children {
if listing.Data.Subreddit == subreddit && listing.Data.Title == "manifest.json" {
m, err := CreateManifestFromString(listing.Data.Text)
if err != nil {
return m, err
}
m.Location = listing.Data.ID
return m, err
}
}
return
}