-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpython_api_functions.py
More file actions
115 lines (90 loc) · 4.83 KB
/
python_api_functions.py
File metadata and controls
115 lines (90 loc) · 4.83 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
import http.client
import json
import csv
import base64
from flask import Flask, request
import requests
import pandas as pd
#next, define any functions you will need. This way, we define it once but can use it as many times as we would like
# this returns all valid color families
def get_color_families(authUsername:str, authPassword:str) -> pd.core.frame.DataFrame:
#define URL
url = 'https://api.ravelry.com/color_families.json'
#make the request
r1 = requests.get(url, auth=requests.auth.HTTPBasicAuth(authUsername, authPassword))
#close the connection
r1.close()
return pd.DataFrame.from_records(json.loads(r1.text)['color_families'])
# this returns all valid yarn weights
def get_yarnweights(authUsername:str, authPassword:str) -> pd.core.frame.DataFrame:
#define URL
url = 'https://api.ravelry.com/yarn_weights.json'
#make the request
r1 = requests.get(url, auth=requests.auth.HTTPBasicAuth(authUsername, authPassword))
#close the connection
r1.close()
return pd.DataFrame.from_records(json.loads(r1.text)['yarn_weights'])
# This returns the pattens from search. Parameters include query (like "hats"), page number, and page size. Note, there are no other filters available from the API for search unline in the web browser
def get_patterns(authUsername:str, authPassword:str, query = '', page = 1, page_size = 100) -> pd.core.frame.DataFrame:
#define URL
url = 'https://api.ravelry.com/patterns/search.json?query={}&page={}&page_size={}'.format(query, page, page_size)
#make the request
r1 = requests.get(url, auth=requests.auth.HTTPBasicAuth(authUsername, authPassword))
#close the connection
r1.close()
return pd.DataFrame.from_records(json.loads(r1.text)['patterns'])
# This returns all the patterns from a given person's rav_username
def get_queue(authUsername:str, authPassword:str, rav_username = 'rieslingm', query = '', page = 1, page_size = 100) -> pd.core.frame.DataFrame:
#define URL
url = 'https://api.ravelry.com/people/{}/queue/list.json?query={}&page={}&page_size={}'.format(rav_username, query, page, page_size)
#make the request
r1 = requests.get(url, auth=requests.auth.HTTPBasicAuth(authUsername, authPassword))
#close the connection
r1.close()
return pd.DataFrame.from_records(json.loads(r1.text)['queued_projects'])
# for a given pattern id, this returns all the pattern details
def get_pattern_details(authUsername:str, authPassword:str, pattern_id:int) -> pd.core.frame.DataFrame:
#define URL
url = 'https://api.ravelry.com/patterns/{}.json'.format(pattern_id)
#make the request
r1 = requests.get(url, auth=requests.auth.HTTPBasicAuth(authUsername, authPassword))
#close the connection
r1.close()
return pd.json_normalize(json.loads(r1.text)['pattern'], max_level = 1)
# for searches for yarns
def search_yarns(authUsername:str, authPassword:str, query='', page = 1, page_size = 50, sort = 'best') -> pd.core.frame.DataFrame:
#Sort order. Options are: best, rating, projects
#define URL
url = 'https://api.ravelry.com/yarns/search.json?query={}&page={}&page_size={}&sort={}'.format(query, page, page_size, sort)
#url = 'https://api.ravelry.com/yarns/search.json'
#make the request
r1 = requests.get(url, auth=requests.auth.HTTPBasicAuth(authUsername, authPassword))
#close the connection
r1.close()
return pd.json_normalize(json.loads(r1.text)['yarns'], max_level = 1)
# for a given yarn id, this returns all the yar details
def get_yarn_details(authUsername:str, authPassword:str, yarn_id:int) -> pd.core.frame.DataFrame:
#define URL
url = 'https://api.ravelry.com/yarns/{}.json'.format(yarn_id)
#make the request
r1 = requests.get(url, auth=requests.auth.HTTPBasicAuth(authUsername, authPassword))
#close the connection
r1.close()
return pd.json_normalize(json.loads(r1.text)['yarn'], max_level = 1)
def greeting_fellow_humans(name):
"""
This function greets to
the person passed in as
a parameter
"""
string_in_string = "Hello, {}. Good morning!".format(name)
return(string_in_string)
# This returns all the favorites from a given person's rav_username
def get_favorites(authUsername, authPassword, rav_username = 'rieslingm', types = 'patterns', query = '', deep_search = '', page = 1, page_size = 100) -> pd.core.frame.DataFrame:
#define URL
url = 'https://api.ravelry.com/people/{}/favorites/list.json?type={}&query={}&deep_search={}&page={}&page_size={}'.format(rav_username, types, query, deep_search, page, page_size)
#make the request
r1 = requests.get(url, auth=requests.auth.HTTPBasicAuth(authUsername, authPassword))
#close the connection
r1.close()
return pd.json_normalize(json.loads(r1.text)['favorites'], max_level = 1)