This repository was archived by the owner on Dec 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathStackOverflowTasks.cs
131 lines (118 loc) · 4.68 KB
/
StackOverflowTasks.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading;
using NUnit.Framework;
using ServiceStack;
using ServiceStack.Admin.Web;
using ServiceStack.Data;
using ServiceStack.OrmLite;
using ServiceStack.Text;
namespace Admin.Tasks
{
[TestFixture]
public class StackOverflowTasks
{
private IDbConnectionFactory dbFactory;
[OneTimeSetUp]
public void OneTimeSetUp()
{
dbFactory = new OrmLiteConnectionFactory(Config.ConnectionString, 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);
using (var questionsResponse = client.Get<HttpWebResponse>(
"https://api.stackexchange.com/2.2/questions?page={0}&pagesize={1}&site={2}&tagged=servicestack"
.Fmt(i, pageSize, "stackoverflow")))
{
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();
using (var answersResponse = client.Get<HttpWebResponse>(
"https://api.stackexchange.com/2.2/answers/{0}?sort=activity&site=stackoverflow"
.Fmt(acceptedAnswers.Join(";"))))
{
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 ServiceStack.Text.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();
}
}
}