Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

获取最受欢迎的解答 #68

Merged
merged 3 commits into from
Sep 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,10 @@
text="open content" description="open content" icon="AllIcons.FileTypes.UiForm">
</action>

<action id="leetcode.OpenTopVotedSolution" class="com.shuzijun.leetcode.plugin.actions.OpenTopVotedSolution"
text="open top voted solution" description="open top voted solution" icon="AllIcons.Actions.Find">
</action>


<action id="leetcode.OpenInWebAction" class="com.shuzijun.leetcode.plugin.actions.OpenInWebAction"
text="open in web" description="open in web" icon="AllIcons.Actions.MoveTo2">
Expand Down Expand Up @@ -353,6 +357,7 @@
<group id="leetcode.NavigatorActionsMenu">
<reference id="leetcode.OpenAction"/>
<reference id="leetcode.OpenContentAction"/>
<reference id="leetcode.OpenTopVotedSolution"/>
<reference id="leetcode.OpenInWebAction"/>
<separator/>
<reference id="leetcode.SubmitAction"/>
Expand Down
34 changes: 34 additions & 0 deletions src/com/shuzijun/leetcode/plugin/actions/OpenTopVotedSolution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.shuzijun.leetcode.plugin.actions;

import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.project.Project;
import com.shuzijun.leetcode.plugin.manager.CodeManager;
import com.shuzijun.leetcode.plugin.model.Config;
import com.shuzijun.leetcode.plugin.model.Question;
import com.shuzijun.leetcode.plugin.utils.*;

import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;

/**
* @author rufus
*/
public class OpenTopVotedSolution extends AbstractAction {

@Override
public void actionPerformed(AnActionEvent anActionEvent, Config config) {
JTree tree = anActionEvent.getData(DataKeys.LEETCODE_PROJECTS_TREE);
DefaultMutableTreeNode note = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
Question question = (Question) note.getUserObject();
Project project = anActionEvent.getProject();


ApplicationManager.getApplication().invokeLater(new Runnable() {
@Override
public void run() {
CodeManager.openTopVotedSolution(question, project);
}
});
}
}
93 changes: 89 additions & 4 deletions src/com/shuzijun/leetcode/plugin/manager/CodeManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VirtualFile;
import com.shuzijun.leetcode.plugin.model.CodeTypeEnum;
import com.shuzijun.leetcode.plugin.model.Config;
import com.shuzijun.leetcode.plugin.model.Constant;
import com.shuzijun.leetcode.plugin.model.Question;
import com.shuzijun.leetcode.plugin.model.*;
import com.shuzijun.leetcode.plugin.setting.PersistentConfig;
import com.shuzijun.leetcode.plugin.utils.*;
import org.apache.commons.lang.StringUtils;
Expand Down Expand Up @@ -101,6 +98,94 @@ public static void openContent(Question question, Project project) {
}
}

public static void openTopVotedSolution(Question question, Project project){
Config config = PersistentConfig.getInstance().getInitConfig();

String filePath = PersistentConfig.getInstance().getTempFilePath() + "solution_" + VelocityUtils.convert(config.getCustomFileName(), question) +".md";
File file = new File(filePath);
if (file.exists()) {
VirtualFile vf = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(file);
OpenFileDescriptor descriptor = new OpenFileDescriptor(project, vf);
FileEditorManager.getInstance(project).openTextEditor(descriptor, false);
} else {
SolutionArticle solution = getTopVotedSolution(question);
if (solution != null){
FileUtils.saveFile(file, solution.toString());

VirtualFile vf = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(file);
OpenFileDescriptor descriptor = new OpenFileDescriptor(project, vf);
FileEditorManager.getInstance(project).openTextEditor(descriptor, false);
}
}
}

private static SolutionArticle getTopVotedSolution(Question question){
HttpPost post = null;
String slug = null;
try {
post = new HttpPost(URLUtils.getLeetcodeGraphql());
StringEntity getTileEntity = new StringEntity("{\n" +
"\t\"operationName\": \"questionSolutionArticles\",\n" +
"\t\"variables\": {\n" +
"\t\t\"questionSlug\": \""+ question.getTitleSlug() +"\",\n" +
"\t\t\"first\": 10,\n" +
"\t\t\"skip\": 0,\n" +
"\t\t\"orderBy\": \"MOST_UPVOTE\"\n" +
"\t},\n" +
"\t\"query\": \"query questionSolutionArticles($questionSlug: String!, $skip: Int, $first: Int, $orderBy: SolutionArticleOrderBy, $userInput: String) {\\n questionSolutionArticles(questionSlug: $questionSlug, skip: $skip, first: $first, orderBy: $orderBy, userInput: $userInput) {\\n totalNum\\n edges {\\n node {\\n ...article\\n __typename\\n }\\n __typename\\n }\\n __typename\\n }\\n}\\n\\nfragment article on SolutionArticleNode {\\n slug\\n status\\n __typename\\n}\\n\"\n" +
"}");
post.setEntity(getTileEntity);
post.setHeader("Accept", "application/json");
post.setHeader("Content-type", "application/json");
CloseableHttpResponse response = HttpClientUtils.executePost(post);
if (response != null && response.getStatusLine().getStatusCode() == 200) {

String body = EntityUtils.toString(response.getEntity(), "UTF-8");

JSONObject jsonObject = JSONObject.parseObject(body).getJSONObject("data").getJSONObject("questionSolutionArticles").getJSONArray("edges").getJSONObject(0).getJSONObject("node");
slug = jsonObject.getString("slug");
} else {
MessageUtils.showWarnMsg("error", PropertiesUtils.getInfo("response.code"));
}

StringEntity getDetailEntity = new StringEntity("{\n" +
"\t\"operationName\": \"solutionDetailArticle\",\n" +
"\t\"variables\": {\n" +
"\t\t\"slug\": \""+ slug +"\"\n" +
"\t},\n" +
"\t\"query\": \"query solutionDetailArticle($slug: String!) {\\n solutionArticle(slug: $slug) {\\n ...article\\n content\\n __typename\\n }\\n}\\n\\nfragment article on SolutionArticleNode {\\n title\\n slug\\n reactedType\\n status\\n identifier\\n canEdit\\n reactions {\\n count\\n reactionType\\n __typename\\n }\\n tags {\\n name\\n nameTranslated\\n slug\\n __typename\\n }\\n createdAt\\n thumbnail\\n author {\\n username\\n profile {\\n userAvatar\\n userSlug\\n realName\\n __typename\\n }\\n __typename\\n }\\n summary\\n topic {\\n id\\n commentCount\\n viewCount\\n __typename\\n }\\n byLeetcode\\n isMyFavorite\\n isMostPopular\\n isEditorsPick\\n upvoteCount\\n upvoted\\n hitCount\\n __typename\\n}\\n\"\n" +
"}");
post.setEntity(getDetailEntity);
post.setHeader("Accept", "application/json");
post.setHeader("Content-type", "application/json");
response = HttpClientUtils.executePost(post);
if (response != null && response.getStatusLine().getStatusCode() == 200) {

String body = EntityUtils.toString(response.getEntity(), "UTF-8");

JSONObject jsonObject = JSONObject.parseObject(body).getJSONObject("data").getJSONObject("solutionArticle");
String summary = jsonObject.getString("summary");
String content = jsonObject.getString("content");
String author = jsonObject.getJSONObject("author").getString("username");
int upvoteCount = jsonObject.getInteger("upvoteCount");

return new SolutionArticle(author, summary, content, upvoteCount);
} else {
MessageUtils.showWarnMsg("error", PropertiesUtils.getInfo("response.code"));
}

} catch (Exception e) {
LogUtils.LOG.error("获取答案失败", e);
MessageUtils.showWarnMsg("error", PropertiesUtils.getInfo("response.code"));
} finally {
if (post != null) {
post.abort();
}
}

return null;
}

private static boolean getQuestion(Question question, CodeTypeEnum codeTypeEnum) {
HttpPost post = null;
try {
Expand Down
63 changes: 63 additions & 0 deletions src/com/shuzijun/leetcode/plugin/model/SolutionArticle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.shuzijun.leetcode.plugin.model;

public class SolutionArticle {
private String author;
private String summary;
private String content;
private int upvoteCount;//点赞数

public SolutionArticle() {
}

public SolutionArticle(String author, String summary, String content, int upvoteCount) {
this.author = author;
this.summary = summary;
this.content = content;
this.upvoteCount = upvoteCount;
}

public String getAuthor() {
return author;
}

public void setAuthor(String author) {
this.author = author;
}

public String getSummary() {
return summary;
}

public void setSummary(String summary) {
this.summary = summary;
}

public String getContent() {
return content;
}

public void setContent(String content) {
this.content = content;
}

public int getUpvoteCount() {
return upvoteCount;
}

public void setUpvoteCount(int upvoteCount) {
this.upvoteCount = upvoteCount;
}

@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("####Author ").append(author).append(" \n");
builder.append("####Votes ").append(upvoteCount).append(" \n");
builder.append("____________________________________________________________________________\n\n");

// builder.append(summary).append("\n");
builder.append(content).append("\n ");

return builder.toString();
}
}