Repository navigation
Expand file tree
/
Copy pathStackOverflowTasks.cs
More file actions
133 lines (117 loc) · 4.61 KB
/
Copy pathStackOverflowTasks.cs
File metadata and controls
133 lines (117 loc) · 4.61 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
129
130
131
132
133
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
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()
{
var dbPath = "~/App_Data/db.sqlite".MapProjectPath();
if (File.Exists(dbPath))
{
File.Delete(dbPath);
}
dbFactory = new OrmLiteConnectionFactory(dbPath, SqliteDialect.Provider);
}
[Test]
public void Import_from_StackOverflow()
{
int numberOfPages = 100;
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);
//StackOverflow API always returns gzipped response and .NET Core HttpWebRequest doesn't support auto uncompressing yet so need to decompress manually
var bytes = $"https://api.stackexchange.com/2.2/questions?page={i}&pagesize={pageSize}&site=stackoverflow&tagged=servicestack".GetBytesFromUrl();
var json = bytes.GUnzip();
QuestionsResponse qResponse;
using (new ConfigScope())
{
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();
var answersList = acceptedAnswers.Join(";");
bytes = $"https://api.stackexchange.com/2.2/answers/{answersList}?sort=activity&site=stackoverflow".GetBytesFromUrl();
json = bytes.GUnzip();
using (new ConfigScope())
{
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(
dateHandler: DateHandler.UnixTime,
propertyConvention: PropertyConvention.Lenient,
emitLowercaseUnderscoreNames: true,
emitCamelCaseNames: false);
holdQsStrategy = QueryStringSerializer.ComplexTypeStrategy;
QueryStringSerializer.ComplexTypeStrategy = QueryStringStrategy.FormUrlEncoded;
}
public void Dispose()
{
QueryStringSerializer.ComplexTypeStrategy = holdQsStrategy;
scope.Dispose();
}
}
}