-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
110 lines (85 loc) · 3.9 KB
/
main.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
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
from igdb.wrapper import IGDBWrapper
from decouple import config
from platformid import platform_ids
import pandas as pd
import json
import ast
import time
# API authentication configuration
CLIENT_ID = config('CLIENT_ID')
AUTHORIZATION = config('AUTHORIZATION')
wrapper = IGDBWrapper(CLIENT_ID, AUTHORIZATION)
# PARAMETERS
####################
# Current time as unix timestamp; appended to csv name
current_time = 1683225945
# Total entries per POST request (max is 500)
limit_value = 500
# total_entries should be divisible by limit value
# Indicates the total number of entries found overall by running the program once
total_entries = 1000
# Can use as an offset if you don't want to start search from beginning of database
alltime_entries = 0
# Checks to see if you want the games outputted to have release dates or have a release date of TBD
# True = release dates, False = TBD
want_release_date = True
if want_release_date:
release_date = 'release_dates.date > ' + str(current_time) + '; '
else:
release_date = 'release_dates.category = 7; '
####################
# List to hold all dataframes to be combined later on
dataframe_list = []
# Sends multiple POST requests using the previously defined parameters
for offset in range(alltime_entries, total_entries, limit_value):
# POST request for data on upcoming titles on PC including name, publisher, release date, website links
post_req = 'fields name,involved_companies.company.name,release_dates.human,platforms,websites.url; ' \
'where involved_companies.publisher = true & ' + release_date + '' \
'sort date asc; ' \
'limit ' + str(limit_value) + '; ' \
'offset ' + str(offset) + ';'
# Sends POST request to the games endpoint
byte_array = wrapper.api_request(
'games',
post_req
)
# Decodes byte array returned by API wrapper and converts to JSON
byte_array = bytearray(byte_array)
req_dict = ast.literal_eval(byte_array.decode('utf8'))
json_result = json.dumps(req_dict, indent=4, sort_keys=True)
# Converts JSON to dataframe
dataframe = pd.read_json(json_result)
# Cleans up data by inserting only relevant data points into each cell & converts platform ids to names
for entry in range(len(dataframe)):
company_list = []
if type(dataframe['involved_companies'][entry]) == list:
for company in dataframe['involved_companies'][entry]:
company_list.append(company['company']['name'])
dataframe['involved_companies'][entry] = company_list
dates_list = []
if type(dataframe['release_dates'][entry]) == list:
for date in dataframe['release_dates'][entry]:
dates_list.append(date['human'])
dataframe['release_dates'][entry] = dates_list
website_list = []
if type(dataframe['websites'][entry]) == list:
for website in dataframe['websites'][entry]:
website_list.append(website['url'])
dataframe['websites'][entry] = website_list
platform_list = []
if type(dataframe['platforms'][entry]) == list:
for platform in dataframe['platforms'][entry]:
if platform in platform_ids:
platform_list.append(platform_ids[platform])
else:
platform_list.append(platform)
dataframe['platforms'][entry] = platform_list
# Appends dataframe to the dataframe list
dataframe_list.append(dataframe)
# Combines all dataframes into one dataframe
combined_dataframe = pd.concat(dataframe_list)
# Converts to dataframe to csv
csv_name = 'database' + str(time.time()) + '.csv'
outfile = open(csv_name, 'wb')
combined_dataframe.to_csv(outfile, index=False, header=True, sep=',', encoding='utf-8')
outfile.close()