Last active
August 29, 2015 14:05
-
-
Save maxanier/6f0ad6d9e5e4429b8678 to your computer and use it in GitHub Desktop.
Autouploads minecraft mods to ge.tt and registeres them on a website. Requires: http://jgett.atcetera.it/
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
package de.maxgb.util.versionupload; | |
import it.atcetera.jgett.AuthenticationException; | |
import it.atcetera.jgett.FileInfo; | |
import it.atcetera.jgett.JGettClient; | |
import it.atcetera.jgett.ShareInfo; | |
import it.atcetera.jgett.UserInfo; | |
import java.io.File; | |
import java.io.IOException; | |
import java.net.HttpURLConnection; | |
import java.net.MalformedURLException; | |
import java.net.ProtocolException; | |
import java.net.URL; | |
public class VersionUpload { | |
//Constants | |
private static String apiKey=null; | |
private static String email=null; | |
private static String password=null; | |
private static String registerPass=null; | |
private static String modname; | |
private static String addlink; | |
/** | |
*Required file names: Modname-MCVersion-Version.jar and Modname-MCVersion-Version-dev.jar. No more "-". | |
* @param args <folder with files> <modname> <link to register, "no" if dont register> (<ge.tt email><ge.tt pass><password for register site><ge.tt api Key> These can either be speciefid via arguments or via env variables) | |
*/ | |
public static void main(String[] args) { | |
System.out.println("Starting uploadscript"); | |
if(args.length<3||(args.length!=3&&args.length!=7)){ | |
System.err.println("Missing Arguments"); | |
return; | |
} | |
modname=args[1]; | |
addlink=args[2]; | |
if(args.length==3){ | |
email=System.getenv("email"); | |
password=System.getenv("password"); | |
registerPass=System.getenv("registerpass"); | |
apiKey=System.getenv("apikey"); | |
} | |
else{ | |
email=args[3]; | |
password=args[4]; | |
registerPass=args[5]; | |
apiKey=args[6]; | |
} | |
if(email==null||password==null||registerPass==null||apiKey ==null){ | |
System.err.println("Missing email or password or registerpass or apikey in environment"); | |
} | |
File folder= new File(args[0]); | |
File[] files=folder.listFiles(); | |
for(File f:files){ | |
if(!f.getName().contains("dev")){ | |
for(File f2:files){ | |
if(f2.getName().startsWith(f.getName().replace(".jar", ""))&&f2.getName().contains("dev")){ | |
System.out.println("Found files: File: "+f.getName()+" Devfile: "+f2.getName()+"\n"); | |
upload(f,f2); | |
} | |
} | |
} | |
} | |
} | |
private static void upload(File file,File devfile){ | |
if(!file.exists()){ | |
System.err.println("File does not exist"); | |
return; | |
} | |
if(!devfile.exists()){ | |
System.err.println("DevFile does not exist"); | |
return; | |
} | |
//Process filename to gather informations | |
String parts[] =file.getName().split("-"); | |
if(parts.length!=3&&parts.length!=4){ | |
System.err.println("Cannot process filename"); | |
return; | |
} | |
String mcv=parts[1]; | |
String version=parts[2].replace(".jar", "").replace(".zip", ""); | |
String[] vparts=version.split("\\."); | |
String mainversion=vparts[0]+"."+vparts[1]; | |
System.out.println("Uploading version "+version+"("+mainversion+") for MC"+mcv) | |
; | |
JGettClient client = new JGettClient(); | |
try { | |
client.authenticate(apiKey, email, password); | |
} catch (IOException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
return; | |
} catch (AuthenticationException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
return; | |
} | |
FileInfo info=null; | |
FileInfo devinfo=null; | |
try { | |
System.out.println("Uploading"); | |
ShareInfo share=null; | |
for(ShareInfo s:client.getShares()){ | |
if(s!=null&&s.getTitle()!=null&&s.getTitle().equals(modname+"-"+mainversion)){ | |
share = s; | |
break; | |
} | |
} | |
if(share==null){ | |
share=client.createShare(modname+"-"+mainversion); | |
} | |
info =client.uploadFile(file, share, null); | |
devinfo = client.uploadFile(devfile, share, null); | |
} catch (IllegalArgumentException e) { | |
System.err.println("Filename is already used"); | |
e.printStackTrace(); | |
return; | |
} catch (IOException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
return; | |
} | |
System.out.println("Uploaded"); | |
int recommend=0; | |
if(System.getenv("MODVERSION").equals("1")){ | |
recommend=1; | |
System.out.println("Version recommend"); | |
} | |
registerUpload(mcv,version,recommend,info.getUrl().toString(),devinfo.getUrl().toString()); | |
try { | |
UserInfo me = client.getUserInformation(); | |
System.out.println("Available space: "+(me.getStorageInfo().getLimitSpace()-me.getStorageInfo().getUsedSpace())); | |
} catch (IOException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
} | |
private static void registerUpload(String mcv,String version,int recommend,String link,String devlink){ | |
if(addlink=="no"){ | |
System.out.println("Skipping registering because of argument"); | |
return; | |
} | |
String url_string =addlink+"?"+"mcversion="+mcv+"&version="+version+"&recommend="+recommend+"&link="+link+"&pass="+registerPass+"&devlink="+devlink; | |
try { | |
URL url=new URL(url_string); | |
HttpURLConnection con = (HttpURLConnection) url.openConnection(); | |
// optional default is GET | |
con.setRequestMethod("GET"); | |
int responseCode = con.getResponseCode(); | |
System.out.println("Response Code : " + responseCode); | |
} catch (MalformedURLException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} catch (ProtocolException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} catch (IOException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment