-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathStackOverflowTasks.cs
More file actions
128 lines (112 loc) · 4.43 KB
/
StackOverflowTasks.cs
File metadata and controls
128 lines (112 loc) · 4.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading;
using NUnit.Framework;
using ServiceStack;
using ServiceStack.Data;
using ServiceStack.OrmLite;
using ServiceStack.Text;
using StackApis.ServiceModel;
using StackApis.ServiceModel.Types;
namespace StackApis.Tests
{
[TestFixture]
public class StackOverflowTasks
{
private IDbConnectionFactory dbFactory;
[OneTimeSetUp]
public void OneTimeSetUp()
{
dbFactory = new OrmLiteConnectionFactory(
"~/../../../StackApis/App_Data/db.sqlite".MapServerPath(), SqliteDialect.Provider);
}
[Test]
public void Import_from_StackOverflow()
{
var client = new JsonServiceClient();
int numberOfPages = 10;
int pageSize = 100;
var dbQuestions = new List<Question>();
var dbAnswers = new List<Answer>();
try
{
for (int i = 1; i < numberOfPages + 1; i++)
{
//Throttle queries
Thread.Sleep(100);
var url = $"https://api.stackexchange.com/2.2/questions?page={i}&pagesize={pageSize}&site=stackoverflow&tagged=servicestack";
var questionsResponse = client.Get<HttpWebResponse>(url);
QuestionsResponse qResponse;
using (new ConfigScope())
{
var json = questionsResponse.ReadToEnd();
qResponse = json.FromJson<QuestionsResponse>();
dbQuestions.AddRange(qResponse.Items.Select(q => q.ConvertTo<Question>()));
}
var acceptedAnswers =
qResponse.Items
.Where(x => x.AcceptedAnswerId != null)
.Select(x => x.AcceptedAnswerId).ToList();
url = $"https://api.stackexchange.com/2.2/answers/{acceptedAnswers.Join(";")}?sort=activity&site=stackoverflow";
var answersResponse = client.Get<HttpWebResponse>(url);
using (new ConfigScope())
{
var json = answersResponse.ReadToEnd();
var aResponse = JsonSerializer.DeserializeFromString<AnswersResponse>(json);
dbAnswers.AddRange(aResponse.Items.Select(a => a.ConvertTo<Answer>()));
}
}
}
catch (Exception ex)
{
//ignore
ex.Message.Print();
}
//Filter duplicates
dbQuestions = dbQuestions.GroupBy(q => q.QuestionId).Select(q => q.First()).ToList();
dbAnswers = dbAnswers.GroupBy(a => a.AnswerId).Select(a => a.First()).ToList();
var questionTags = dbQuestions.SelectMany(q =>
q.Tags.Select(t => new QuestionTag { QuestionId = q.QuestionId, Tag = t }));
using (var db = dbFactory.OpenDbConnection())
{
db.DropAndCreateTable<Question>();
db.DropAndCreateTable<Answer>();
db.DropAndCreateTable<QuestionTag>();
db.InsertAll(dbQuestions);
db.InsertAll(dbAnswers);
db.InsertAll(questionTags);
}
}
[Test]
public void Test_Import()
{
using var db = dbFactory.OpenDbConnection();
var numberOfQuestions = db.Count<Question>();
var numberOfAnswers = db.Count<Answer>();
Assert.That(numberOfQuestions > 0);
Assert.That(numberOfAnswers > 0);
}
}
public class ConfigScope : IDisposable
{
private readonly WriteComplexTypeDelegate holdQsStrategy;
private readonly JsConfigScope scope;
public ConfigScope()
{
scope = JsConfig.With(new Config {
DateHandler = DateHandler.UnixTime,
PropertyConvention= PropertyConvention.Lenient,
TextCase = TextCase.SnakeCase,
});
holdQsStrategy = QueryStringSerializer.ComplexTypeStrategy;
QueryStringSerializer.ComplexTypeStrategy = QueryStringStrategy.FormUrlEncoded;
}
public void Dispose()
{
QueryStringSerializer.ComplexTypeStrategy = holdQsStrategy;
scope.Dispose();
}
}
}