-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtweet_builder.py
53 lines (39 loc) · 1.38 KB
/
tweet_builder.py
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
import os
import requests
import json
from requests import Response
from random import randint
class TweetBuilder():
num_results: int
base_volumes_url: str
in_your_pants: str
language: str
print_type: str
fields: str
def __init__(self):
self.num_results = 30
self.base_volumes_url = 'https://www.googleapis.com/books/v1/volumes?q='
self.in_your_pants = ' in your pants'
self.language = 'en'
self.print_type = 'books'
self.fields = 'items(volumeInfo/title)'
def build_tweet(self) -> str:
return self.get_title() + self.in_your_pants
def find_subject(self) -> str:
with open('subjects.json', 'r') as read_file:
data = json.load(read_file)
nouns = data['nouns']
genres = data['genres']
noun_index = randint(0, len(nouns) - 1)
genre_index = randint(0, len(genres) - 1)
return nouns[noun_index] + genres[genre_index]
def get_title(self) -> str:
response = self.query_google_books()
parsed_response = response.json()
items = parsed_response['items']
book_index = randint(0, len(items) - 1)
return items[book_index]['volumeInfo']['title']
def query_google_books(self) -> Response:
subject = self.find_subject()
url = f"{self.base_volumes_url}{subject}&fields={self.fields}&printType={self.print_type}&maxResults={self.num_results}&langRestrict={self.language}"
return requests.get(url=url)