CRAW (C Reddit API Wrapper) is a lightweight Reddit API wrapper written in C.
It is designed to help developers fetch Reddit data and build bots using pure C, while keeping the library fast, simple, and easy to integrate into any project.
- Where to get client id and secret key
- How to build
- Data structures
- Functions
- List of enums
- Basic example
If you want to use this Wrapper, you need a reddit client id and secret key which you can get by going here and logging in with your credentials
NOTE:- Non OAuth endpoints have been implemented aswell, so the library can also be used without an API key
The project has a dependency of libcurl and cJSON, it can be installed in linux by
Ubuntu:
# means to run the command as root
# apt-get install curl
# apt-get install libcurl4-openssl-dev
# apt-get install libcjson-dev
Clone this repo using
git clone https://github.com/SomeTroller77/CRAW
then go into the CRAW Directory and do (# means to run the command as root)
$ mkdir build && cd build
$ cmake ..
$ make
# make install
And you have installed CRAW, lets see hoe you can use it
CRAW uses struct to store the data. heres the table of the structs in C
| Keyword | Description |
|---|---|
CRAW |
It is used to store the developers data that developers provide which will be used to send requests |
CRAW_Account |
It stores the account data which is sent by the reddit API |
CRAW_Subreddit |
It stores the data of subreddit which is sent by the reddit API |
CRAW_Messages |
It stores the data of a message/comment which is sent by the reddit API |
CRAW_Links |
It is a list of many data structures together, basically like an array |
here are some stuff which is stored and usable in CRAW_Account struct pointer
| Data Type | Field | Description |
|---|---|---|
char * |
id |
its stores the reddit id of the account |
char * |
name |
it stores the username of the account |
long |
created_utc |
it stores the time of creation of account in EPOCH time |
int |
total karma |
it stores the total karma of the account |
| Data Type | Field | Description |
|---|---|---|
char * |
description |
sidebar text |
char * |
display_name |
human name of the subreddit |
char * |
header_img |
full URL to the header image |
char * |
header_title |
description of header |
bool |
over18 |
whether the subreddit is marked as NSFW |
char * |
public_description |
Description shown in subreddit search results |
bool |
public_traffic |
whether the subreddit's traffic page is publically accesssible |
long |
subscribers |
the number of redditors subscribed to this subreddit |
long |
created_utc |
UNIX Timestamp at which the subreddit was created |
CRAW_Subreddit_type |
subreddit_type | the type of the subreddit (public, private, restricted) |
char * |
title |
title of the main page |
char * |
url |
the relative URL of the subreddit |
bool |
is_user_banned |
whether the logged-in user is banned from the subreddit |
bool |
is_user_contributor |
whether the logged-in user is an approved submitter of the reddit |
bool |
is_user_moderator |
whether the logged-in user is a moderator of the subreddit |
bool |
is_user_subscriber |
whether the logged-in user is subscribed to the subreddit |
| Data Type | Field | Description |
|---|---|---|
char * |
author |
username of the author |
char * |
body |
the message itself |
char * |
context |
if the message is a comment, then the permalink to the comment with ?context=3 appended to the end, otherwise an empty string |
char * |
first_message |
either null or the first message's ID represented as base 10 |
char * |
first_message_name |
either null or the first message's fullname |
bool |
likes |
how the logged-in user has voted on the message - True = upvoted, False = downvoted, null = no vote |
char * |
link_title |
if the message is actually a comment, contains the title of the thread it was posted in |
char * |
name |
ex: "t4_8xwlg" |
char * |
parent_id |
null if no parent is attached |
char * |
replies |
Again, an empty string if there are no replies. |
char * |
subject |
subject of message |
char * |
subreddit |
null if not a comment. |
| Data Type | Field | Description |
|---|---|---|
char * |
author |
the name of the author |
char * |
author_fullname |
the id of the author |
char * |
domain |
the domain from which the link is originated from |
bool |
hidden |
whether the post is hidden from the user or not |
bool |
is_self |
whether the post is made by the logged-in user |
CRAW_Vote |
vote_status |
whether the logged-in user has voted on the link |
bool |
is_locked |
whether the post is locked |
int |
num_comments |
number of comments the link has |
bool |
over_18 |
whether the post is NSFW or not |
char * |
permalink |
the permalink of the post |
bool |
is_saved |
whether the post is saved by the logged-in user |
int |
score |
the difference between the upvotes and the downvotes of the link |
char * |
selftext |
the content of the post |
char * |
subreddit |
the name of the subreddit without /r/ |
char * |
subreddit_id |
the id of the subreddit |
char * |
title |
the title of the post |
char * |
url |
the url of the post |
long |
edited |
the UNIX Timestamp when the post was edited, null when not edited |
bool |
is_stickied |
whether the post has been stickied in a subreddit |
| Data Type | Field | Description |
|---|---|---|
char * |
after |
gives the id of the next listing |
int |
array_size |
stores the size/length of the array |
CRAW_children * |
children |
array of CRAW Data structures |
| Data Type | Field | Description |
|---|---|---|
CRAW_Datatype |
type |
stores the datatype of the array element |
void * |
data |
the pointer to the CRAW Data structure |
Functions implemented:-
| Return type | Function declaration | Description |
|---|---|---|
CRAW * |
CRAW *CRAW_Init(const char *client_id, const char *secret_key, const char *username, const char *password, const char *user_agent, bool is_oauth) |
Returns a pointer to the struct CRAW, returns NULL if the servers are down or any arguments are wrong, putting false as the argument for is_oauth will result in other arguments getting ignored as it initializes a non OAuth handle |
void |
CRAW_free(CRAW *handle) |
frees the pointer initialised using CRAW_Init() |
| Return type | Function declaration | Description |
|---|---|---|
CRAW_Account * |
CRAW_Account_Init() |
Returns a pointer to a CRAW_Account struct which can be used to store an account data, returns NULL if your out of memory |
CRAWcode |
CRAW_Account_me(CRAW *handle, CRAW_Account * accHandle) |
Returns a CRAW code and writes the data of logged in account to passed accHandle |
CRAWcode |
CRAW_Account_getAbout(CRAW *handle, char *username, CRAW_Account *accHandle) |
Returns a CRAWcode and writes the provided data from api to the passed accHandle |
CRAWcode |
CRAW_getHotPosts(CRAW *handle, CRAW_Listing *list) |
get the hot posts from the logged-in user's feed (default 25, option to customize will be implemented soon) |
CRAWcode |
CRAW_getNewPosts(CRAW *handle, CRAW_Listing *list) |
get the new posts from the logged-in user's feed (default 25, option to customize will be implemented soon) |
CRAWcode |
CRAW_getRisingPosts(CRAW *handle, CRAW_Listing *list) |
get the rising posts from the logged-in user's feed (default 25, option to customize will be implemented soon) |
void |
CRAW_Account_Free(CRAW_Account *accHandle) |
frees the passed pointer (Must be run on all the CRAW_Account variables initialised using CRAW_Account_Init() ) |
| Return type | Function declaration | Description |
|---|---|---|
CRAW_Subreddit * |
CRAW_Subreddit_Init() |
returns the pointer of a CRAW_Subreddit struct |
CRAWcode |
CRAW_Subreddit_GetInfo(CRAW *handle, CRAW_Subreddit *subreddit, char *subreddit_name) |
inputs the information of a subreddit into the pointer subreddit |
CRAWcode |
CRAW_Subreddit_getHotPosts(CRAW *handle, CRAW_Listing *list, char *subreddit_name) |
get the hot posts in a subreddit (default 25, option to customize will be implemented soon) |
CRAWcode |
CRAW_Subreddit_getNewPosts(CRAW *handle, CRAW_Listing *list, char *subreddit_name) |
get the new posts in a subreddit (default 25, option to customize will be implemented soon) |
CRAWcode |
CRAW_Subreddit_getRisingPosts(CRAW *handle, CRAW_Listing *list, char *subreddit_name) |
get the rising posts in a subreddit (default 25, option to customize will be implemented soon) |
void |
CRAW_Subreddit_Free(CRAW_Subreddit *ptr) |
frees the pointer ptr safely |
| Return type | Function declaration | Description |
|---|---|---|
CRAW_Message * |
CRAW_Message_Init() |
Initializes the CRAW_Message struct |
CRAWcode |
CRAW_Message_getInbox(CRAW *handle, CRAW_Listing *ptr) |
Gets the logged-in users inbox |
void |
CRAW_Message_free(CRAW_Message *handle) |
Frees the CRAW_Message pointer |
| Return type | Function declaration | Description |
|---|---|---|
CRAW_Listing * |
CRAW_Listing_Init() |
initializes the CRAW_Listing struct |
void |
CRAW_Listing_Free(CRAW_Listing *ptr) |
Frees the CRAW_Listing struct |
Enums make it easy for developers to be able to use conditions without needing to remember the int values of different entities
The datatypes which are used by CRAW to manage listing as it utilizes void pointers which are needed to be casted to be able to compile it
Heres a list of datatypes which CRAW uses
| CRAW_Datatype | Description |
|---|---|
CRAW_COMMENT |
A reddit comment data |
CRAW_ACCOUNT |
A reddit account data |
CRAW_LINK |
A reddit link data (eg subreddit posts etc) |
CRAW_MESSAGE |
A reddit message data |
CRAW_SUBREDDIT |
A subreddit data |
CRAW_AWARD |
A reddit award data |
CRAW_UNKNOWN_DATATYPE |
Datatype which has not been implemented into CRAW (yet) |
Determines the visibility status of a subreddit
| CRAW_Subreddit_type | Description |
|---|---|
CRAW_SUBREDDIT_PUBLIC |
A public subreddit |
CRAW_SUBREDDIT_PRIVATE |
A private subreddit |
CRAW_SUBREDDIT_RESTRICTED |
A restricted subreddit |
Tells you whether the logged-in user has upvoted, downvoted, or not voted to the post
| CRAW_Vote | Description |
|---|---|
CRAW_UPVOTED |
User has upvoted the post |
CRAW_DOWNVOTED |
User has downvoted the post |
CRAW_NO_VOTE |
User has neither upvoted nor downvoted the post |
Heres a list of CRAWcode which may be return by the functions with return type CRAWcode
| CRAWcode | Description |
|---|---|
CRAW_OK |
If you get this CRAWcode, then your all good to go |
CRAW_PARSE_ERROR |
If you get this CRAWcode, then there a parsing error, recheck your parameters and try again |
CRAW_GRAB_ERROR |
If you get this error, then check your internet as the wrapper couldnt grab the data from API |
CRAW_NOT_FOUND |
If you get this error then check your parameter as the API couldnt find the data your looking for |
CRAW_UNAUTHORISED |
If you get this error then check your username and password as the API was not able to authorize you ( there might be a chance of you being banned by api if you receive this error) |
CRAW_FORBIDDEN |
If you get this error then check your username and password and then try again |
CRAW_TOO_MANY_REQUESTS |
If you get this error, then take a chill pill and try it after 1-5 mins as you have been sending too many requests |
CRAW_UNKNOWN_CODE |
If you get this error, then create a issue and send us your code and we will fix it as theres no CRAWcode registered till now for this |
If you still get any on the CRAWcode after following the instructions, then you are welcome to create a issue and we will help you!
Heres a basic example on how you could use it right now
navigate to examples/ to see examples
For the developers looking to contribute towards the project, i have a TO DO list which you could follow and would help me a lot :D
- Implement CRAW_Revoke_AccessToken()
- Implement CRAW_Refresh_AccessToken()
- Implement ratelimit handler
- Implement postData() to be utilized in endpoints like creating a post or composing a message
- Implement Reddit messages and start implementing its endpoints
- Implement CRAW_createPost()
Note:- Ignore the CI pipeline for now as it seems that reddit has blocked github's ip address which is leading to the return of HTML instead of JSON
This project is not well made as i am still developing it, it might take some time but i will try to make it a good project, issues are welcome
