-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
121 lines (100 loc) · 3.72 KB
/
main.py
File metadata and controls
121 lines (100 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import os
from xml.etree.ElementTree import tostring
from dotenv import load_dotenv
import flask
from flask import request, jsonify
from flask_cors import CORS
from scrape_tool import create_scrape_job
from postgre import savehashtagList, getHashtagList, findHashtag, fetchData, removeHashtag
from threading import Thread
load_dotenv()
SCRAPE_LIST = []
app = flask.Flask(__name__)
CORS(app)
if os.getenv("PRJ_ENV") == "development":
app.config["DEBUG"] = True
else:
app.config["DEBUG"] = False
@app.route('/registerScrape', methods=['GET'])
def register():
if 'api_key' in request.args:
api_key = request.args['api_key']
else:
return "Error: No APIKEY provided. Please specify."
if api_key != os.getenv('APIKEY'):
return "Error: Invalid APIKEY provided."
if 'hashtag' in request.args:
hashtag = request.args['hashtag']
else:
return "Error: No id field provided. Please specify an id."
if 'bool' in request.args:
bool = request.args['bool']
if os.getenv("PRJ_ENV") == "development":
if 'bool' in request.args:
pass
else:
return "Error: No id field provided. Please specify a bool."
needToScrape = open(os.getenv("need_to_scrape_path") +
"/need_to_scrape_"+hashtag, 'w')
needToScrape.write(bool)
needToScrape.close()
if bool == "True":
SCRAPE_LIST.append(hashtag)
needToScrape = open(os.getenv("need_to_scrape_path") +
"/toScrape", 'w')
needToScrape.write(str(SCRAPE_LIST))
needToScrape.close()
thread = Thread(target=create_scrape_job)
thread.start()
return "Success: Scrape job created for hashtag: " + hashtag
else:
SCRAPE_LIST.remove(hashtag)
needToScrape = open(os.getenv("need_to_scrape_path") +
"/toScrape", 'w')
needToScrape.write(str(SCRAPE_LIST))
needToScrape.close()
thread = Thread(target=create_scrape_job)
thread.start()
return "Successfully unregistered scrape job for hashtag: " + hashtag
else:
hashtagRegitered = findHashtag(hashtag)
if hashtagRegitered:
removeHashtag(hashtag)
thread = Thread(target=create_scrape_job)
thread.start()
return "Successfully unregistered scrape job for hashtag: " + hashtag
else:
savehashtagList(hashtag)
thread = Thread(target=create_scrape_job)
thread.start()
return "Successfully registered scrape job for hashtag: " + hashtag
@app.route('/getHashtag', methods=['GET'])
def hashtag():
if 'api_key' in request.args:
api_key = request.args['api_key']
else:
return "Error: No APIKEY provided. Please specify."
if api_key != os.getenv('APIKEY'):
return "Error: Invalid APIKEY provided."
if 'hashtag' in request.args:
hashtag = request.args['hashtag']
else:
return "Error: No id field provided. Please specify an id."
if os.getenv("PRJ_ENV") == "development":
results = open("res/"+hashtag, 'r')
results = results.read().splitlines()
results = results[0]
print(results)
return jsonify(results)
else:
results = fetchData(hashtag)
if results:
return jsonify(results)
else:
return "Error: Hashtag not registered or no data to fetch."
@app.route('/joebiden', methods=['GET'])
def joebiden():
return "<h1>DECUDETOIJOE</h1><p>AMOGUS.</p>"
@app.route('/', methods=['GET'])
def home():
return "<h1>MozeAppHashtagApi</h1><p>AMOGUS.</p>"