1+ import org .json .simple .JSONObject ;
2+ import org .json .simple .parser .JSONParser ;
3+ import org .json .simple .parser .ParseException ;
4+
5+ import java .io .BufferedReader ;
6+ import java .io .IOException ;
7+ import java .io .InputStreamReader ;
8+ import java .io .OutputStreamWriter ;
9+ import java .net .*;
10+ import java .nio .charset .StandardCharsets ;
11+ import java .util .Base64 ;
12+
13+ public class GitAPI {
14+ private String sha ,content ,owner ;
15+ private String token ;
16+
17+ public GitAPI (String token ){
18+ this .token =token ;
19+ }
20+
21+ public int callGetApi (String strUrl ){
22+ String result ="" ;
23+ try {
24+ URL url =new URL (strUrl );
25+ BufferedReader bf ;
26+ bf =new BufferedReader (new InputStreamReader (url .openStream (),"UTF-8" ));
27+ result =bf .readLine ();
28+
29+ JSONParser jsonParser =new JSONParser ();
30+ JSONObject jsonObject =(JSONObject ) jsonParser .parse (result );
31+
32+ setSha ((String ) jsonObject .get ("sha" ));
33+ setContent ((String )jsonObject .get ("content" ));
34+
35+ return 1 ;
36+ } catch (MalformedURLException e ) {
37+ e .printStackTrace ();
38+ } catch (IOException e ) {
39+ e .printStackTrace ();
40+ } catch (ParseException e ) {
41+ e .printStackTrace ();
42+ }
43+ return 0 ;
44+ }
45+
46+ public int callPutApi (String strUrl ,String owner ){
47+ try {
48+ URL url =new URL (strUrl );
49+ HttpURLConnection con = (HttpURLConnection ) url .openConnection ();
50+ con .setRequestMethod ("PUT" );
51+ con .setRequestProperty ("Authorization" ,"token " +token );
52+ con .setRequestProperty ("Content-type" ,"application/json" );
53+ con .setDoOutput (true );
54+ con .setDoInput (true );
55+
56+ JSONObject params =new JSONObject ();
57+ params .put ("message" ,"update" );
58+ params .put ("sha" ,getSha ());
59+ params .put ("owner" ,owner );
60+ params .put ("content" ,getContent ());
61+
62+ OutputStreamWriter wr =new OutputStreamWriter (con .getOutputStream (),"UTF-8" );
63+ wr .write (params .toString ());
64+ wr .flush ();
65+
66+ int responseCode =con .getResponseCode ();
67+ if (responseCode ==HttpURLConnection .HTTP_OK ){
68+ return 1 ;
69+ }
70+ else {
71+ return 0 ;
72+ }
73+ } catch (ProtocolException e ) {
74+ e .printStackTrace ();
75+ } catch (MalformedURLException e ) {
76+ e .printStackTrace ();
77+ } catch (IOException e ) {
78+ e .printStackTrace ();
79+ }
80+ return 0 ;
81+ }
82+
83+ public void readContents (String owner ,String repo ,String path ){
84+ if (callGetApi ("https://api.github.com/repos/" +owner +"/" +repo +"/contents/" +path )==1 ){
85+ System .out .println ("Read success!" );
86+ }
87+ else {
88+ System .out .println ("Read fail!" );
89+ }
90+ }
91+
92+ public void updateContents (String owner ,String repo ,String path ){
93+ //temp generate 1000~23853
94+ int rNum = (int ) (Math .random ()*(23853 -1000 +1 )+1000 );
95+ String InputString ="<br>* A : [BOJ_" +rNum +"](https://www.acmicpc.net/problem/" +rNum +") **Name**" ;
96+ byte [] message = InputString .getBytes (StandardCharsets .UTF_8 );
97+ String encoded =Base64 .getEncoder ().encodeToString (message );
98+ setContent (getContent ()+encoded );
99+
100+ if (callPutApi ("https://api.github.com/repos/" +owner +"/" +repo +"/contents/" +path ,owner )==1 ){
101+ System .out .println ("Update Success!" );
102+ }
103+ else {
104+ System .out .println ("Update Fail!" );
105+ }
106+ }
107+
108+ public String getSha () {
109+ return sha ;
110+ }
111+
112+ private void setSha (String sha ) {
113+ this .sha = sha ;
114+ }
115+
116+ public String getContent () {
117+ return content ;
118+ }
119+
120+ public void setContent (String content ) {
121+ this .content = content ;
122+ }
123+ }
0 commit comments