# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

import csv
import os
import json
import numpy as np
import pandas as pd
from pprint import pprint
import random

datasetSizeController = 200
src_Folder = "C:\\Users\\haris\\Documents\\Altmetric Data\\data\\"
with open("RandomData.csv", mode = 'w', newline = '') as csvFile:
    fieldnames = ['altmetric_id', 'abstract', 'blog_post']
    csvWriter = csv.DictWriter(csvFile, fieldnames = fieldnames)
    csvWriter.writeheader()

filesCount = 0
filesTried = 0

# +
with open("RandomData.csv", mode = 'a', newline = '') as csvFile:
    csvWriter = csv.writer(csvFile)
    random.seed(25)
    while(filesCount < datasetSizeController):
        rand = random.randint(10,846)
        json_Folder = src_Folder + str(rand)
        fileName = json_Folder + "\\" + str(random.choice(os.listdir(json_Folder)))
        try:
            with open(fileName) as json_file:
                json_data = json.load(json_file)
                #print("File opened")
                filesTried = filesTried + 1
                altmetric_id = json_data['altmetric_id']
                if ('abstract' not in json_data['citation']):
                    #print("Debug1")
                    continue
                else:
                    #print("Debug2")
                    abstract = json_data['citation']['abstract']
                    abstract = abstract.replace(',', '')
                    
                    if('blogs' not in json_data['posts']):
                        #print("Debug3")
                        continue
                    else:
                        print("Debug4")
                        blog_post = json_data['posts']['blogs'][0]['summary']
                        blog_post = blog_post.replace(',', '')
                        print(str(blog_post))
                        filesCount = filesCount + 1
                        csvWriter.writerow([altmetric_id, abstract, blog_post])
                print("Files Tried: " + str(filesTried))
                print("Files Read: " + str(filesCount))
                #print(str(filesCount))
        except:
            pass

#print("CSV File Generated!")                  
# -



StarcoderdataJupyterScriptsDedupFiltered