Skip to content

Commit ce59afd

Browse files
author
avats101
committed
Added transparent background
2 parents b3942b4 + d081e05 commit ce59afd

File tree

9 files changed

+224
-0
lines changed

9 files changed

+224
-0
lines changed

hackernews_scrapper/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Hacker News Scrapper
2+
This script allows the user to scrape a particular count *(based on user choice)* of tech news from **Hacker News** and store it in the form of a **csv file** in the same directory as the script. Csv file is divided into 2 columns; the topic of the news and the link to the news.
3+
4+
## Requirements
5+
There's a file provided in the repository named requirements.txt. Run the following command after changing to the local folder holding the files.
6+
```
7+
pip install -r requirements.txt
8+
```
9+
This should download any necessary library required for running the tool.
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import requests
2+
import csv
3+
import sys
4+
from bs4 import BeautifulSoup as bs
5+
6+
7+
def main():
8+
# Base url for the latest articles on the hackernews website
9+
baseurl = "https://news.ycombinator.com/newest"
10+
11+
# Number of articles requested by the user
12+
try:
13+
number_of_articles = int(input(
14+
'''Enter the number of articles you want from the hackernews website.
15+
(1-30) : '''))
16+
except ValueError:
17+
print("\nYou did not enter a number. Try again.\n")
18+
sys.exit(1)
19+
20+
if not 1 <= number_of_articles <= 30:
21+
print("\nYour input was not in the given range!\n")
22+
sys.exit(1)
23+
# Response obect to fetch the hackernews url
24+
response = requests.get(baseurl)
25+
26+
# soup object for easy scrapping
27+
soup = bs(response.content, 'html.parser')
28+
29+
# Finding all the a tags with the class storylink
30+
latest = soup.find_all('a', attrs={'class': 'storylink'})
31+
32+
# list to track the links of the articles
33+
links = []
34+
35+
# list to keep track of the names of the articles
36+
titles = []
37+
38+
# Fetching the links and names from the soup object
39+
# storing them in respective lists
40+
for article in latest:
41+
links.append(article['href'])
42+
titles.append(article.text)
43+
44+
result = []
45+
46+
for title, link in zip(titles[:number_of_articles],
47+
links[:number_of_articles]):
48+
d = {}
49+
d["News Title"] = title
50+
d["Link to the News"] = link
51+
result.append(d)
52+
53+
keys = ["News Title", "Link to the News"]
54+
55+
with open("hackernews_latest.csv", "w") as hackernews:
56+
writer = csv.DictWriter(hackernews, fieldnames=keys)
57+
writer.writeheader()
58+
writer.writerows(result)
59+
60+
return
61+
62+
63+
if __name__ == "__main__":
64+
main()
65+
print("\nYour news file has been successfully created!\n")

hackernews_scrapper/requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
requests == 2.26.0
2+
beautifulsoup4 == 4.10.0

profanity_filter/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
A script to censor bad words.
2+
3+
**Usage**<br/>
4+
`python3 filter.py [-h] (-t | -f ) [-b] [-o]`
5+
6+
```
7+
-h: show help
8+
-t: pass in a string to censor
9+
-f: pass in a file to censor
10+
-b: create a backup of original content
11+
-o: output censored data to a file
12+
```

profanity_filter/filter.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
from better_profanity import profanity
2+
import argparse
3+
4+
5+
# the main function
6+
def main(args: argparse.Namespace) -> None:
7+
# this will store the passed content
8+
content = ''
9+
10+
# if text is passed then read that
11+
if args.text is not None:
12+
content = args.text
13+
else: # if file is passed then read that file content
14+
try: # try to open the file
15+
with open(args.file) as f:
16+
for data in f.readlines():
17+
content += data
18+
except FileNotFoundError: # if file not found
19+
# \033[91m specifies the red color
20+
print('\033[91mERROR: file not found\033[0m')
21+
exit(0) # exit the program
22+
23+
# censor the content
24+
censored = profanity.censor(content)
25+
26+
# writing censored data to a file if asked
27+
if args.output is not None:
28+
with open(args.output, 'w') as f:
29+
f.write(censored)
30+
31+
# informing the user
32+
print(f'\033[92m[+] Censored data written to {args.output}\033[0m')
33+
34+
# printing the content to the console
35+
print()
36+
print('\033[1mCENSORED DATA\033[0m')
37+
print()
38+
print(censored)
39+
40+
# if -f is passed then write the censored data back to the original file
41+
42+
43+
if __name__ == '__main__':
44+
# argparse obj
45+
parser = argparse.ArgumentParser()
46+
47+
# adding a group
48+
input_grp = parser.add_mutually_exclusive_group(required=True)
49+
50+
# adding arguments to the group
51+
# argument for text
52+
input_grp.add_argument(
53+
'-t', '--text',
54+
type=str,
55+
metavar='',
56+
help='Specify the string to censor'
57+
)
58+
59+
# argument for file
60+
input_grp.add_argument(
61+
'-f', '--file',
62+
type=str,
63+
metavar='',
64+
help='Specify the file to censor'
65+
)
66+
67+
# argument for output file
68+
parser.add_argument(
69+
'-o',
70+
'--output',
71+
metavar='',
72+
help='Specify an output file'
73+
)
74+
75+
# parsing the args
76+
args = parser.parse_args()
77+
78+
# passing the args to the main
79+
main(args)

profanity_filter/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
better_profanity==0.7.0

yaml_to_json/README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
## Yaml to Json converter
2+
Yaml to Json converter converts and saves a yaml file as a json file.
3+
## Installing Dependencies
4+
run the command:
5+
```cmd
6+
pip install -r requirements.txt
7+
```
8+
## Steps to Convert Yaml to Json
9+
Run the script using
10+
```cmd
11+
python yaml_to_json.py
12+
```
13+
Enter the name of your yaml file\n
14+
15+
![image](https://user-images.githubusercontent.com/21291203/136048355-dea118d2-183b-4f87-a8ae-bc11d63a54e1.png)
16+
17+
Enter the name for the output json file\n
18+
19+
![image](https://user-images.githubusercontent.com/21291203/136048408-c2d1c8de-f4f0-493d-ba70-4d4855a5951b.png)
20+
21+
And your yaml file will be converted to a json file\n
22+
23+
![image](https://user-images.githubusercontent.com/21291203/136048462-a0553367-6a59-4bda-bafb-470bd60a5e33.png)
24+

yaml_to_json/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ruyaml==0.20.0

yaml_to_json/yaml_to_json.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from ruyaml import YAML
2+
import json
3+
4+
5+
def get_yaml_data():
6+
yaml_name = input("Enter the yaml file name: ")
7+
8+
try:
9+
with open(yaml_name, "r+") as f:
10+
yaml_data = YAML().load(f)
11+
return yaml_data
12+
except: # noqa
13+
print("Invalid input enter a valid yaml file name e.g. example.yaml")
14+
yaml_data = get_yaml_data()
15+
16+
17+
def convert_to_json(yaml_data):
18+
json_name = input("Enter the name of output json file: ")
19+
20+
try:
21+
with open(json_name, "w+") as o:
22+
o.write(json.dumps(yaml_data))
23+
except: # noqa
24+
print("Invalid input enter a valid json file name e.g. example.json")
25+
convert_to_json(yaml_data)
26+
27+
28+
yaml_data = get_yaml_data()
29+
convert_to_json(yaml_data)
30+
31+
print("Your yaml file has been converted and saved as json")

0 commit comments

Comments
 (0)