-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
26 changed files
with
142 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,36 +4,68 @@ Copyright © 2024 Front Matter <[email protected]> | |
package cmd | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
|
||
"commonmeta-go/commonmeta" | ||
|
||
"commonmeta-go/crossref" | ||
|
||
"commonmeta-go/datacite" | ||
|
||
"commonmeta-go/types" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
// listCmd represents the list command | ||
var listCmd = &cobra.Command{ | ||
Use: "list", | ||
Short: "A brief description of your command", | ||
Long: `A longer description that spans multiple lines and likely contains examples | ||
and usage of using your command. For example: | ||
Short: "A list of works", | ||
Long: `A list of works. Currently only available for | ||
the Crossref and DataCite provider. Options include numnber of works, | ||
work type, and Crossref member id or DataCite client id. For example: | ||
Cobra is a CLI library for Go that empowers applications. | ||
This application is a tool to generate the needed files | ||
to quickly create a Cobra application.`, | ||
commonmeta list --number 10 --member 78 --type journal-article, | ||
commonmeta list --number 10 --member cern.zenodo --type dataset`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
fmt.Println("list called") | ||
number, _ := cmd.Flags().GetInt("number") | ||
from, _ := cmd.Flags().GetString("from") | ||
|
||
member, _ := cmd.Flags().GetString("member") | ||
type_, _ := cmd.Flags().GetString("type") | ||
hasORCID, _ := cmd.Flags().GetBool("has-orcid") | ||
hasROR, _ := cmd.Flags().GetBool("has-ror-id") | ||
hasReferences, _ := cmd.Flags().GetBool("has-references") | ||
hasRelation, _ := cmd.Flags().GetBool("has-relation") | ||
hasAbstract, _ := cmd.Flags().GetBool("has-abstract") | ||
hasAward, _ := cmd.Flags().GetBool("has-award") | ||
hasLicense, _ := cmd.Flags().GetBool("has-license") | ||
hasArchive, _ := cmd.Flags().GetBool("has-archive") | ||
|
||
var data []types.Data | ||
var err error | ||
sample := false | ||
if from == "crossref" { | ||
data, err = crossref.FetchCrossrefList(number, member, type_, sample, hasORCID, hasROR, hasReferences, hasRelation, hasAbstract, hasAward, hasLicense, hasArchive) | ||
} else if from == "datacite" { | ||
data, err = datacite.FetchDataciteList(number, sample) | ||
} | ||
if err != nil { | ||
fmt.Println(err) | ||
} | ||
output, jsErr := commonmeta.WriteCommonmetaList(data) | ||
var out bytes.Buffer | ||
json.Indent(&out, output, "=", "\t") | ||
fmt.Println(out.String()) | ||
|
||
if jsErr != nil { | ||
fmt.Println(jsErr) | ||
} | ||
}, | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(listCmd) | ||
|
||
// Here you will define your flags and configuration settings. | ||
|
||
// Cobra supports Persistent Flags which will work for this command | ||
// and all subcommands, e.g.: | ||
// listCmd.PersistentFlags().String("foo", "", "A help for foo") | ||
|
||
// Cobra supports local flags which will only run when this command | ||
// is called directly, e.g.: | ||
// listCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,10 +15,10 @@ import ( | |
"strings" | ||
"time" | ||
|
||
"github.com/front-matter/commonmeta-go/dateutils" | ||
"github.com/front-matter/commonmeta-go/doiutils" | ||
"github.com/front-matter/commonmeta-go/types" | ||
"github.com/front-matter/commonmeta-go/utils" | ||
"commonmeta-go/dateutils" | ||
"commonmeta-go/doiutils" | ||
"commonmeta-go/types" | ||
"commonmeta-go/utils" | ||
) | ||
|
||
type Content struct { | ||
|
@@ -247,10 +247,10 @@ func FetchCrossref(str string) (types.Data, error) { | |
return data, nil | ||
} | ||
|
||
func FetchCrossrefSample(number int, member string, _type string, hasORCID bool, hasROR bool, hasReferences bool, hasRelation bool, hasAbstract bool, hasAward bool, hasLicense bool, hasArchive bool) ([]types.Data, error) { | ||
func FetchCrossrefList(number int, member string, _type string, sample bool, hasORCID bool, hasROR bool, hasReferences bool, hasRelation bool, hasAbstract bool, hasAward bool, hasLicense bool, hasArchive bool) ([]types.Data, error) { | ||
|
||
var data []types.Data | ||
content, err := GetCrossrefSample(number, member, _type, hasORCID, hasROR, hasReferences, hasRelation, hasAbstract, hasAward, hasLicense, hasArchive) | ||
content, err := GetCrossrefList(number, member, _type, sample, hasORCID, hasROR, hasReferences, hasRelation, hasAbstract, hasAward, hasLicense, hasArchive) | ||
if err != nil { | ||
return data, err | ||
} | ||
|
@@ -566,7 +566,7 @@ func ReadCrossref(content Content) (types.Data, error) { | |
return data, nil | ||
} | ||
|
||
func GetCrossrefSample(number int, member string, _type string, hasORCID bool, hasROR bool, hasReferences bool, hasRelation bool, hasAbstract bool, hasAward bool, hasLicense bool, hasArchive bool) ([]Content, error) { | ||
func GetCrossrefList(number int, member string, _type string, sample bool, hasORCID bool, hasROR bool, hasReferences bool, hasRelation bool, hasAbstract bool, hasAward bool, hasLicense bool, hasArchive bool) ([]Content, error) { | ||
// the envelope for the JSON response from the Crossref API | ||
type Response struct { | ||
Status string `json:"status"` | ||
|
@@ -581,11 +581,11 @@ func GetCrossrefSample(number int, member string, _type string, hasORCID bool, h | |
if number > 100 { | ||
number = 100 | ||
} | ||
url := CrossrefApiSampleUrl(number, member, _type, hasORCID, hasROR, hasReferences, hasRelation, hasAbstract, hasAward, hasLicense, hasArchive) | ||
url := CrossrefQueryUrl(number, member, _type, sample, hasORCID, hasROR, hasReferences, hasRelation, hasAbstract, hasAward, hasLicense, hasArchive) | ||
req, err := http.NewRequest("GET", url, nil) | ||
v := "0.1" | ||
u := "[email protected]" | ||
userAgent := fmt.Sprintf("commonmeta-go/%s (https://commonmeta.org/commonmeta-go/; mailto: %s)", v, u) | ||
userAgent := fmt.Sprintf("commonmeta-go/%s (https://commonmeta.org; mailto: %s)", v, u) | ||
req.Header.Set("User-Agent", userAgent) | ||
req.Header.Set("Cache-Control", "private") | ||
if err != nil { | ||
|
@@ -613,7 +613,7 @@ func GetCrossrefSample(number int, member string, _type string, hasORCID bool, h | |
return response.Message.Items, nil | ||
} | ||
|
||
func CrossrefApiSampleUrl(number int, member string, _type string, hasORCID bool, hasROR bool, hasReferences bool, hasRelation bool, hasAbstract bool, hasAward bool, hasLicense bool, hasArchive bool) string { | ||
func CrossrefQueryUrl(number int, member string, _type string, sample bool, hasORCID bool, hasROR bool, hasReferences bool, hasRelation bool, hasAbstract bool, hasAward bool, hasLicense bool, hasArchive bool) string { | ||
types := []string{ | ||
"book-section", | ||
"monograph", | ||
|
@@ -660,7 +660,11 @@ func CrossrefApiSampleUrl(number int, member string, _type string, hasORCID bool | |
} | ||
u, _ := url.Parse("https://api.crossref.org/works") | ||
values := u.Query() | ||
values.Add("sample", strconv.Itoa(number)) | ||
if sample { | ||
values.Add("sample", strconv.Itoa(number)) | ||
} else { | ||
values.Add("rows", strconv.Itoa(number)) | ||
} | ||
var filters []string | ||
if member != "" { | ||
filters = append(filters, "member:"+member) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.