Skip to content

Commit a7c3858

Browse files
committed
update with GithubApi&random number
1 parent 2a9f413 commit a7c3858

7 files changed

Lines changed: 146 additions & 2 deletions

File tree

‎SJAlgorithmBot/README.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
1st: Read SJAlgorithm's README
2+
2nd: Generate Random Number
3+
3rd: Get URL and Rank (with Crawling)
4+
4th: Update SJAlgorithm's README

‎SJAlgorithmBot/pom.xml‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,18 @@
1212
<maven.compiler.source>17</maven.compiler.source>
1313
<maven.compiler.target>17</maven.compiler.target>
1414
</properties>
15-
15+
<dependencies>
16+
<!--기본 org.json-->
17+
<dependency>
18+
<groupId>org.json</groupId>
19+
<artifactId>json</artifactId>
20+
<version>20180813</version>
21+
</dependency>
22+
<!--구글 json 라이브러리-->
23+
<dependency>
24+
<groupId>com.googlecode.json-simple</groupId>
25+
<artifactId>json-simple</artifactId>
26+
<version>1.1</version>
27+
</dependency>
28+
</dependencies>
1629
</project>
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
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+
}
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
public class SJAlgorithmBot {
2+
23
public static void main(String[] args){
3-
System.out.println("Hello world!");
4+
String token=args[0];
5+
GitAPI gitAPI = new GitAPI(token);
6+
gitAPI.readContents("SJAlgorithm","SJAlgorithmBot","README.md");
7+
gitAPI.updateContents("SJAlgorithm","SJAlgorithmBot","README.md");
48
}
59
}
252 Bytes
Binary file not shown.
5.46 KB
Binary file not shown.
696 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)