|
1 | 1 | using System;
|
2 |
| -using GraphQL.Client; |
3 |
| -using GraphQL.Common.Request; |
4 | 2 | using System.Threading.Tasks;
|
5 | 3 | using System.Collections.Generic;
|
6 | 4 | using Newtonsoft.Json;
|
7 |
| - |
| 5 | +using System.Linq; |
8 | 6 | namespace graphql
|
9 | 7 | {
|
10 | 8 | class Program
|
11 | 9 | {
|
| 10 | + private static readonly Leetcode lc = new Leetcode(); |
12 | 11 | static void Main(string[] args)
|
13 | 12 | {
|
14 |
| - try |
| 13 | + var questionId = 0L; |
| 14 | + if(args.Length > 0) |
15 | 15 | {
|
16 |
| - GetLeetcodeAsync().Wait(); |
| 16 | + long.TryParse(args[0], out questionId); |
17 | 17 | }
|
18 |
| - catch (Exception ex) |
| 18 | + if(questionId <= 0) |
19 | 19 | {
|
20 |
| - Console.WriteLine($"There was an exception: {ex.ToString()}"); |
| 20 | + var s = ""; |
| 21 | + do |
| 22 | + { |
| 23 | + Console.WriteLine("Please enter a QuestionId:"); |
| 24 | + s = Console.ReadLine(); |
| 25 | + long.TryParse(s, out questionId); |
| 26 | + |
| 27 | + } while (questionId <= 0); |
21 | 28 | }
|
22 |
| - } |
23 |
| - static private async Task GetLeetcodeAsync() |
24 |
| - { |
25 |
| - var heroAndFriendsRequest = new GraphQLRequest |
| 29 | + try |
26 | 30 | {
|
27 |
| - Query = @" |
28 |
| - query getQuestionDetail($titleSlug: String!) { |
29 |
| - question(titleSlug: $titleSlug) { |
30 |
| - questionId |
31 |
| - questionTitle |
32 |
| - questionTitleSlug |
33 |
| - content |
34 |
| - difficulty |
35 |
| - categoryTitle |
36 |
| - codeDefinition |
37 |
| - } |
38 |
| - |
39 |
| - }", |
40 |
| - OperationName = "getQuestionDetail", |
41 |
| - Variables = new |
| 31 | + var questionStat = lc.GetAllAsync().Result; |
| 32 | + if(questionStat != null && questionStat.StatStatusPairs.Any()) |
42 | 33 | {
|
43 |
| - titleSlug = "house-robber" |
| 34 | + var statStatusPair = questionStat.StatStatusPairs.Where(x=>x.Stat.QuestionId==questionId).FirstOrDefault(); |
| 35 | + if(statStatusPair != null) |
| 36 | + { |
| 37 | + var QuestionDetail = lc.GetLeetcodeAsync(statStatusPair.Stat.QuestionTitleSlug).Result; |
| 38 | + Console.WriteLine(QuestionDetail.ToJson()); |
| 39 | + } |
44 | 40 | }
|
45 |
| - }; |
46 |
| - var graphQLClient = new GraphQLClient("https://leetcode.com/graphql"); |
47 |
| - var graphQLResponse = await graphQLClient.GetAsync(heroAndFriendsRequest); |
48 |
| - var questionDetail = graphQLResponse.GetDataFieldAs<QuestionDetail>("question"); |
49 |
| - Console.WriteLine(questionDetail.ToJson()); |
50 |
| - var codeDefinitions = JsonConvert.DeserializeObject<List<CodeDefinition>>(questionDetail.CodeDefinition); |
51 |
| - Console.WriteLine(codeDefinitions[0].ToJson()); |
| 41 | + |
| 42 | + } |
| 43 | + catch (Exception ex) |
| 44 | + { |
| 45 | + Console.WriteLine($"There was an exception: {ex.ToString()}"); |
| 46 | + } |
52 | 47 | }
|
| 48 | + |
53 | 49 | }
|
54 | 50 | }
|
0 commit comments