File tree 1 file changed +13
-14
lines changed
1 file changed +13
-14
lines changed Original file line number Diff line number Diff line change 1
1
#!/usr/bin/python3
2
+ """get a lit of suscribber on a subreddit"""
2
3
import requests
3
4
4
- def number_of_subscribers (subreddit ):
5
- """
6
- Returns the number of subscribers for a given subreddit.
7
- If the subreddit is invalid, returns 0.
8
- """
9
- url = f"https://oauth.reddit.com/r/{ subreddit } /about"
10
- headers = {"User-Agent" : "Mozilla/5.0" }
11
5
12
- try :
13
- response = requests .get (url , headers = headers , allow_redirects = False )
14
- response .raise_for_status ()
15
- data = response .json ()
16
- return data ["data" ]["subscribers" ]
17
- except (requests .exceptions .RequestException , ValueError , KeyError ):
18
- return 0
6
+ def number_of_subscribers (subreddit ):
7
+ """number of suscribers"""
8
+ if subreddit is None or type (subreddit ) != str :
9
+ return (0 )
10
+ url = "http://www.reddit.com/r/{}/about.json" .format (
11
+ subreddit )
12
+ headers = {'User-Agent' : 'API project' }
13
+ response = requests .get (url , headers = headers )
14
+ if (response .status_code != 200 ):
15
+ return (0 )
16
+ sus = response .json ().get ("data" ).get ('subscribers' , 0 )
17
+ return (sus )
You can’t perform that action at this time.
0 commit comments