-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclipsource_server.py
More file actions
102 lines (89 loc) · 3.55 KB
/
Copy pathclipsource_server.py
File metadata and controls
102 lines (89 loc) · 3.55 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
from flask import Flask
import win32clipboard as clp
import json
# Setup server
app = Flask(__name__)
@app.route("/citations.py")
def get_citations():
"""
Returns the contents of the specified CITATIONS format when opening following URL: https://localhost:5000/source.py
:return: Current clipboard citations in JSON format
"""
try:
# Try to get the clipboard's text. If no text is available, set "No Text available" as text
clp.OpenClipboard(None)
try:
text = unicode(clp.GetClipboardData(clp.CF_TEXT), errors='replace')
except:
text = "No Text available"
# Register citation format, so we can check if this format is available
citation_format = clp.RegisterClipboardFormat("CITATIONS")
# Checks if citations are available, then returns them as JSON
if clp.IsClipboardFormatAvailable(citation_format):
data = clp.GetClipboardData(citation_format)
clp.CloseClipboard()
return data
else:
# If format is unavailable, set placeholders as data and return JSON object
clp.CloseClipboard()
data = {}
data['APA'] = "no citations in clipboard"
data['AMA'] = "no citations in clipboard"
data['content'] = text
json_data = json.dumps(data)
return json_data
except:
# If anything above fails, close the clipboard und return placeholders as JSON object
try:
clp.CloseClipboard()
except:
print "clipboard already closed"
data = {}
data['APA'] = "no citations in clipboard"
data['AMA'] = "no citations in clipboard"
data['content'] = "no text in clipboard"
json_data = json.dumps(data)
return json_data
# Returns current clipboard source as JSON (https://localhost:5000/source.py)
@app.route("/source.py")
def get_source():
"""
Returns the contents of the specified SOURCE format when opening following URL: https://localhost:5000/source.py
:return: Current clipboard source in JSON format
"""
try:
# Try to get the clipboard's text. If no text is available, set "No Text available" as text
clp.OpenClipboard(None)
try:
text = unicode(clp.GetClipboardData(clp.CF_TEXT), errors='replace')
except:
text = "No Text available"
# Register source format, so we can check if this format is available
source_format = clp.RegisterClipboardFormat("SOURCE")
if clp.IsClipboardFormatAvailable(source_format):
# Checks if sources are available, then returns them as JSON
data = clp.GetClipboardData(source_format)
clp.CloseClipboard()
return data
else:
# If format is unavailable, set placeholders as data and return JSON object
clp.CloseClipboard()
data = {}
data['source'] = "no source in clipboard"
data['content'] = text
json_data = json.dumps(data)
return json_data
except:
# If anything above fails, close the clipboard und return placeholders as JSON object
try:
clp.CloseClipboard()
except:
print "clipboard already closed"
data = {}
data['source'] = "no source in clipboard"
data['content'] = "no text in clipboard"
json_data = json.dumps(data)
return json_data
if __name__ == "__main__":
# Run flask server without certificate
app.run(ssl_context='adhoc')