Compare commits
No commits in common. "master" and "3fdc170db865f3db687b21b00760aab6f9c2681e" have entirely different histories.
master
...
3fdc170db8
|
@ -1,3 +1,10 @@
|
|||
package main
|
||||
|
||||
const USER_AGENT string = "linux:omordl:v0.2.0 (by u/the_blank_x)"
|
||||
const (
|
||||
CONFIG_PLACEHOLDER string = "https://old.reddit.com/prefs/apps"
|
||||
USER_AGENT string = "linux:omordl:v0.1.0 (by u/the_blank_x)"
|
||||
EXAMPLE_CONFIG string = `{
|
||||
"client_id": "https://old.reddit.com/prefs/apps",
|
||||
"client_secret": "https://old.reddit.com/prefs/apps"
|
||||
}`
|
||||
)
|
||||
|
|
72
main.go
72
main.go
|
@ -11,13 +11,14 @@ import (
|
|||
"os/exec"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
func main() {
|
||||
if len(os.Args) != 2 && len(os.Args) != 3 {
|
||||
fmt.Fprintf(os.Stderr, "Usage: %s <submission id/url> [direct url to download instead of inferring from the post]\n", os.Args[0])
|
||||
if len(os.Args) != 2 {
|
||||
fmt.Fprintf(os.Stderr, "Usage: %s <submission id/url>\n", os.Args[0])
|
||||
os.Exit(1)
|
||||
}
|
||||
LoadMimetypes()
|
||||
|
@ -26,15 +27,10 @@ func main() {
|
|||
fmt.Fprintf(os.Stderr, "Error when parsing submission url: %s\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
submissionJsonUrl := ""
|
||||
submissionId := os.Args[1]
|
||||
if submissionUrl.Hostname() == "redd.it" {
|
||||
submissionJsonUrl = "https://old.reddit.com" + submissionUrl.EscapedPath() + ".json?raw_json=1&limit=1"
|
||||
submissionId = submissionUrl.EscapedPath()[1:]
|
||||
} else if submissionUrl.Hostname() == "" {
|
||||
submissionJsonUrl = "https://old.reddit.com/" + os.Args[1] + ".json?raw_json=1&limit=1"
|
||||
} else {
|
||||
submissionJsonUrl = "https://old.reddit.com" + submissionUrl.EscapedPath() + "/.json?raw_json=1&limit=1"
|
||||
} else if submissionUrl.Hostname() != "" {
|
||||
split := strings.SplitN(submissionUrl.EscapedPath(), "/", 6)
|
||||
if len(split) < 5 {
|
||||
fmt.Fprintln(os.Stderr, "URL passed does not have enough path seperators")
|
||||
|
@ -42,8 +38,24 @@ func main() {
|
|||
}
|
||||
submissionId = split[4]
|
||||
}
|
||||
client := &http.Client{}
|
||||
submission, err := GetSubmission(client, submissionJsonUrl)
|
||||
if submissionId == "" {
|
||||
fmt.Fprintln(os.Stderr, "Submission ID is empty")
|
||||
os.Exit(1)
|
||||
}
|
||||
client := &http.Client{
|
||||
Timeout: time.Duration(30 * time.Second),
|
||||
}
|
||||
err = LoadConfigAndData()
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Failed to load config and data: %s\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
token, err := GetToken(client)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Failed to get token: %s\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
submission, err := GetSubmission(client, token, submissionId)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Failed to get submission: %s\n", err)
|
||||
os.Exit(1)
|
||||
|
@ -53,7 +65,7 @@ func main() {
|
|||
filename = "_" + filename[1:]
|
||||
}
|
||||
if submission.CrosspostParent != "" && len(submission.CrosspostParentList) > 0 {
|
||||
submission, err = GetSubmission(client, "https://old.reddit.com"+submission.CrosspostParentList[0].Permalink+".json?raw_json=1&limit=1")
|
||||
submission, err = GetSubmission(client, token, submission.CrosspostParent[3:])
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Failed to get original submission: %s\n", err)
|
||||
os.Exit(1)
|
||||
|
@ -138,7 +150,7 @@ func main() {
|
|||
if domain == "imgur.com" || strings.HasSuffix(domain, ".imgur.com") {
|
||||
parsedUrl.Host = "i.imgur.com"
|
||||
if strings.HasPrefix(path, "/a/") || strings.HasPrefix(path, "/gallery/") {
|
||||
albumId := strings.SplitN(strings.SplitN(path, "/", 4)[2], ".", 2)[0]
|
||||
albumId := strings.SplitN(path, "/", 4)[2]
|
||||
imgurImages, err := GetImgurAlbum(client, albumId)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Failed to get imgur album: %s\n", err)
|
||||
|
@ -203,11 +215,7 @@ func main() {
|
|||
break
|
||||
}
|
||||
}
|
||||
if len(os.Args) == 3 {
|
||||
unparsedUrl = os.Args[2]
|
||||
} else {
|
||||
unparsedUrl = urls[i]
|
||||
}
|
||||
response, err := client.Get(unparsedUrl)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Failed to get response: %s\n", err)
|
||||
|
@ -229,28 +237,38 @@ func main() {
|
|||
writer := bufio.NewWriter(file)
|
||||
defer writer.Flush()
|
||||
reader := bufio.NewReader(response.Body)
|
||||
contentLengthString := ""
|
||||
if response.ContentLength >= 0 {
|
||||
contentLengthString = FormatBytes(float64(response.ContentLength))
|
||||
}
|
||||
lastOutputLength := 0
|
||||
bytesDownloaded := 0
|
||||
toWrite := make([]byte, 1024*1024)
|
||||
for {
|
||||
lastOutputLength = PrintStatus(float64(bytesDownloaded), float64(response.ContentLength), lastOutputLength)
|
||||
output := fmt.Sprintf("%s downloaded", FormatBytes(float64(bytesDownloaded)))
|
||||
if response.ContentLength >= 0 {
|
||||
output = fmt.Sprintf("%s out of %s downloaded (%.2f%%)", FormatBytes(float64(bytesDownloaded)), contentLengthString, float64(bytesDownloaded)/float64(response.ContentLength)*100.0)
|
||||
}
|
||||
fmt.Print(output)
|
||||
for i := 0; i < lastOutputLength-len(output); i++ {
|
||||
fmt.Print(" ")
|
||||
}
|
||||
lastOutputLength = len(output)
|
||||
fmt.Print("\r")
|
||||
n, err := reader.Read(toWrite)
|
||||
if n > 0 {
|
||||
_, writeErr := writer.Write(toWrite[:n])
|
||||
if writeErr != nil {
|
||||
fmt.Fprintf(os.Stderr, "Failed to write response: %s\n", writeErr)
|
||||
os.Exit(1)
|
||||
}
|
||||
bytesDownloaded += n
|
||||
lastOutputLength = PrintStatus(float64(bytesDownloaded), float64(response.ContentLength), lastOutputLength)
|
||||
}
|
||||
if errors.Is(err, io.EOF) {
|
||||
if n == 0 && errors.Is(err, io.EOF) {
|
||||
break
|
||||
}
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Failed to read response: %s\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
_, err = writer.Write(toWrite[:n])
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Failed to write response: %s\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
bytesDownloaded += n
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
|
21
structs.go
21
structs.go
|
@ -1,5 +1,21 @@
|
|||
package main
|
||||
|
||||
type Config struct {
|
||||
ClientId string `json:"client_id"`
|
||||
ClientSecret string `json:"client_secret"`
|
||||
}
|
||||
|
||||
type Data struct {
|
||||
AuthorizationHeader string `json:"authorization_header"`
|
||||
AuthorizationExpiry int64 `json:"authorization_expiry"`
|
||||
}
|
||||
|
||||
type TokenResponse struct {
|
||||
AccessToken string `json:"access_token"`
|
||||
TokenType string `json:"token_type"`
|
||||
ExpiresIn int64 `json:"expires_in"`
|
||||
}
|
||||
|
||||
type GalleryData struct {
|
||||
Items []struct {
|
||||
Id int `json:"id"`
|
||||
|
@ -23,9 +39,8 @@ type PreviewSource struct {
|
|||
|
||||
type Submission struct {
|
||||
CrosspostParent string `json:"crosspost_parent"`
|
||||
CrosspostParentList []struct {
|
||||
Permalink string `json:"permalink"`
|
||||
} `json:"crosspost_parent_list"`
|
||||
// we don't care about the list, just does it exist or not
|
||||
CrosspostParentList []interface{} `json:"crosspost_parent_list"`
|
||||
Url string `json:"url"`
|
||||
IsVideo bool `json:"is_video"`
|
||||
SecureMedia struct {
|
||||
|
|
132
utils.go
132
utils.go
|
@ -6,13 +6,74 @@ import (
|
|||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/fs"
|
||||
"mime"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
var mimeOverride map[string]string
|
||||
var (
|
||||
configDir string
|
||||
config *Config
|
||||
data *Data
|
||||
mimeOverride map[string]string
|
||||
)
|
||||
|
||||
func LoadConfigAndData() error {
|
||||
iHateBugs, err := os.UserConfigDir()
|
||||
if err != nil {
|
||||
return fmt.Errorf("Cannot determine config directory: %s", err)
|
||||
}
|
||||
configDir = filepath.Join(iHateBugs, "omordl")
|
||||
err = os.MkdirAll(configDir, 0o700)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Cannot create config directory: %s", err)
|
||||
}
|
||||
configFile, err := os.OpenFile(filepath.Join(configDir, "config.json"), os.O_RDONLY, 0o600)
|
||||
if err == nil {
|
||||
contents, err := io.ReadAll(configFile)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to read config file: %s", err)
|
||||
}
|
||||
err = json.Unmarshal(contents, &config)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to parse config file: %s", err)
|
||||
}
|
||||
} else if errors.Is(err, fs.ErrNotExist) {
|
||||
configFile, err = os.OpenFile(filepath.Join(configDir, "config.json"), os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0o600)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to open config file for writing default template: %s", err)
|
||||
}
|
||||
_, err = configFile.WriteString(EXAMPLE_CONFIG)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to write to config file: %s", err)
|
||||
}
|
||||
return fmt.Errorf("Please fill out the default template at %s", filepath.Join(configDir, "config.json"))
|
||||
} else {
|
||||
return fmt.Errorf("Failed to open config file: %s", err)
|
||||
}
|
||||
if config.ClientId == CONFIG_PLACEHOLDER || config.ClientSecret == CONFIG_PLACEHOLDER {
|
||||
return fmt.Errorf("Please fill out the default template at %s", filepath.Join(configDir, "config.json"))
|
||||
}
|
||||
dataFile, err := os.OpenFile(filepath.Join(configDir, "data.json"), os.O_RDONLY, 0o600)
|
||||
if err == nil {
|
||||
contents, err := io.ReadAll(dataFile)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to read data file: %s", err)
|
||||
}
|
||||
err = json.Unmarshal(contents, &data)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to parse data file: %s", err)
|
||||
}
|
||||
} else if !errors.Is(err, fs.ErrNotExist) {
|
||||
return fmt.Errorf("Failed to open data file: %s", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// a jpeg file can have a jpe extension and i personally don't like it
|
||||
// https://github.com/LonamiWebs/Telethon/blob/2e1be01ad4f6462de2e9e1f96a33537e51f44980/telethon/utils.py#L33
|
||||
|
@ -38,12 +99,63 @@ func LoadMimetypes() {
|
|||
mimeOverride["audio/flac"] = ".flac"
|
||||
}
|
||||
|
||||
func GetSubmission(client *http.Client, submissionJsonUrl string) (*Submission, error) {
|
||||
request, err := http.NewRequest("GET", submissionJsonUrl, nil)
|
||||
func WriteData() error {
|
||||
contents, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
file, err := os.OpenFile(filepath.Join(configDir, "data.json"), os.O_WRONLY|os.O_CREATE, 0o600)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = file.Write(contents)
|
||||
return err
|
||||
}
|
||||
|
||||
func GetToken(client *http.Client) (string, error) {
|
||||
if data.AuthorizationHeader != "" && data.AuthorizationExpiry != 0 && data.AuthorizationExpiry > time.Now().Unix() {
|
||||
return data.AuthorizationHeader, nil
|
||||
}
|
||||
request, err := http.NewRequest("POST", "https://www.reddit.com/api/v1/access_token?grant_type=client_credentials", nil)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("Failed to create request: %s", err)
|
||||
}
|
||||
request.Header.Add("User-Agent", USER_AGENT)
|
||||
request.SetBasicAuth(config.ClientId, config.ClientSecret)
|
||||
response, err := client.Do(request)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("Failed to get response: %s", err)
|
||||
}
|
||||
contents, err := io.ReadAll(response.Body)
|
||||
response.Body.Close()
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("Failed to read response body: %s", err)
|
||||
}
|
||||
if response.StatusCode != 200 {
|
||||
return "", fmt.Errorf("Response returned status code %d, body: %s", response.StatusCode, contents)
|
||||
}
|
||||
var tokenResponse TokenResponse
|
||||
err = json.Unmarshal(contents, &tokenResponse)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("Failed to parse response: %s", err)
|
||||
}
|
||||
s := []string{tokenResponse.TokenType, tokenResponse.AccessToken}
|
||||
data.AuthorizationHeader = strings.Join(s, " ")
|
||||
data.AuthorizationExpiry = time.Now().Unix() + tokenResponse.ExpiresIn - 5
|
||||
err = WriteData()
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Warning: Failed to save token to disk: %s\n", err)
|
||||
}
|
||||
return data.AuthorizationHeader, nil
|
||||
}
|
||||
|
||||
func GetSubmission(client *http.Client, token, submissionId string) (*Submission, error) {
|
||||
request, err := http.NewRequest("GET", "https://oauth.reddit.com/comments/"+submissionId+"/?raw_json=1&limit=1", nil)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Failed to create request: %s", err)
|
||||
}
|
||||
request.Header.Add("User-Agent", USER_AGENT)
|
||||
request.Header.Add("Authorization", data.AuthorizationHeader)
|
||||
response, err := client.Do(request)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Failed to get response: %s", err)
|
||||
|
@ -183,17 +295,3 @@ func GetExtension(mimeType string) (string, error) {
|
|||
fmt.Println(exts)
|
||||
return exts[0], nil
|
||||
}
|
||||
|
||||
func PrintStatus(bytesDownloaded, contentLength float64, lastOutputLength int) int {
|
||||
output := fmt.Sprintf("%s downloaded", FormatBytes(bytesDownloaded))
|
||||
if contentLength >= 0 {
|
||||
contentLengthString := FormatBytes(contentLength)
|
||||
output = fmt.Sprintf("%s out of %s downloaded (%.2f%%)", FormatBytes(bytesDownloaded), contentLengthString, bytesDownloaded/contentLength*100.0)
|
||||
}
|
||||
fmt.Print(output)
|
||||
for i := 0; i < lastOutputLength-len(output); i++ {
|
||||
fmt.Print(" ")
|
||||
}
|
||||
fmt.Print("\r")
|
||||
return len(output)
|
||||
}
|
||||
|
|
Reference in New Issue