Last active
August 29, 2015 14:10
-
-
Save ErebusBat/c290deb73e4380e8fccf to your computer and use it in GitHub Desktop.
YouTubeCenter Version Checker
This file contains hidden or 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
/// | |
// YouTube Center Checker | |
// | |
// A very messy golang script to download the chrome manifest from the | |
// YouTubeCenter (https://github.com/YePpHa/YouTubeCenter) repo and display it | |
// | |
// This is more of a learning exercise than a useful utility. | |
package main | |
import ( | |
"encoding/xml" | |
"flag" | |
"fmt" | |
"io/ioutil" | |
"log" | |
"net/http" | |
) | |
const ( | |
MANIFEST_URL = "https://github.com/YePpHa/YouTubeCenter/raw/master/dist/chrome-update.xml" | |
) | |
func main() { | |
var manifest *chromeManifest | |
var err error | |
var flagTest bool | |
flag.BoolVar(&flagTest, "test", false, "Use test data, don't contact GitHub") | |
flag.Parse() | |
if flagTest { | |
testData, _ := GetManifestTestXML() | |
manifest, err = GetManifest(testData) | |
} else { | |
manifest, err = GetManifest("") | |
} | |
if err != nil { | |
log.Fatal(err) | |
} | |
fmt.Println(manifest.App.UpdateCheck.Version) | |
} | |
type chromeManifest struct { | |
App xmlApp `xml:"app"` | |
} | |
type xmlApp struct { | |
UpdateCheck updateCheck `xml:"updatecheck"` | |
} | |
type updateCheck struct { | |
Version string `xml:"version,attr"` | |
} | |
// Downloads and unmarshals the manifest | |
func GetManifest(xmlString string) (manifest *chromeManifest, err error) { | |
// var err error | |
if xmlString == "" { | |
xmlString, err = GetManifestXML() | |
} | |
if err != nil { | |
return manifest, err | |
} | |
// fmt.Println(xmlString) | |
xmlBytes := []byte(xmlString) | |
manifest = new(chromeManifest) | |
err = xml.Unmarshal(xmlBytes, &manifest) | |
return | |
} | |
func GetManifestTestXML() (contents string, err error) { | |
return `<?xml version="1.0" encoding="UTF-8"?> | |
<gupdate xmlns="http://www.google.com/update2/response" protocol="2.0"> | |
<app appid="ajijnmbjgaeekdpmpohgppkckmnagimk"> | |
<updatecheck codebase="https://raw.github.com/YePpHa/YouTubeCenter/master/dist/YouTubeCenter.crx" version="31337" /> | |
</app> | |
</gupdate>`, nil | |
} | |
func GetManifestXML() (contents string, err error) { | |
if response, err := http.Get(MANIFEST_URL); err != nil { | |
return "", err | |
} else { | |
defer response.Body.Close() | |
if xml, err := ioutil.ReadAll(response.Body); err != nil { | |
return "", err | |
} else { | |
contents = fmt.Sprintf("%s\n", string(xml)) | |
} | |
} | |
return | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment