Created
May 2, 2017 00:21
-
-
Save jsutterfield/e6d0692d9a8d60cc93c97752ee0dc4d6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/python | |
import csv | |
class GetPostCreator(object): | |
THEME_IDS = [1, 2, 3] | |
def __init__(self, id, row): | |
self.id = self.wrap_in_quotes(str(id)) | |
self.type = self.get_type(row[0]) | |
self.headline = self.wrap_in_quotes(row[1]) | |
self.supporting_text = self.wrap_in_quotes_or_null(row[2]) | |
self.tags = self.get_tags(row[7]) | |
self.source_attribution = self.wrap_in_quotes(row[4]) | |
self.source_url = self.wrap_in_quotes(row[3]) | |
self.author_name = self.wrap_in_quotes_or_null(row[5]) | |
self.author_url = self.wrap_in_quotes_or_null(row[6]) | |
self.created_at = "april28MinusDay(" + str(id) + ")" | |
self.theme_id = self.wrap_in_quotes(str(GetPostCreator.THEME_IDS[id % len(GetPostCreator.THEME_IDS)])) | |
self.image = self.wrap_in_quotes_or_null(row[9]) | |
self.embedded_posts = None | |
self.is_in_story_stream = row[10] == "Yes" | |
def get_type(self, type): | |
if type == "Original" or type == "Video": | |
return "FANDOM_ARTICLE" | |
elif type == "Instagram": | |
return "INSTAGRAM" | |
elif type == "Tweet": | |
return "TWEET" | |
elif type == "Curated": | |
return "OTHER" | |
elif type == "Story Stream": | |
return "STORY_STREAM" | |
else: | |
raise Exception("type not found") | |
def wrap_in_quotes_or_null(self, val): | |
if val: | |
return self.wrap_in_quotes(val) | |
return "null" | |
def get_tags(self, tags): | |
tag_dict = { | |
"Featured": "featured()", | |
"Games": "games()", | |
"Movies": "movies()", | |
"TV": "tv()" | |
} | |
tag_calls = [] | |
for tag in tags.split(","): | |
stripped_tag = tag.strip() | |
if stripped_tag in tag_dict: | |
tag_calls.append(tag_dict[stripped_tag]) | |
if tag_calls: | |
return "ImmutableList.of(" + ",".join(tag_calls) + ")" | |
return "Collections.emptyList()" | |
def get_embedded_posts(self): | |
if not self.embedded_posts: | |
return "null" | |
return "ImmutableList.of(" + ",\n".join(map(lambda posts: posts.get_string_repr(), self.embedded_posts)) + ")" | |
def wrap_in_quotes(self, val): | |
return '"' + val + '"' | |
def get_string_repr(self): | |
return 'getPost(' + ",\n".join( [ | |
self.id, | |
self.type, | |
self.headline, | |
self.supporting_text, | |
self.tags, | |
self.source_attribution, | |
self.source_url, | |
self.author_name, | |
self.author_url, | |
self.created_at, | |
self.theme_id, | |
self.image, | |
self.get_embedded_posts() | |
] ) + ')' | |
class GenerateText(object): | |
def __init__(self): | |
self.reader = csv.reader(open("output.csv", "rb")) | |
next(self.reader) # skip header line | |
def generateText(self): | |
postId = 0 | |
posts = [] | |
for row in self.reader: | |
postId += 1 | |
posts.append(GetPostCreator(postId, row)) | |
posts = self.combine_story_stream(posts) | |
print ",\n".join(map(lambda method_call: method_call.get_string_repr(), posts)) | |
def combine_story_stream(self, posts): | |
all_posts = [] | |
stories_in_stream = [] | |
story_stream = None | |
for post in posts: | |
if post.is_in_story_stream: | |
stories_in_stream.append(post) | |
elif post.type == "STORY_STREAM": | |
story_stream = post | |
else: | |
all_posts.append(post) | |
story_stream.embedded_posts = stories_in_stream | |
all_posts.append(story_stream) | |
return all_posts | |
if __name__ == "__main__": | |
generateText = GenerateText() | |
generateText.generateText() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment