forked from pruet/DNWS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTwitterAPIPlugin.cs
More file actions
97 lines (88 loc) · 3.72 KB
/
Copy pathTwitterAPIPlugin.cs
File metadata and controls
97 lines (88 loc) · 3.72 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
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Linq;
using System;
using System.ComponentModel.DataAnnotations.Schema;
namespace DNWS
{
class TwitterAPIPlugin : TwitterPlugin{
Private List<User> GetUser(){
using(var newContext = new TweetContext()){
try{
List<User> users = newContext.Users.Where(b => true).Include(base => base.Following).ToList();
return users;
}
catch(Exception){
return null;
}
}
}
public override HTTPResponse GetResponse (HTTPRequest request){
HTTPResponse response = new HTTPResponse(200);
string username = request.getRequestByKey("user");
string password = request.getRequestByKey("password");
string following = request.getRequestByKey("following");
string timeline = request.getRequestByKey("timline");
string message = request.getRequestByKey("message");
string []url = request.Filename.split("?");
try{
if(url[0] == "users"){
if(request.Method == "GET")
{//taught by 600611001
string js = JsonConvert.SerializeObject(GetUser());
response.body = Encoding.UTF8.GetBytes(js);
}
else if(request.Method == "POST")
{
if(username != null && password != null){
Twitter.AddUser(username, password);
}
}
else if(request.Method == "DELETE")
{
if(username != null && following != null)
{
Twitter account = new Twitter(username);
account.RemoveFollowing(following);
}
}
}
else if(url[0] == "tweets")
{
if (username != null)
{
if(request.Method == "GET"){
//taught by 600611001
Twitter account = new Twitter(username);
string js = JsonConvert.SerializeObject(account.GetUserTimeline());
response.body = Encoding.UTF8.GetBytes(js);
if (timeline != null)
{
string js_1 = JsonConvert.SerializeObject(account.GetFollowingTimeline());
response.body = Encoding.UTF8.GetBytes(js_1);
}
}
else if (request.Method == "POST")
{
Twitter account = new Twitter(user);
account.PostTweet(message);
}
}
}
}
catch (Exception except)
//taught by 600611001
{
StringBuilder sb = new StringBuilder();
Console.WriteLine(except.ToString());
sb.Append(String.Format("Error [{0}], please go back to <a href=\"/twitter\">login page</a> to try again", ex.Message));
response.body = Encoding.UTF8.GetBytes(sb.ToString());
return response;
}
response.type = "application/json; charset=UTF-8";
return response;
}
}
}