-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
180 lines (154 loc) · 4.97 KB
/
utils.py
File metadata and controls
180 lines (154 loc) · 4.97 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import requests
from bs4 import BeautifulSoup
from datetime import datetime
from decouple import config
import base64
class WebScraper:
def __init__(self, url):
self.url = url
def get_soup(self):
try:
response = requests.get(self.url)
response.raise_for_status()
if response.status_code == 200:
return BeautifulSoup(response.text, "html.parser")
except requests.exceptions.RequestException:
return None
def get_blob(self):
try:
response = requests.get(self.url)
return response.content
except requests.exceptions.RequestException:
return None
@staticmethod
def clean_name(name):
return name.strip().replace("/", "-").replace(".", "-")
class DamScraper(WebScraper):
data = []
@staticmethod
def is_todays_data(filename):
try:
date_str = filename.split('–')[1].strip()
date_str = date_str.split(".")[0]
today = datetime.now().strftime("%d/%m/%Y")
if today == date_str:
return True
except Exception:
pass
return False
def get_source_urls(self, filetype="pdf"):
soup = self.get_soup()
if soup:
div = soup.find("div", class_="entry-content")
li_tags = div.find_all("li")
for li in li_tags:
name = li.text.strip()
if self.is_todays_data(name):
a_tag = li.find('a', href=True)
href = a_tag.attrs.get("href")
self.data.append({
"name": self.clean_name(name),
"url": href,
"type": filetype
})
return self.data
class RiverScraper(WebScraper):
data = []
@staticmethod
def is_todays_data(date_str):
try:
date_str = date_str.split('M')[1].strip()
today = datetime.now().strftime("%d.%m.%Y")
if today == date_str:
return True
except Exception:
pass
return False
def get_source_urls(self, filetype="jpeg"):
soup = self.get_soup()
if soup:
div = soup.find("div", class_="entry-content")
p_tags = div.find_all("p")
try:
p_tags = p_tags[1:4]
except IndexError:
return self.data
date_str = p_tags.pop(-1)
if self.is_todays_data(date_str.text):
for p in p_tags:
name = p.text.strip()
a_tag = p.find('a', href=True)
href = a_tag.attrs.get("href")
self.data.append({
"name": self.clean_name(name),
"url": href,
"type": filetype
})
return self.data
class GitHubAPI:
def __init__(self):
self.base_url = "https://api.github.com/repos"
self.repo = "Gather"
self.owner = "sreehari1997"
self.token = config("ACCESS_TOKEN")
self.branch = "main"
def get_headers(self):
headers = {
'Authorization': 'Bearer {}'.format(self.token),
'User-Agent': (
'Mozilla/5.0 (Windows NT 6.1; Win64; '
'x64; rv:47.0) Gecko/20100101 Firefox/47.0'
),
'X-GitHub-Api-Version': '2022-11-28',
'Accept': 'application/vnd.github+json'
}
return headers
def get(self, url):
try:
response = requests.get(
url,
headers=self.get_headers()
)
return response.json()
except requests.exceptions.RequestException:
return None
def put(self, url, data):
try:
response = requests.put(
url,
headers=self.get_headers(),
json=data
)
return response.json()
except requests.exceptions.RequestException:
return None
def get_last_commit_message(self):
url = "{}/{}/{}/branches/{}".format(
self.base_url,
self.owner,
self.repo,
self.branch
)
data = self.get(url)
if data:
return data["commit"]["commit"]["message"]
else:
return None
def create_file(self, content, path, commit_message):
encoded = str(base64.b64encode(content).decode("utf-8"))
data = {
"message": commit_message,
"committer": {
"name": "sreehari1997",
"email": "sreehaivijayan619@gmail.com"
},
"content": encoded
}
url = "{}/{}/{}/contents/{}".format(
self.base_url,
self.owner,
self.repo,
path
)
response = self.put(url, data)
return response